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

在android中,如何在imageview的同一区域上填充另一种颜色?,android,canvas,imageview,paint,Android,Canvas,Imageview,Paint,我有imageview,我设置了“A”字符来填充颜色。我的实际问题是如何在imageview的同一部分的不同颜色上填充颜色?下面是我的颜色托盘源代码,用于单击更改不同的颜色。 case R.id.btn_color_one: { // do something for button 1 click setupDrawing(); paint.setColor(RED); break;

我有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));

    }

位图类有一个返回颜色的getPixel(intx,inty)


然后您可以检查该值并再次将Pixel设置为新值。只需运行所有位图检查红色并将其设置为蓝色或其他颜色。

在进行了大量的研发之后,我得到了我的答案:

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

}
private final Paint mPaintSrcIn=new Paint(Paint.ANTI_别名_标志| Paint.DITHER_标志| Paint.FILTER_位图_标志);
私有最终油漆mPaintDstIn=新油漆(油漆.ANTI_别名_标志|油漆.DITHER_标志|油漆.FILTER_位图_标志);
专用最终油漆mPaintColor=新油漆(油漆.防漆别名标志);
专用最终油漆MPAInterator=新油漆(油漆.防油漆别名标志);
私有最终矩阵mMatrix=新矩阵();
私有最终画布mLayerCanvas=新画布();
私人住宅;
私有位图形状;
私有位图mLayerBitmap;
private ArrayList mDrawOps=new ArrayList();
private DrawOp mCurrentOp=new DrawOp();
private ArrayList mUndoneOps=new ArrayList();
公共绘图视图(上下文)
{
这个(上下文,null,0);
}
公共绘图视图(上下文、属性集属性)
{
这(上下文,属性,0);
}
公共绘图视图(上下文、属性集属性、int-defStyle)
{
超级(上下文、属性、定义样式);
mPaintSrcIn.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.SRC_-IN));
mPaintDstIn.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.DST_-IN));
mPaintColor.setStyle(绘制.样式.笔划);
mPaintColor.setStrokeJoin(绘制.连接.圆形);
mPaintColor.setStrokeCap(油漆盖圆形);
MPAInterser.set(mPaintColor);
mPaintEraser.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaintEraser.setMaskFilter(新的BlurMaskFilter(getResources())
.getDisplayMetrics().density*4,BlurMaskFilter.Blur.NORMAL));
}
公共空间设置形状(内部和外部)
{
BitmapFactory.Options=new-BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.ALPHA_8;
setShape(BitmapFactory.decodeResource(getResources(),内部,选项),
decodeResource(getResources(),outer,options));
}
公共无效设置形状(int[]内部,int[]外部){
对于(int i=0;i 0){
DrawOp redo=mUndoneOps.remove(mUndoneOps.size()-1);
mDrawOps.add(重做);
使无效();
}
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh)
{
super.onSizeChanged(w,h,oldw,oldh);
mLayerBitmap=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
mLayerCanvas.setBitmap(mLayerBitmap);
if(mOuterShape!=null){
intdx=(w-mOuterShape.getWidth())/2;
int dy=(h-mOuterShape.getHeight())/2;
mMatrix.setTranslate(dx,dy);
}
}
@凌驾
受保护的void onDraw(画布)
{
super.onDraw(帆布);
if(isInEditMode()){
返回;
}
//注意:没有额外的位图或图层..但硬件加速不支持setMaskFilter,这意味着
//橡皮擦在绘图时有很强的边缘。
//@见http://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported 
/*
drawBitmap(mOuterShape,0,0,null);
canvas.saveLayer(null、mpain、canvas.FULL\u COLOR\u LAYER\u SAVE\u标志);
canvas.drawColor(0,PorterDuff.Mode.CLEAR);
drawBitmap(minnersape,0,0,null);
saveLayer(null,mPaintSrcIn,canvas.FULL\u COLOR\u LAYER\u SAVE\u标志);
drawBitmap(mBitmapDraw,0,0,null);
canvas.drawPath(mPath,mPaintDraw);
canvas.restore();
canvas.restore();
*/
//清除软件画布
mLayerCanvas.drawColor(0,PorterDuff.Mode.CLEAR);
//从行动计划中画出一幅画
用于(绘图操作:mDrawOps){
drawOp(mLayerCanvas,op);
}
drawOp(mLayerCanvas、mCurrentOp);
//将图形遮罩到形状的内表面区域
mLayerCanvas.drawBitmap(Minnersape、mMatrix、mPaintDstIn);
//绘制要查看的原始形状
drawBitmap(mOuterShape,mMatrix,null);
//绘制遮罩图像以查看
drawBitmap(mLayerBitmap,0,0,null);
}
私有void drawOp(画布,drawOp)
{
if(op.path.isEmpty()){
返回;
}
最终油漆;
if(op.type==DrawOp.type.PAINT){
油漆=mPaintColor;
油漆。设置颜色(op.color);
油漆。设置行程宽度(操作行程);
}否则{
油漆=MPaInterser;
油漆。设置行程宽度(操作行程);
}
mLayerCanvas.绘制路径(操作路径,绘制);
}
@SuppressLint(“ClickableViewAccessibility”)
@凌驾
公共布尔onTouchEvent(运动事件)
{
最终浮点x=event.getX();
最终浮点y=event.getY();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
mUndoneOps.clear();
mCurrentOp.path.moveTo(x,y);
打破
case MotionEvent.ACTION\u移动:
对于(int i=0;i
if(mybitmap.getPixel(x,y)=Color.CYAN){mybitmap.setPixel(x,y,Color.RED)}
记得为x添加一个,为y添加另一个,并使用您的颜色而不是我的颜色哪个值代表x,y?我还使用画布和绘画对象。伙计!只需运行位图的宽度和高度(从零开始)。您可以通过使用