画圆圈触摸

我试图画一个用户触摸屏幕的圈子。

public class MainActivity extends Activity implements OnTouchListener { LinearLayout layout; float x=0; float y=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); layout=(LinearLayout)findViewById(R.id.layout); layout.addView(new CustomView(MainActivity.this)); } public class CustomView extends View { Bitmap mBitmap; Paint paint; public CustomView(Context context) { super(context); mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888); paint=new Paint(); paint.setColor(Color.RED); paint.setStyle(Style.FILL); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.setBitmap(mBitmap); canvas.drawCircle(x, y, 50, paint); Toast.makeText(MainActivity.this, "DDD", 1).show(); } } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { layout.invalidate(); } return false; }} 

我会这样做:

 public class TestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new DrawingView(this)); } class DrawingView extends SurfaceView { private final SurfaceHolder surfaceHolder; private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); public DrawingView(Context context) { super(context); surfaceHolder = getHolder(); paint.setColor(Color.RED); paint.setStyle(Style.FILL); } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { if (surfaceHolder.getSurface().isValid()) { Canvas canvas = surfaceHolder.lockCanvas(); canvas.drawColor(Color.BLACK); canvas.drawCircle(event.getX(), event.getY(), 50, paint); surfaceHolder.unlockCanvasAndPost(canvas); } } return false; } } } 

这是你的版本与修复,使其工作:

 public class MainActivity extends Activity { LinearLayout layout; float x = 0; float y = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); layout=(LinearLayout)findViewById(R.id.layout); layout.addView(new CustomView(MainActivity.this)); } public class CustomView extends View { Bitmap mBitmap; Paint paint; public CustomView(Context context) { super(context); mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888); paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Style.FILL); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(x, y, 50, paint); } public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { x = event.getX(); y = event.getY(); invalidate(); } return false; } } } 

有了这个代码,当你触摸,画一个圆,你可以移动的圆:

 public class Draw extends View { private static final String TAG = "CirclesDrawingView"; boolean flag = false; /** Main bitmap */ //private Bitmap mBitmap = null; //private Rect mMeasuredRect; /** Stores data about single circle */ private static class CircleArea { int radius; int centerX; int centerY; CircleArea(int centerX, int centerY, int radius) { this.radius = radius; this.centerX = centerX; this.centerY = centerY; } @Override public String toString() { return "Circle[" + centerX + ", " + centerY + ", " + radius + "]"; } } /** Paint to draw circles */ private Paint mCirclePaint; Paint strokePaint; private static final int CIRCLES_LIMIT = 1; /** All available circles */ private HashSet<CircleArea> mCircles = new HashSet<CircleArea>(CIRCLES_LIMIT); private SparseArray<CircleArea> mCirclePointer = new SparseArray<CircleArea>(CIRCLES_LIMIT); /** * Default constructor * * @param ct {@link android.content.Context} */ public Draw(final Context ct) { super(ct); init(ct); } private void init(final Context ct) { // Generate bitmap used for background //mBitmap = BitmapFactory.decodeResource(ct.getResources(), R.drawable.abc_ic_menu_cut_mtrl_alpha); mCirclePaint = new Paint(); mCirclePaint.setColor(Color.argb(100,50,205,50)); mCirclePaint.setStrokeWidth(40); mCirclePaint.setStyle(Paint.Style.FILL); strokePaint = new Paint(); strokePaint.setColor(Color.argb(100,0,100,0)); strokePaint.setStyle(Paint.Style.STROKE); strokePaint.setStrokeWidth(3); } @Override public void onDraw(final Canvas canv) { // background bitmap to cover all area //canv.drawBitmap(mBitmap, null, mMeasuredRect, null); for (CircleArea circle : mCircles) { canv.drawCircle(circle.centerX, circle.centerY, circle.radius, mCirclePaint); canv.drawCircle(circle.centerX, circle.centerY, circle.radius, strokePaint); } } @Override public boolean onTouchEvent(@Nullable final MotionEvent event) { boolean handled = false; CircleArea touchedCircle; int xTouch; int yTouch; int pointerId; int actionIndex = event.getActionIndex(); // get touch event coordinates and make transparent circle from it switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: // it's the first pointer, so clear all existing pointers data clearCirclePointer(); xTouch = (int) event.getX(0); yTouch = (int) event.getY(0); // check if we've touched inside some circle touchedCircle = obtainTouchedCircle(xTouch, yTouch); touchedCircle.centerX = xTouch; touchedCircle.centerY = yTouch; mCirclePointer.put(event.getPointerId(0), touchedCircle); invalidate(); handled = true; break; case MotionEvent.ACTION_MOVE: final int pointerCount = event.getPointerCount(); for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) { // Some pointer has moved, search it by pointer id pointerId = event.getPointerId(actionIndex); xTouch = (int) event.getX(actionIndex); yTouch = (int) event.getY(actionIndex); touchedCircle = mCirclePointer.get(pointerId); if (null != touchedCircle) { touchedCircle.centerX = xTouch; touchedCircle.centerY = yTouch; } } invalidate(); handled = true; break; case MotionEvent.ACTION_UP: clearCirclePointer(); invalidate(); handled = true; break; case MotionEvent.ACTION_POINTER_UP: // not general pointer was up pointerId = event.getPointerId(actionIndex); mCirclePointer.remove(pointerId); invalidate(); handled = true; break; case MotionEvent.ACTION_CANCEL: handled = true; break; default: // do nothing break; } return super.onTouchEvent(event) || handled; } /** * Clears all CircleArea - pointer id relations */ private void clearCirclePointer() { mCirclePointer.clear(); } /** * Search and creates new (if needed) circle based on touch area * * @param xTouch int x of touch * @param yTouch int y of touch * * @return obtained {@link CircleArea} */ private CircleArea obtainTouchedCircle(final int xTouch, final int yTouch) { CircleArea touchedCircle = getTouchedCircle(xTouch, yTouch); if (null == touchedCircle) { touchedCircle = new CircleArea(xTouch, yTouch, 50/*mRadiusGenerator.nextInt(RADIUS_LIMIT) + RADIUS_LIMIT*/); if (mCircles.size() == CIRCLES_LIMIT) { } if (flag == false) { mCircles.add(touchedCircle); flag = true; } } return touchedCircle; } /** * Determines touched circle * * @param xTouch int x touch coordinate * @param yTouch int y touch coordinate * * @return {@link CircleArea} touched circle or null if no circle has been touched */ private CircleArea getTouchedCircle(final int xTouch, final int yTouch) { CircleArea touched = null; for (CircleArea circle : mCircles) { if ((circle.centerX - xTouch) * (circle.centerX - xTouch) + (circle.centerY - yTouch) * (circle.centerY - yTouch) <= circle.radius * circle.radius) { touched = circle; break; } } return touched; } } 

并用于onCreate

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Draw draw = new Draw(this); setContentView(draw); }