1

Android 开发新手,我的应用是在画布上绘制路径(具有位图),从颜色选择器中选择多种颜色。先前绘制的路径颜色正在使用画布上的新路径颜色进行更新。任何帮助我,这是我的代码......

@Override
public void run() {
    // TODO Auto-generated method stub
    while (locker) {
        canvas = null;
        try {
            if (!holder.getSurface().isValid()) {
                continue;
            }
            canvas = holder.lockCanvas();

            draw(canvas);

        } finally {
            if (canvas != null) {
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // synchronized (thread.getSurfaceHolder()) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        path = new Path();
        path.moveTo(event.getX(), event.getY());
        path.lineTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        path.lineTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        path.lineTo(event.getX(), event.getY());
        paths.add(path);
    }
    return true;
    // }
}

/**
 * This method deals with paint-works. Also will paint something in
 * background
 */
private void draw(Canvas canvas) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.bell);
    // canvas.drawColor(Color.CYAN);
    canvas.drawBitmap(bitmap, 20, 20, mPaint);
    // canvas.drawPath(mPath, mPaint);
    for (Path path : paths) {
        // canvas.drawPoint(graphic.x, graphic.y, mPaint);
        canvas.drawPath(path, mPaint);
    }
}

}

4

1 回答 1

0

您需要记住每条路径的绘制颜色。例如,您可以拥有一个与路径列表同时填充的颜色列表(整数)。然后在你的 draw 方法中这样做:

private void draw(Canvas canvas) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.bell);
    canvas.drawBitmap(bitmap, 20, 20, mPaint);

    int count = path.size();
    for (int i = 0; i < count; i++) {
        mPaint.setColor(colors.get(i));
        canvas.drawPath(paths.get(i), mPaint);
    }
}
于 2013-01-16T10:38:13.540 回答