public class MyView extends View { // int bh = originalBitmap.getHeight(); // int bw = originalBitmap.getWidth(); public MyView(Context c,int w,int h) { super(c); mBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888); // Bitmap mBitmap = // Bitmap.createScaledBitmap(originalBitmap,200,true); mCanvas = new Canvas(mBitmap); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); //mBitmapPaint.setColor(Color.YELLOW); //mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); } @Override protected void onSizeChanged(int w,int h,int oldw,int oldh) { super.onSizeChanged(w,oldw,oldh); // mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888); // mCanvas = new Canvas(mBitmap); } @Override protected void onDraw(Canvas canvas) { //canvas.drawColor(customColor); canvas.drawBitmap(mBitmap,mBitmapPaint); canvas.drawPath(mPath,mPaint); } // //////************touching evants for painting**************/////// private float mX,mY; private static final float TOUCH_TOLERANCE = 5; private void touch_start(float x,float y) { //mCanvas.drawCircle(x,y,progress+1,mPaint); mPath.reset(); mPath.moveTo(x,y); //mPaint.setStyle(Paint.Style.FILL); //mPath.addCircle(x,(float) (progress+0.15),Direction.CW); mCanvas.drawPath(mPath,mPaint); mX = x; mY = y; //mPaint.setStyle(Paint.Style.STROKE); } private void touch_move(float x,float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX,mY,(x + mX) / 2,(y + mY) / 2); mX = x; mY = y; } } private void touch_up() { mPath.lineTo(mX,mY); // commit the path to our offscreen mCanvas.drawPath(mPath,mPaint); // kill this so we don't double draw mPath.reset(); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touch_start(x,y); invalidate(); break; case MotionEvent.ACTION_MOVE: touch_move(x,y); invalidate(); break; case MotionEvent.ACTION_UP: touch_up(); invalidate(); break; } return true; } // end of touch events for image }
解决方法
is there any way to draw circle or point using path?
而不是尝试这样做使用类Canvas中的方法drawPoint(float x,float y,Paint paint).
要在API演示中使用它,您需要更改3件事:
>有一个私有布尔mDrawPoint;在MyView类中区分点击和幻灯片.
>如果路径已更改(即在if语句中),则在touch_start()中将mDrawPoint设置为true,在touch_move()中将mDrawPoint设置为false.
>在touch_up()中检查mDrawPoint的值.如果它是假的,那么该函数之前做了什么,如果它是真的,那么在画布上绘制点:
mCanvas.drawPoint(mX,mPaint);
新版touch_up():
private void touch_up() { if(mDrawPoint == true) { mCanvas.drawPoint(mX,mPaint); } else { mPath.lineTo(mX,mPaint); // kill this so we don't double draw mPath.reset(); } }
when i move up my finger after drawing a line it automatically draws a point next to it,where the line ended. And when i starts a new line/curve the prevIoUsly drawn line would remove /clear from the canvas leaving only dots behind that were drawn.
您不需要比我的答案更多的修改.你可能忘了实现它的一部分.
我在尝试它时遇到了同样的问题并在发布我的答案之前解决了它.这是因为你在两个不同的画布上绘制,在onDraw方法上,当你的手指起来时它会丢失而另一个是mCanvas,这是在touch_up中保存行的地方.这就是为什么touch_up有一个if:
如果我们没有移动手指(只是点击),那么我们将点绘制到mCanvas,以便它仍然存在于下一个onDraw上(onDraw将mCanvas绘制到它作为参数接收的画布上,以便绘制旧的线条和点mCanvas仍然可见).
如果我们移动了手指,那么我们将传递给onDraw的画布所绘制的路径保存到mCanvas,以便在获得手指后它仍然存在.
您遇到的问题来自touch_up函数的else部分永远不会被执行,因此在touch_up上无论是否应该绘制一个点,并且路径永远不会被提交到mCanvas,因此下次onDraw被调用时会消失.
这很可能源于你在touch_start()中将mDrawPoint设置为true,正如我在第2点中所说的那样.但忘记在touch_move中将mDrawPoint设置为false,正如我在第2点中所说的那样.
这是我的touch_move的样子:
private void touch_move(float x,float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX,(x + mX)/2,(y + mY)/2); mX = x; mY = y; mDrawPoint = false; } }