Android自定義View實(shí)現(xiàn)風(fēng)車效果
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)風(fēng)車效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
畫桿
public class WindmillRodView extends View { ? ? private int mWidth; ? ? private int mHeight; ? ? private Paint mPaint; ? ? public WindmillRodView(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public WindmillRodView(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public WindmillRodView(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? init(); ? ? } ? ? private void init() { ? ? ? ? mPaint = new Paint(); ? ? ? ? mPaint.setColor(Color.WHITE); ? ? ? ? mPaint.setStyle(Paint.Style.FILL); ? ? ? ? mPaint.setAntiAlias(true); ? ? ? ? mPaint.setDither(true); ? ? } ? ? @Override ? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) { ? ? ? ? super.onSizeChanged(w, h, oldw, oldh); ? ? ? ? mWidth = getMeasuredWidth(); ? ? ? ? mHeight = getMeasuredHeight(); ? ? } ? ? private int _rod_width = dp2px(2); ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? int xCenter = mWidth / 2; ? ? ? ? int yCenter = mHeight / 3; ? ? ? ? int radius = mHeight / 3 * 2; ? ? ? ? drawRod(canvas, xCenter, yCenter, radius); ? ? } ? ? private void drawRod(Canvas canvas, int xCenter, int yCenter, int radius) { ? ? ? ? Path path = new Path(); ? ? ? ? path.moveTo(xCenter - _rod_width, yCenter); ? ? ? ? path.lineTo(xCenter - 2 * _rod_width, radius - dp2px(5)); ? ? ? ? path.lineTo((xCenter + 2 * _rod_width), radius - dp2px(5)); ? ? ? ? path.lineTo(xCenter + _rod_width, yCenter); ? ? ? ? path.close(); ? ? ? ? canvas.drawPath(path, mPaint); ? ? ? ? RectF rectF = new RectF(xCenter - 2 * _rod_width, ? ? ? ? ? ? ? ? radius - dp2px(8), ? ? ? ? ? ? ? ? xCenter + 2 * _rod_width, ? ? ? ? ? ? ? ? radius - dp2px(3)); ? ? ? ? canvas.drawOval(rectF, mPaint); ? ? } ? ? private int dp2px(int dp) { ? ? ? ? return (int) (Resources.getSystem().getDisplayMetrics().density * dp + 0.5); ? ? } }
先畫風(fēng)車的桿,再在底部畫一個(gè)橢圓
畫風(fēng)車
public class WindmillView extends View { ? ? private int mWidth; ? ? private int mHeight; ? ? private Paint mPaint; ? ? private ObjectAnimator mRotationAnim; ? ? public WindmillView(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public WindmillView(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public WindmillView(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? init(); ? ? } ? ? private void init() { ? ? ? ? mPaint = new Paint(); ? ? ? ? mPaint.setColor(Color.WHITE); ? ? ? ? mPaint.setStyle(Paint.Style.FILL); ? ? ? ? mPaint.setAntiAlias(true); ? ? ? ? mPaint.setDither(true); ? ? } ? ? @Override ? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) { ? ? ? ? super.onSizeChanged(w, h, oldw, oldh); ? ? ? ? mWidth = getMeasuredWidth(); ? ? ? ? mHeight = getMeasuredHeight(); ? ? } ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? int xCenter = mWidth / 2; ? ? ? ? int yCenter = mHeight / 3; ? ? ? ? int radius = mHeight / 3 * 2; ? ? ? ? canvas.drawCircle(xCenter, yCenter - dp2px(7), dp2px(4), mPaint); ? ? ? ? setPivotX(xCenter); ? ? ? ? setPivotY(yCenter - dp2px(7)); ? ? ? ? canvas.save(); ? ? ? ? for (int i = 0; i < 3; i++) { ? ? ? ? ? ? Path path = new Path(); ? ? ? ? ? ? path.moveTo(xCenter, 0); ? ? ? ? ? ? path.lineTo(xCenter, yCenter - dp2px(11)); ? ? ? ? ? ? path.lineTo(xCenter + dp2px(8), yCenter - dp2px(26)); ? ? ? ? ? ? path.close(); // ? ? ? ?mPaint.setStrokeJoin(Paint.Join.ROUND); ? ? ? ? ? ? CornerPathEffect cornerPathEffect = new CornerPathEffect(30); ? ? ? ? ? ? mPaint.setPathEffect(cornerPathEffect); ? ? ? ? ? ? canvas.drawPath(path, mPaint); ? ? ? ? ? ? canvas.rotate(360 / 3, xCenter, yCenter - dp2px(7)); ? ? ? ? } ? ? ? ? canvas.restore(); ? ? ? ? startAnim(); ? ? } ? ? private int dp2px(int dp) { ? ? ? ? return (int) (Resources.getSystem().getDisplayMetrics().density * dp + 0.5); ? ? } ? ? public void startAnim() { ? ? ? ? if (mRotationAnim != null && mRotationAnim.isRunning()) return; ? ? ? ? mRotationAnim = ObjectAnimator.ofFloat(this, "Rotation", 360f) ? ? ? ? ? ? ? ? .setDuration(3000); ? ? ? ? mRotationAnim.setRepeatCount(-1); ? ? ? ? mRotationAnim.setInterpolator(new LinearInterpolator()); ? ? ? ? mRotationAnim.start(); ? ? } ? ? public void stopAnim() { ? ? ? ? if (mRotationAnim != null && mRotationAnim.isRunning()) { ? ? ? ? ? ? mRotationAnim.cancel(); ? ? ? ? ? ? mRotationAnim = null; ? ? ? ? } ? ? } }
這里使用畫面的旋轉(zhuǎn)方法,繪制扇頁(yè)
旋轉(zhuǎn)
使用屬性動(dòng)畫來(lái)旋轉(zhuǎn)
.... ?public void startAnim() { ? ? ? ? if (mRotationAnim != null && mRotationAnim.isRunning()) return; ? ? ? ? mRotationAnim = ObjectAnimator.ofFloat(this, "Rotation", 360f) ? ? ? ? ? ? ? ? .setDuration(3000); ? ? ? ? mRotationAnim.setRepeatCount(-1); ? ? ? ? mRotationAnim.setInterpolator(new LinearInterpolator()); ? ? ? ? mRotationAnim.start(); ? ? } ? ? public void stopAnim() { ? ? ? ? if (mRotationAnim != null && mRotationAnim.isRunning()) { ? ? ? ? ? ? mRotationAnim.cancel(); ? ? ? ? ? ? mRotationAnim = null; ? ? ? ? } ? ? } ?....
在布局文件中使用
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="#000000"> ? ? <com.example.windmill.WindmillRodView ? ? ? ? android:layout_width="200dp" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:layout_centerInParent="true" /> ? ? <com.example.windmill.WindmillView ? ? ? ? android:layout_width="200dp" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:layout_centerInParent="true" /> </RelativeLayout>
這里只是介紹了如何繪制類似的效果,很多計(jì)算都是寫死的,如果要實(shí)際使用的話,最好寫成自定義屬性通過(guò)xml屬性聲明傳進(jìn)去。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?Studio實(shí)現(xiàn)簡(jiǎn)單頁(yè)面跳轉(zhuǎn)的詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于Android?Studio實(shí)現(xiàn)簡(jiǎn)單頁(yè)面跳轉(zhuǎn)的詳細(xì)教程,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android?Studio具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01Android Studio項(xiàng)目中導(dǎo)入開源庫(kù)的方法
這篇文章主要介紹了Android Studio項(xiàng)目中導(dǎo)入開源庫(kù)的方法,即使用第三方庫(kù)、第三廣場(chǎng)框架的方法,需要的朋友可以參考下2015-06-06Android常用控件ImageSwitcher使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android常用控件ImageSwitcher的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android開發(fā)實(shí)現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中實(shí)現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09AndroidStudio插件GsonFormat之Json快速轉(zhuǎn)換JavaBean教程
這篇文章主要介紹了AndroidStudio插件GsonFormat之Json快速轉(zhuǎn)換JavaBean教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Android自定義View圓形和拖動(dòng)圓、跟隨手指拖動(dòng)效果
單純的自定義一個(gè)圓非常簡(jiǎn)單 只需要幾步就完成 拖動(dòng)圓添加實(shí)現(xiàn)觸摸事件即可 。接下來(lái)通過(guò)本文給大家分享Android自定義View圓形和拖動(dòng)圓、跟隨手指拖動(dòng)效果,感興趣的朋友一起看看吧2017-09-09Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法詳解
這篇文章主要介紹了Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法,結(jié)合實(shí)例形式分析了Android開發(fā)中ServiceManager自定義服務(wù)的相關(guān)創(chuàng)建與使用方法,需要的朋友可以參考下2017-08-08Retrofit Rxjava實(shí)現(xiàn)圖片下載、保存并展示實(shí)例
本篇文章主要介紹了Retrofit Rxjava實(shí)現(xiàn)圖片下載、保存并展示實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06