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}
这是代码,我应该在这段代码中编辑什么才能在finertouch上绘制点/点?
解决方法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; } }总结
以上是内存溢出为你收集整理的Android FingerPaint示例不会绘制点?全部内容,希望文章能够帮你解决Android FingerPaint示例不会绘制点?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)