** * author:Created by WeiWeifeng on 2018/4/12. */ public class Xiaoguo extends View implements View.OnClickListener { private RectF rectF; private Paint paint; private int centerX; private int centerY; private Paint paint1; private int[] colors; private Paint pai; private int stepAngle; float x=0; float y=0; public Xiaoguo(Context context) { this(context, null); } public Xiaoguo(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public Xiaoguo(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); getInit(); } private void getInit() { setOnClickListener(this); //画笔 paint = new Paint(); paint.setColor(Color.GRAY); paint1 = new Paint(); paint1.setColor(Color.RED); pai = new Paint(); colors = new int[]{Color.BLUE, Color.RED, Color.YELLOW, Color.CYAN, Color.BLACK, Color.GREEN, Color.BLUE, Color.RED, Color.YELLOW, Color.CYAN, Color.BLACK, Color.GREEN}; stepAngle = 360 / colors.length; //装扇形的矩形 rectF = new RectF(-200, -200, 200, 200); } //测量 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = getMeasuredWidth(); int height = getMeasuredHeight(); centerX = width / 2; centerY = height / 2; } int d = 60; int dd = 60; //动画 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //设置远点在正中心 // canvas.translate(point.x + centerX, point.y + centerY); canvas.translate(x + centerX, y + centerY); // canvas.translate(centerX,centerY); //让装扇形的矩形显示 // canvas.drawRect(rectF,paint); canvas.rotate(d); //创建一个圆 // canvas.drawCircle(, point.y, 200, paint); //创建一个扇形 // canvas.drawArc(rectF, 0, 30, true, paint1); //旋转 drawCircleWithColor(canvas); } private void drawCircleWithColor(Canvas canvas) { int angleStart = 0; for (int i = 0; i < colors.length; i++) { pai.setColor(colors[i]); canvas.drawArc(rectF, angleStart, stepAngle, true, pai); angleStart += stepAngle; } } int dian = 1; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.e("myMessage", "ACTION_DOWN" + dian); break; case MotionEvent.ACTION_MOVE: x = event.getX() - centerX; y = event.getY() - centerY; Log.e("myMessage", "ACTION_MOVE" + y); dd += 10; postInvalidate(); break; case MotionEvent.ACTION_UP: dian++; Log.e("myMessage", "ACTION_UP"); break; } return super.onTouchEvent(event); } @Override public void onClick(View view) { Log.e("myMessage", "onClick"); TimerTask timerTask = new TimerTask() { @Override public void run() { d += 10; if (dian % 2 == 0) { Log.e("myMessage", "Dui"); postInvalidate(); } } }; Timer timer = new Timer(); timer.schedule(timerTask, 2000, 30); } }
自定义View实现画布手动or自动旋转,点击暂停,画布跟随鼠标移动
最新推荐文章于 2022-01-18 11:04:18 发布