【问题标题】:Regarding Android Paint drawing color关于Android Paint绘制颜色
【发布时间】:2012-08-04 05:34:16
【问题描述】:

DrawView.java

public class DrawView extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
public Paint mPaint;
ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();

private MaskFilter mEmboss;
private MaskFilter mBlur;

private Bitmap im;

public DrawView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    paths.clear();
    undonePaths.clear();

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(4);
    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

    mCanvas = new Canvas();
    mPath = new Path();
    // paths.add(mPath);

    im = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.ic_launcher);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Share.cColor);

    for (Path p : paths) {

        canvas.drawPath(p, mPaint);
    }

    canvas.drawPath(mPath, mPaint);

}

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

private void touch_start(float x, float y) {

    mPaint.setColor(Share.dColor);
    undonePaths.clear();
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
}

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
    paths.add(mPath);
    mPath = new Path();

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
}

public void onClickUndo() {

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
    if (paths.size() > 0) {
        undonePaths.add(paths.remove(paths.size() - 1));
        invalidate();
    } else {

    }
    // toast the user
}

public void onClickRedo() {

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
    if (undonePaths.size() > 0) {
        paths.add(undonePaths.remove(undonePaths.size() - 1));
        invalidate();
    } else {

    }
    // toast the user
}

@Override
public boolean onTouch(View arg0, 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;
}

}

我正在尝试用不同的颜色绘制手指画,但是每当我改变颜料的颜色时,所有以前的路径或绘图都会获取并使用更新的颜色进行绘制,我想用不同的颜色进行绘制,我该怎么做?请给我一些解决方案。

【问题讨论】:

    标签: android draw paint


    【解决方案1】:

    为此,您必须为每个绘制的对象创建一个新的Paint。这是因为当Canvas 重绘时,它每次都引用同一个Paint 对象,所以所有路径都会使用这个绘制。

    首先,我将更改您的paths 数组以同时包含PaintPath。您可以使用 Android 类型 Pair 来实现此目的。

    ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
    

    您还必须以这种方式转换您的 undonePaths 变量。

    然后,在您的touch_up() 方法中,您需要添加这个新的Paint 对象。

    Paint newPaint = new Paint(mPaint); // Clones the mPaint object
    paths.add(new Pair<Path, Paint>(mPath, newPaint));
    

    最后,您的循环也必须为此进行调整:

    for (Pair<Path, Paint> p : paths) {
        canvas.drawPath(p.first, p.second);
    }
    

    这会占用大量内存,因此当这些项目不再使用时,您必须小心重置它们,但是要拥有这么多不同的颜色,您必须拥有所有这些不同的Paint 对象。

    【讨论】:

    • 非常感谢您为我提供了我的查询的解决方案。再次衷心感谢
    • @dilipkaklotar 我遇到了同样的问题,我更改了答案中提到的代码,但是撤消和重做不起作用,你能检查一下为什么会这样。我的代码链接是pastebin.com/Ygvu27k3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多