您的位置:首页 > 移动开发 > Android开发

Android画图之setXfermode处理两图相交叉的情况

2015-09-17 17:17 495 查看
注意:不能直接在”画布“上画,要先放张”纸“再画。

mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);

canvas原有的图片 可以理解为背景 就是dst

新画上去的图片 可以理解为前景 就是src



用法:

[code]...
PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.DST_OVER);
mPaintRect.setXfermode(mode);
...
mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
canvasBit=new Canvas(mBitmap);
...
canvasBit.drawCircle(width/2,height/2,width/2,mPaintCircel);//dst
canvasBit.drawRect(0,0,width/2,width/2,mPaintRect);//src
canvas.drawBitmap(mBitmap,0,0,null);


这里我用自己写的一个例子说明

我画了一个***的圆和一个绿色的矩形来举例

[code]public class MyBitmapTwo extends View{
    private int width;
    private int height;
    private Bitmap mBitmap;
    private Paint mPaintCircel;
    private Paint mPaintRect;
    private Canvas canvasBit;
    public MyBitmapTwo(Context context) {
        super(context);
    }

    public MyBitmapTwo(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaintCircel=new Paint();
        mPaintCircel.setColor(Color.YELLOW);

        mPaintRect=new Paint();
        mPaintRect.setColor(Color.GREEN);

        PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.DST_OVER);
        mPaintRect.setXfermode(mode);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);//设置画布的大小,长和宽

        mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        canvasBit=new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.RED);
        canvasBit.drawCircle(width/2,height/2,width/2,mPaintCircel);
        canvasBit.drawRect(0,0,width/2,width/2,mPaintRect);
        canvas.drawBitmap(mBitmap,0,0,null);
    }


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: