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

Introduction to the

SetShader is the fourth implementation of Shader, BitmapShader. The first three are gradients. BitmapShader uses images as shaders, and Paint draws shapes from bitmaps in the Shader. BitmapShader Uses bitmap to construct shader, and uses bitmap to draw.

The constructor

/**
 * Call this to create a new shader that will draw with a bitmap.
 *
 * @param bitmap The bitmap to use inside the shader
 * @param tileX The tiling mode for x to draw the bitmap in.
 * @param tileY The tiling mode for y to draw the bitmap in.
 */
public BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, 
@NonNull TileMode tileY) ;
Copy the code

Parameter description: Bitmap: Bitmap used to construct shader tileX: TileMode in the X-axis direction, tileY: TileMode in the Y-axis direction.

Use BitmapShader to draw the whole picture

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.redbag); BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mPaint.setShader(bitmapShader); Canvas. DrawRect (0, 0, 1000, 15a00, mPaint);Copy the code

Shape BitmapShader

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.redbag);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(bitmapShader);
canvas.drawCircle(500, 500, 400, mPaint);
Copy the code

\