多色android的绘图线问题

时间:2017-09-25 05:47:24

标签: android view colors drawing

我正在使用这个类来绘制像多色的笔工具它工作得很好但是当我改变颜色时,它在选择颜色之间不会改变颜色。我检查并尝试了许多解决方案他们没有任何解决方案。

public class DrawingView extends View {
private static final float TOUCH_TOLERANCE = 4;
Paint mPaint;
//MaskFilter  mEmboss;
//MaskFilter  mBlur;
Bitmap mBitmap;
Canvas mCanvas;
Path mPath;
Paint mBitmapPaint;
ProgressDialog pd;
String color;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Paint> paints = new ArrayList<Paint>();
private ArrayList<Integer> colorlist = new ArrayList<Integer>();
private float mX, mY;

public DrawingView(Context context, String color) {
    super(context);
    // TODO Auto-generated constructor stub
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
     mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(10);
    pd = new ProgressDialog(context);
    mPath = new Path();
    paths.add(mPath);
     mBitmapPaint = new Paint();
    paints.add(mPaint);
    colorlist.add(Color.parseColor(color));
    mBitmapPaint.setColor(Color.parseColor(color));
}

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


    if (w > 0 && h > 0) {
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    } else {
        mBitmap = Bitmap.createBitmap(300, 250, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

}


@Override
public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.draw(canvas);
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    //  canvas.drawPath(mPath, mPaint);

    int count = paints.size();
    for (int i = 0; i < count; i++) {
        mPaint.setColor(colorlist.get(i));
        canvas.drawPath(paths.get(i), mPaint);
    }


}

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();
    // mPath= new Path();
}


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

}

我使用这个类画画,我在这样的活动中使用这个类。

mDrawingView = new DrawingView(getApplicationContext(), color);
                mDrawingPad.addView(mDrawingView);

mDrawingPad是一种线性布局,我可以在其中创建视图。

1 个答案:

答案 0 :(得分:0)

我从here

得到了这个问题的解决方案

我在代码中根据此解决方案进行了一些更改,最后我找到了它。

public class DrawingView extends View {
private static final float TOUCH_TOLERANCE = 4;
Paint mPaint;
//MaskFilter  mEmboss;
//MaskFilter  mBlur;
Bitmap mBitmap;
Canvas mCanvas;
Path mPath;
Paint mBitmapPaint;
ProgressDialog pd;
String color;
ArrayList<Pair<Path, Paint>> p = new ArrayList<Pair<Path, Paint>>();
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Paint> paints = new ArrayList<Paint>();
private float mX, mY;

public DrawingView(Context context, String color) {
    super(context);
    // TODO Auto-generated constructor stub
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    //  mPaint.setColor(Color.parseColor(color));
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(10);
    pd = new ProgressDialog(context);
    mPath = new Path();
    paths.add(mPath);
    mBitmapPaint = new Paint();
    paints.add(mPaint);
    p.add(new Pair<Path, Paint>(mPath, mPaint));

    //colorlist.put(mPath,color);
    mBitmapPaint.setColor(Color.parseColor(color));
}

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


    if (w > 0 && h > 0) {
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    } else {
        mBitmap = Bitmap.createBitmap(300, 250, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

}


@Override
public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.draw(canvas);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    //  canvas.drawPath(mPath, mPaint);

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

}

private void touch_start(float x, float y) {
    mPaint.setColor(ImageEditOptionActivity.pencolor);
    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
    p.add(new Pair<Path, Paint>(mPath, mPaint));
    mPath.reset();
    mPath = new Path();
    //colorlist.put(mPath,color);
    mPaint = new Paint(mPaint);
    p.add(new Pair<Path, Paint>(mPath, mPaint));


}


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

}