关于Android的涂料绘制颜色 [英] Regarding Android Paint drawing color

查看:118
本文介绍了关于Android的涂料绘制颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DrawView.java

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;
}

}

我想画出不同颜色的手指画,但每当我改变油漆的颜色,则所有previous路径或图纸获取和绘制最新的颜色我想提请用不同的颜色我怎么能做到这一点?请给我一些解决方案这一点。

I am trying to draw finger paint with different colors but whenever i change the color of paint then all previous path or drawing get and draw with updated color i want to draw with different colors how can i do that ? please give me some solutions for this.

推荐答案

要做到这一点,你必须创建一个新的油漆为每个绘制的对象。这是因为当画布重绘,它引用了相同的油漆对象的时候,让所有的路径将使用这种涂料

To do this, you'd have to create a new Paint for every object drawn. This is because when the Canvas redraws, it references that same Paint object every time, so all paths will use this paint.

首先,我会改变你的路径阵列中同时包含一个油漆路径。您可以使用Android键入做到这一点。

Firstly, I would change your paths array to contain both a Paint and a Path. You can achieve this using the Android type Pair.

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

您也将有你的 undonePaths 变量转换以这种方式。

You will also have to convert your undonePaths variable in this manner.

然后,在你的 touch_up()方法,你需要添加这个新的油漆对象。

Then, in your touch_up() method, you need to add this new Paint object.

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

最后,你的循环,必须同时调整如下:

Lastly, your loop has to be adjusted for this as well:

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

这是相当内存密集型的,所以你必须要照顾好重置这些项目时,他们不再使用,但有这么多不同的颜色,你的必须的拥有所有的这些不同的油漆的对象。

This is quite memory intensive, so you will have to take good care to reset these items when they're no longer in use, but to have so many different colors, you must have all of these different Paint objects.

这篇关于关于Android的涂料绘制颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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