Android的FingerPaint样品不画点? [英] Android FingerPaint sample does not draw dot?

查看:120
本文介绍了Android的FingerPaint样品不画点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fingerpaint例如,在样本的Andr​​oid API演示用手指触摸屏幕上不画点/点。在code他们使用路径来绘制线条,有没有什么办法来绘制圆形或点上使用的路径?

 公共类MyView的扩展视图{
    // INT BH = originalBitmap.getHeight();
    // INT体重= originalBitmap.getWidth();

    公共MyView的(上下文C中,int W,INT高){
        超级(C);
        mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
        //位图mBitmap =
        // Bitmap.createScaledBitmap(originalBitmap,200,200,真正的);
        mCanvas =新的Canvas(mBitmap);
        的mpath =新路径();
        mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
        //mBitmapPaint.setColor(Color.YELLOW);
        //mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }

    @覆盖
    保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
        super.onSizeChanged(W,H,oldw,oldh);
        // mBitmap = Bitmap.createBitmap(BW,BH,Bitmap.Config.ARGB_8888);
        // mCanvas =新的Canvas(mBitmap);
    }


    @覆盖
    保护无效的OnDraw(帆布油画){
        //canvas.drawColor(customColor);
        canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
        canvas.drawPath(的mpath,mPaint);
    }

    // ////// ************触摸evants绘画************** ///////
    私人浮动MX,我的;
    私有静态最终浮动TOUCH_TOLERANCE = 5;

    私人无效touch_start(浮X,浮动Y){
        //mCanvas.drawCircle(x,Y,进步+ 1,mPaint);

        mPath.reset();
        mPath.moveTo(X,Y);
        //mPaint.setStyle(Paint.Style.FILL);
        //mPath.addCircle(x,Y(浮动)(进度+ 0.15),Direction.CW);
        mCanvas.drawPath(的mpath,mPaint);
        MX = X;
        我= Y;
        //mPaint.setStyle(Paint.Style.STROKE);

    }

    私人无效TOUCH_MOVE(浮X,浮动Y){
        浮DX = Math.abs(X  -  MX);
        浮DY = Math.abs(Y  - 我的);
        如果(DX> = TOUCH_TOLERANCE || DY> = TOUCH_TOLERANCE){
            mPath.quadTo(MX,MY,(X + MX)/ 2,(Y +我)/ 2);
            MX = X;
            我= Y;
        }
    }

    私人无效touch_up(){
        mPath.lineTo(MX,MY);
        //提交路径,我们的屏幕外
        mCanvas.drawPath(的mpath,mPaint);
        //杀了这个,所以我们不重复抽奖
        mPath.reset();
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件){
        浮X = event.getX();
        浮动Y = event.getY();
        开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:
            touch_start(X,Y​​);
            无效();
            打破;
        案例MotionEvent.ACTION_MOVE:
            TOUCH_MOVE(X,Y);
            无效();
            打破;
        案例MotionEvent.ACTION_UP:
            润色();
            无效();
            打破;
        }
        返回true;
    } //触摸事件进行图像结束
}
 

下面是code,我应该怎么编辑此code,以能画点/点finertouch?

解决方案
  

有没有什么办法来绘制圆形或点上使用的路径?

而不是试图这样做,用的Canvas类的方法 drawPoint(浮X,浮动Y,油漆涂料)

要使用它的API演示中,你将需要改变三样东西:

  1. 有一个私人布尔mD​​rawPoint; MyView的类水龙头和幻灯片之间的区别
  2. 设置 mDrawPoint touch_start() TOUCH_MOVE()如果该路径被改变(即在如果语句)。
  3. touch_up()检查 mDrawPoint 的价值。如果是假的做的功能做之前,如果这是真的,那么在画布上绘制了这一点: mCanvas.drawPoint(MX,我,mPaint);

新版本 touch_up()

 私人无效touch_up(){
    如果(mDrawPoint ==真){
        mCanvas.drawPoint(MX,我,mPaint);
    } 其他 {
        mPath.lineTo(MX,MY);
        //提交路径,我们的屏幕外
        mCanvas.drawPath(的mpath,mPaint);
        //杀了这个,所以我们不重复抽奖
        mPath.reset();
    }
}
 

  

当我向上画一条线,它会自动绘制在它旁边的一个点,在该行结束后我的手指。当我开始新的线/曲线previously画线将删除/从画布上留下的,只有点清晰绘制。

您不需要任何比什么在我的回答更多的修改。你可能忘了实现它的一部分。

想起来的时候我有同样的问题,并张贴我的答案之前解决它。它造成的,因为你画在两个不同的画布上给予的OnDraw 方法,它会丢失,当你的手指起来,另一种是 mCanvas ,这就是该行保存在 touch_up 。这也就是为什么 touch_up 有一个如果

如果我们没有动的手指(只是拍了拍),那么我们得出点到 mCanvas ,以便它仍然存在于一个的OnDraw 的OnDraw 绘制 mCanvas 到画布上它接收参数,因此,老年人线和点涂在 mCanvas 仍清晰可见)。

如果我们移动手指,然后我们将这个被吸引到传递给的OnDraw mCanvas 以便它仍然是present得到手指后

您的问题来自于 touch_up 函数的其他从来没有得到执行,这样对<$一部分C $ C> touch_up A点被绘制无论它是否应该和路径从未被提交到 mCanvas ,因此接下来的时间<$消失C $ C>的OnDraw 之称。

这从您 mDrawPoint 设置为true,最有可能干touch_start()正如我在第2点说。 却忘记设置 mDrawPoint 高达虚假 TOUCH_MOVE 我在第2点也表示

下面是我的 TOUCH_MOVE 是这样的:

 私人无效TOUCH_MOVE(浮X,浮动Y){
    浮DX = Math.abs(X  -  MX);
    浮DY = Math.abs(Y  - 我的);
    如果(DX&GT; = TOUCH_TOLERANCE || DY&GT; = TOUCH_TOLERANCE){
        mPath.quadTo(MX,MY,(X + MX)/ 2,(Y +我)/ 2);
        MX = X;
        我= Y;
        mDrawPoint = FALSE;
        }
    }
 

Fingerpaint example in sample android api demo does not draw dot/point by touching finger on screen. In code they have used Path to draw line, is there any way to draw circle or point using path?

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,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, h, 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, 0, 0, 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, y, (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
}

Here is the code, what should i edit in this code to able to draw dot/point on finertouch?

解决方案

is there any way to draw circle or point using path?

Instead of trying to do that use the method drawPoint(float x, float y, Paint paint) in the class Canvas.

To use it in the API demo you will need to change 3 things:

  1. Have a private boolean mDrawPoint; in the MyView class to differentiate between a tap and a slide.
  2. Set mDrawPoint to true in touch_start() and to false in touch_move() if the path is changed (that is in the if statement).
  3. In touch_up() check the value of mDrawPoint. If it is false do what the function did before and if it is true then draw the point on the canvas: mCanvas.drawPoint(mX, mY, mPaint);

New version of touch_up():

private void touch_up() {
    if(mDrawPoint == true) {
        mCanvas.drawPoint(mX, mY, mPaint);          
    } else {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, 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.

You don't need any more modification than what's in my answer. You probably forgot to implement a part of it.

I had the same problem when trying it up and solved it before posting my answer. It was caused because you draw on two different canvas, on given to the onDraw method, which gets lost when your finger gets up and the other is mCanvas, which is where the line is saved in touch_up. This is why touch_up has an if:

If we didn't move the finger (just tapped) then we draw the point to the mCanvas so that it is still there on the next onDraw (onDraw draws the mCanvas to the canvas it receives as argument so that older lines and points painted on mCanvas are still visible).

If we moved the finger then we save the path that was drawn to the canvas passed to onDraw to the mCanvas so that it is still present after getting the finger up.

The problem you have comes from the else part of the touch_up function never getting executed so that on touch_up a point gets drawn regardless of whether it should and the path never gets committed to mCanvas and thus disappear the next time onDraw is called.

This most likely stem from you setting mDrawPoint to true in touch_start() as I said in point 2. but forgetting to set mDrawPoint up to false in touch_move as I also said in point 2.

Here is what my touch_move looks like:

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;
        mDrawPoint = false;
        }
    }

这篇关于Android的FingerPaint样品不画点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆