Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. ComposeShader profile

ComposeShader Combines the effects of the other two shaders. ComposeShader constructor:

/** * Create a new compose shader, given shaders A, B, and a combining mode. * When the mode is applied, it will be given the result from shader A as its * "dst", and the result from shader B as its "src". * * @param shaderA The colors from this shader are seen as the "dst" by the mode * @param shaderB The colors from this shader are seen as the "src" by the mode * @param mode The mode that combines  the colors from the two shaders. If mode * is null, then SRC_OVER is assumed. */ public ComposeShader(@NonNull Shader shaderA, @NonNull Shader shaderB, @ NonNull Xfermode mode); /** * Create a new compose shader, given shaders A, B, and a combining PorterDuff mode. * When the mode is applied, it will be given the result from shader A as its * "dst", and the result from shader B as its "src". * * @param shaderA The colors from this shader are seen as the "dst" by the mode * @param shaderB The colors from this shader are seen as the "src" by the mode * @param mode The PorterDuff mode that combines the colors from the two shaders. */ public ComposeShader(@NonNull Shader shaderA, @nonnull Shader shaderB, @nonnull porterduff.mode Mode);Copy the code

Parameter description: shaderA,shaderB: two shaders to be mixed. Xfermode mode: mode for combining two shader colors, PorterDuff.Mode mode: mode for combining two Shader colors.

2. What is Xfermode?

Xfermode is called graphics blending mode, also known as transition mode, which blends two graphics into a single image.

PorterDuffXfermode is used to calculate the image transition mode during graphic composition.

Constructor: PorterDuffXfermode(porterduff.mode Mode) Mode: mixed image Mode.

3. What is Porterduff.mode?

Porterduff. Mode is divided into Alpha synthesis Mode and hybrid Mode. Image composition is the blending of the colors of the source and background pixels, and the resulting color is determined by its RGB color component and Alpha value.

Porterduff. Mode in Android includes:

  • Mode.CLEAR;
  • Mode.SRC;
  • Mode.DST;
  • Mode.SRC_OVER;
  • Mode.DST_OVER;
  • Mode.SRC_IN;
  • Mode.DST_IN;
  • Mode.SRC_OUT;
  • Mode.DST_OUT;
  • Mode.SRC_ATOP;
  • Mode.DST_ATOP;
  • Mode.XOR;
  • Mode.DARKEN;
  • Mode.LIGHTEN;
  • Mode.MULTIPLY;
  • Mode.SCREEN;
  • Mode.ADD;
  • Mode.OVERLAY;

4. paint.setXfermode

RectF rectF = new RectF(0, 0, 700, 700);
canvas.saveLayer(rectF,mPaint);
canvas.drawBitmap(dstBmp, 0, 0, mPaint);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY));
canvas.drawBitmap(srcBmp, 0, 0, mPaint);
mPaint.setXfermode(null);
canvas.restore();
Copy the code

5.ComposeShader

Mix two shader, shader can be BitmapShader, that LinearGradient, RadialGradient, SweepGradient, so you can have a variety of combinations. Since the last parameter is related to porterduff. mode, mode applies the results from shaderA (ComposeShader first parameter) as the target image and shaderB (ComposeShader second parameter) as the source image. Mix and stack. If the two intersecting images are of different sizes, there will be parts that do not want to intersect, and the parts that do not want to intersect will not affect each other