1

有人能解释一下为什么这段代码不能画出每个对象吗?

public class A extends View {
private Paint paint = new Paint();
private Path path = new Path();
ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();


public A(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    for (Pair<Path, Paint> p : paths) {
        canvas.drawPath(p.first, p.second);
    }
    canvas.drawPath(path, paint);
}



@Override
public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(3f);

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            int color = Color.rgb(new Random().nextInt(255),
                                new Random().nextInt(255), 
                                new Random().nextInt(255));
            paint.setColor(color);
            path.reset(); //new stroke, get old one erased
            int historySize = event.getHistorySize();
            for (int i = 0; i > historySize; i++) {
                path.moveTo(eventX, eventY);
            }
             path.moveTo(eventX, eventY);

            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_UP:
            path.lineTo(eventX, eventY);
            // End of stroke, add this to the collection
            paths.add(new Pair<Path, Paint>(path, paint));
            break;
        default:
            break;
    }
    // Schedules a repaint.
    invalidate();
    return true;
}

}

我正在使用 onTouchEvent 捕捉每个笔画,并创建存储在一对对象中的不同路径/绘画对象。可悲的是,当我尝试绘制它们时,在我的 OnDraw 中它失败了。我读过一些主题但没有找到正确的答案。每次有人建议在位图中创建和工作并将其绘制到屏幕上,但我想避免这种解决方案。

谢谢你的帮助 !

4

3 回答 3

1

问题是你总是使用相同的PathPaint对象。您应该创建新的Path并且Paint每次都MotionEvent.ACTION_DOWN被解雇

于 2013-03-10T11:39:35.390 回答
0

在 switch 条件下删除 path.reset。抽奖有效。但是颜色确实会改变,即使是之前的平局。您重置清除上一次绘制的路径。

每次绘制时,我都会使用不同的一对(路径和绘画)。

如果您想使用画布进行绘制,下面的代码就可以了。我还会添加一个颜色选择器以允许用户选择他选择的颜色。

  public class MyView extends View {

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;

    public MyView(Context c) {
        super(c);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    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);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
        // 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;
    }
}
于 2013-03-10T11:29:21.330 回答
0

您只需要使用一个 Path 和 Paint 对象。

实际上可以在这里找到关于这整个事情的详细教程: 在 Android 上制作一个基本的单点触控绘图应用程序 - Creative Punch

于 2014-03-02T16:57:55.013 回答