The author: the summer solstice welcome reproduced, but keep the declaration blog.csdn.net/u011418943/…
To start, here’s a sample Google image that looks awkward:
Why do you say that?
In order to show the effect of PorterDuff in this picture, the code has made some effects, which leads to some friends who find that the effect is inconsistent with that of Google when they write the demo, so they have a question, is it my mistake? Where is the inconsistency? For example, if I write SRC_IN, it should look like this:
In fact, it works like this:
Oh, why is that? I’m not entering in the right way?
Don’t worry, here I want to say is that you did not understand wrong, just source code processing.
1. ApiDemo code
SRC and DST are the same image. Here’s the trick. First look at the code for SRC (the blue image) :
// create a bitmap with a rect, used for the "src" image
static Bitmap makeSrc(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(0xFF66AAFF);
c.drawRect(w/3, h/3, w*19/20, h*19/20, p);
return bm;
}Copy the code
It can be seen that although the width and height are set, the actual canvas is not filled and a blank area is left when drawing. Let’s put a border around it and see what it looks like:
As you can see, there is a large blank area here, and the same is true for the DST image:
And SRC_IN is drawn where they are compared, so the result of drawing the above two pictures can only look like this:
The truth finally came out that it was the boy
Well, here’s some code to help you understand these patterns, and go ahead:
public class XfermodeView extends View { private static final String TAG = "XfermodeView"; private PorterDuffXfermode mPdXfermode; Private static porterduff.mode PD_MODE = porterduff.mode. SRC_IN; private int mScreenW, mScreenH; Private int mWidth = 200; Private int nHeight = 200; private Bitmap mSrcBitmap, mDstBitmap; Private Paint mShowpaint; private Paint mShowpaint; public XfermodeView(Context context) { this(context, null); } public XfermodeView(Context context, AttributeSet attrs) { super(context, attrs); mShowpaint = new Paint(); mShowpaint.setStyle(Paint.Style.STROKE); mShowpaint.setStrokeWidth(2); mScreenW = ScreenUtil.getScreenW(context); mScreenH = ScreenUtil.getScreenH(context); // Create a PorterDuffXfermode object mPdXfermode = new PorterDuffXfermode(PD_MODE); MSrcBitmap = makeSrc(mWidth, nHeight); mDstBitmap = makeDst(mWidth, nHeight); } public XfermodeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } // Define a method to draw a circular Bitmap, Private Bitmap makeDst(int w, int h) {Bitmap bm = bitmap.createBitmap (w, h, bitmap.config.argb_8888); private Bitmap makeDst(int w, int h) {Bitmap bm = bitmap.createBitmap (w, h, bitmap.config.argb_8888); Canvas c = new Canvas(bm); Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setColor(0xFF26AAD1); c.drawOval(new RectF(0, 0, w * 3 / 4, h * 3 / 4), p); MShowpaint. SetColor (0xFF26AAD1); C.d rawRect (0, 0, w, h, mShowpaint); return bm; } // Define a method to draw a rectangle Bitmap, Private Bitmap makeSrc(int w, int h) {Bitmap BM = bitmap.createBitmap (w, h, bitmap.config.argb_8888); Canvas c = new Canvas(bm); Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setColor(0xFFFFCE43); Log.d(TAG, "zsr --> makeSrc: "+w/3+" "+w*19/20); c.drawRect(w / 3, h / 3, w * 19 / 20, h * 19 / 20, p); MShowpaint. SetColor (0xFFFFCE43); mShowpaint.setStyle(Paint.Style.STROKE); C.d rawRect (0, 0, w, h, mShowpaint); return bm; } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setFilterBitmap(false); paint.setStyle(Paint.Style.FILL); DrawBitmap (mSrcBitmap, (mScreenW / 3-mwidth) / 2, (mScreenH / 2-nheight) / 2, paint); drawBitmap(mScreenW / 3-mwidth) / 2, (mScreenH / 2-nheight) / 2, paint); DrawBitmap (mDstBitmap, (mScreenW / 3-mwidth) / 2 + mScreenW / 3, drawBitmap(mDstBitmap, (mScreenW / 3-mwidth) / 2 + mScreenW / 3, (mScreenH / 2 - nHeight) / 2, paint); Int sc = canvas.saveLayer(0, 0, mScreenW, mScreenH, null, canvas. ALL_SAVE_FLAG); canvas.drawBitmap(mDstBitmap, (mScreenW / 3 - mWidth) / 2 + mScreenW / 3 * 2, (mScreenH / 2 - nHeight) / 2, paint); SetXfermode (mPdXfermode); setXfermode(mPdXfermode); canvas.drawBitmap(mSrcBitmap, (mScreenW / 3 - mWidth) / 2 + mScreenW / 3 * 2, (mScreenH / 2 - nHeight) / 2, paint); paint.setXfermode(null); // Restore canvas canvas.restoreToCount(sc); // SRC line int srcleft = (mScreenW / 3-mwidth) / 2 + mScreenW / 3 * 2; // SRC line int srcleft = (mScreenW / 3-mwidth) / 2 + mScreenW / 3 * 2; int srctop = (mScreenH / 2 - nHeight) / 2; mShowpaint.setColor(0xFFFFCE43); canvas.drawRect(srcleft,srctop,srcleft + mWidth,srctop+ nHeight,mShowpaint); Mshowpaint.setcolor (0xFF26AAD1); canvas.drawRect(srcleft + 4,srctop + 4, srcleft + mWidth - 4,srctop+ nHeight -4 ,mShowpaint); }}Copy the code