如何在android的imageview相同区域填充另一种颜色的颜色?

时间:2014-09-10 10:12:04

标签: android canvas imageview paint

我有imageview,我设置了“A”字符来填充颜色。我的实际问题是如何在imageview的同一部分上填充不同颜色的颜色?下面是我的彩色托盘源代码,用于更改点击时的不同颜色。

case  R.id.btn_color_one: {
            // do something for button 1 click

            setupDrawing();
            paint.setColor(RED);
            break;
            }
        case  R.id.btn_color_two: {
            // do something for button 1 click

            setupDrawing();
            paint.setColor(LIGHT_BLUE);
            break;
            }

这是一个setupDrawing(),

public void setupDrawing(){
        initializeMP();
        playsound.start();

        canvas = new Canvas(bitmap);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStrokeWidth(30);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));

    }

2 个答案:

答案 0 :(得分:1)

Bitmap类有一个返回颜色http://developer.android.com/reference/android/graphics/Color.html的getPixel(int x,int y)。

然后您可以再次检查值和setPixel到新的值。只需运行所有位图检查红色并将其设置为蓝色或其他任何内容。

答案 1 :(得分:0)

在做了很多R& D后,我得到了答案:

private final Paint  mPaintSrcIn   = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
private final Paint  mPaintDstIn   = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
private final Paint  mPaintColor   = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint  mPaintEraser  = new Paint(Paint.ANTI_ALIAS_FLAG);

private final Matrix mMatrix = new Matrix();
private final Canvas mLayerCanvas = new Canvas();

private Bitmap mInnerShape;
private Bitmap mOuterShape;
private Bitmap mLayerBitmap;

private ArrayList<DrawOp> mDrawOps = new ArrayList<DrawOp>();
private DrawOp mCurrentOp = new DrawOp();

private ArrayList<DrawOp> mUndoneOps = new ArrayList<DrawOp>();


public DrawingView(Context context)
{
    this(context, null, 0);
}

public DrawingView(Context context, AttributeSet attrs)
{
    this(context, attrs, 0);
}

public DrawingView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);

    mPaintSrcIn.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    mPaintDstIn.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    mPaintColor.setStyle(Paint.Style.STROKE);
    mPaintColor.setStrokeJoin(Paint.Join.ROUND);
    mPaintColor.setStrokeCap(Paint.Cap.ROUND);

    mPaintEraser.set(mPaintColor);
    mPaintEraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    mPaintEraser.setMaskFilter(new BlurMaskFilter(getResources()
        .getDisplayMetrics().density * 4, BlurMaskFilter.Blur.NORMAL));
}

public void setShape(int inner, int outer)
{
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ALPHA_8;
    setShape(BitmapFactory.decodeResource(getResources(), inner, options),
        BitmapFactory.decodeResource(getResources(), outer, options));
}

public void setShape(int[] inner, int[] outer){
    for(int i=0;i<inner.length;i++){
        for(int j=0;j<outer.length;j++){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ALPHA_8;
            setShape(BitmapFactory.decodeResource(getResources(), inner[i], options),
                BitmapFactory.decodeResource(getResources(), outer[j], options));
        }
    }

}

public void setShape(Bitmap inner, Bitmap outer)
{
    mInnerShape = inner;
    mOuterShape = outer;
    requestLayout();
    invalidate();
}

public void setDrawingColor(int color)
{
    mCurrentOp.reset();
    mCurrentOp.type = DrawOp.Type.PAINT;
    mCurrentOp.color = color;
}

public void setDrawingStroke(int stroke)
{
    mCurrentOp.reset();
    mCurrentOp.type = DrawOp.Type.PAINT;
    mCurrentOp.stroke = stroke;
}

public void enableEraser()
{
    mCurrentOp.reset();
    mCurrentOp.type = DrawOp.Type.ERASE;
}

public void clearDrawing()
{
    mDrawOps.clear();
    mUndoneOps.clear();
    mCurrentOp.reset();
    invalidate();
}

public void undoOperation()
{
    if(mDrawOps.size() > 0){
        DrawOp last = mDrawOps.remove(mDrawOps.size() - 1);
        mUndoneOps.add(last);
        invalidate();
    }
}

public void redoOperation()
{
    if(mUndoneOps.size() > 0){
        DrawOp redo = mUndoneOps.remove(mUndoneOps.size() - 1);
        mDrawOps.add(redo);
        invalidate();
    }
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
    super.onSizeChanged(w, h, oldw, oldh);
    mLayerBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mLayerCanvas.setBitmap(mLayerBitmap);

    if(mOuterShape != null){
        int dx = (w - mOuterShape.getWidth()) / 2;
        int dy = (h - mOuterShape.getHeight()) / 2;
        mMatrix.setTranslate(dx, dy);
    }
}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    if(isInEditMode()){
        return;
    }

    // NOTE: Without extra bitmap or layer.. but HW Acceleration does not support setMaskFilter which means
    // eraser has strong edges whilst drawing.
    // @see http://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported 
    /*
    canvas.drawBitmap(mOuterShape, 0, 0, null);
    canvas.saveLayer(null, mPaint, Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
    canvas.drawColor(0, PorterDuff.Mode.CLEAR);
    canvas.drawBitmap(mInnerShape, 0, 0, null);
    canvas.saveLayer(null, mPaintSrcIn, Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
    canvas.drawBitmap(mBitmapDraw, 0, 0, null);
    canvas.drawPath(mPath, mPaintDraw);
    canvas.restore();
    canvas.restore();
    */

    // Clear software canvas
    mLayerCanvas.drawColor(0, PorterDuff.Mode.CLEAR);

    // Draw picture from ops
    for(DrawOp op : mDrawOps){
        drawOp(mLayerCanvas, op);
    }
    drawOp(mLayerCanvas, mCurrentOp);

    // Mask the drawing to the inner surface area of the shape
    mLayerCanvas.drawBitmap(mInnerShape, mMatrix, mPaintDstIn);

    // Draw orignal shape to view
    canvas.drawBitmap(mOuterShape, mMatrix, null);

    // Draw masked image to view
    canvas.drawBitmap(mLayerBitmap, 0, 0, null);
}

private void drawOp(Canvas canvas, DrawOp op)
{
    if(op.path.isEmpty()){
        return;
    }
    final Paint paint;
    if(op.type == DrawOp.Type.PAINT){
        paint = mPaintColor;
        paint.setColor(op.color);
        paint.setStrokeWidth(op.stroke);
    }else{
        paint = mPaintEraser;
        paint.setStrokeWidth(op.stroke);
    }
    mLayerCanvas.drawPath(op.path, paint);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event)
{
    final float x = event.getX();
    final float y = event.getY();

    switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            mUndoneOps.clear();
            mCurrentOp.path.moveTo(x, y);
            break;

        case MotionEvent.ACTION_MOVE:
            for(int i = 0; i < event.getHistorySize(); i++){
                mCurrentOp.path.lineTo(event.getHistoricalX(i), event.getHistoricalY(i));
            }
            mCurrentOp.path.lineTo(x, y);
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mCurrentOp.path.lineTo(x, y);
            mDrawOps.add(new DrawOp(mCurrentOp));
            mCurrentOp.path.reset();
            break;
    }

    invalidate();

    return true;
}

}