您好,登錄后才能下訂單哦!
本篇內容介紹了“Android怎么自定義View實現風車效果”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
效果圖:
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); } }
先畫風車的桿,再在底部畫一個橢圓
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; } } }
這里使用畫面的旋轉方法,繪制扇頁
使用屬性動畫來旋轉
.... 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>
這里只是介紹了如何繪制類似的效果,很多計算都是寫死的,如果要實際使用的話,最好寫成自定義屬性通過xml屬性聲明傳進去。
“Android怎么自定義View實現風車效果”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。