package ss.jind.com.zhouoneweek.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; public class TaiJinView extends View implements View.OnClickListener { private RotateAnimation rotateAnimation; private int width = 360; private int height = 360; private int padding = 5; private Paint mPaint; private RectF mRectf; private RectF blackHalfRect1; private RectF whiteHalfRect; public TaiJinView(Context context) { super(context); } public TaiJinView(Context context, AttributeSet attrs) { super(context, attrs,0); } public TaiJinView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private void initPaint() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(5); } private void initAnim() { //以view的中心点为旋转参考点 rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setRepeatCount(-1); rotateAnimation.setFillAfter(false); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(width,height); mRectf = new RectF(0,0,width,width); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); initPaint(); initAnim(); setOnClickListener(this); drawCirCle(canvas); drawHalfCirCle(canvas); drawSmallCircle(canvas); } private void drawSmallCircle(Canvas canvas) { mPaint.setColor(Color.WHITE); mPaint.setStyle(Paint.Style.FILL); canvas.drawCircle(width/2,width/4,20,mPaint); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.FILL); canvas.drawCircle(width/2,width/4*3,20,mPaint); } private void drawCirCle(Canvas canvas) { mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.WHITE); canvas.drawArc(mRectf,270,180,true,mPaint); mPaint.setColor(Color.BLACK); canvas.drawArc(mRectf,270,-180,true,mPaint); } private void drawHalfCirCle(Canvas canvas) { //画上面黑色半圆 mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.BLACK); blackHalfRect1 = new RectF(width/4,0,width/2+width/4,width/2); canvas.drawArc(blackHalfRect1,270,180,true,mPaint); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.WHITE); whiteHalfRect = new RectF(width/4,width/2,width/2+width/4,width); canvas.drawArc(whiteHalfRect,270,-180,true,mPaint); } @Override public void onClick(View v) { rotateAnimation.setDuration(1000); rotateAnimation.setInterpolator(new LinearInterpolator());//不停顿 startAnimation(rotateAnimation); } }
动图太极
最新推荐文章于 2020-08-14 20:55:30 发布