画圆拖动0630

来源:互联网 发布:金融软件供应商 编辑:程序博客网 时间:2024/05/20 19:49
 importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Rect;importandroid.support.annotation.Nullable;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View/*** Created by muhanxi on 17/6/6.*/ publicclass CircleView extends View {  Paint paint;privatefloat x = 100;privatefloat y = 100;   Rect rect= new Rect(); publicCircleView(Context context) {super(context);} publicCircleView(Context context, @Nullable AttributeSetattrs) {super(context, attrs); paint = new Paint();paint.setColor(Color.RED);paint.setAntiAlias(true); } publicCircleView(Context context, @Nullable AttributeSetattrs, int defStyleAttr) {super(context, attrs, defStyleAttr);} publicCircleView(Context context, @Nullable AttributeSetattrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}   @Overrideprotectedvoid onDraw(Canvascanvas) {super.onDraw(canvas);   canvas.drawCircle(x,y,100,paint);rect.set((int)(x-100),(int) (y-100),(int)( x+100),(int)(y+100));   }  @Overridepublicboolean onTouchEvent(MotionEventevent) { switch (event.getAction()){ caseMotionEvent.ACTION_DOWN: breakcaseMotionEvent.ACTION_MOVE: float x1= event.getX() ;float y1= event.getY();if (x1> rect.left && x1 < rect.right && y1 > rect.top && y1 < rect.bottom) {//主线程调用刷新x = event.getX();y = event.getY();invalidate(); //子线程 调用刷新// postInvalidate();}           break;caseMotionEvent.ACTION_UP: break;caseMotionEvent.ACTION_POINTER_DOWN: System.out.println("event = ACTION_POINTER_DOWN" + event);break;   } returntrue;}}
原创粉丝点击