渲染模式:
tileX tileY:The tiling mode for x/y to draw the bitmap in. 在位图上 X/Y 方向 瓦工/花砖/瓷砖 模式
CLAMP :如果渲染器超出原始边界范围,会复制范围内【边缘】染色
REPEA :横向和纵向的【重复】渲染器图片,平铺
MIRROR:横向和纵向的重复渲染器图片,这个和REPEAT 重复方式不一样,他是以【镜像】方式平铺
渲染模式示例代码
public
class
LinearGradientView
extends
View {
private Paint mPaint = null;
private Shader mLinearGradient11 = null;
private Shader mLinearGradient12 = null;
private Shader mLinearGradient13 = null;
private Shader mLinearGradient21 = null;
private Shader mLinearGradient22 = null;
private Shader mLinearGradient23 = null;
private Shader mLinearGradient31 = null;
private Shader mLinearGradient32 = null;
private Shader mLinearGradient33 = null;
public LinearGradientView(Context context) {
super(context);
// 第一第二个参数表示渐变起点,可以设置在对角等任意位置;第三第四个参数表示渐变终点;第五个参数表示渐变颜色;
// 第六个参数可以为空,表示坐标值为0-1 new float[] {0.25f, 0.5f, 0.75f, 1 } 如果为空,颜色均匀分布,沿梯度线。
mLinearGradient11 = new LinearGradient(0, 0, 0, 200, new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.BLACK }, null, TileMode.CLAMP);
mLinearGradient12 = new LinearGradient(0, 0, 0, 300, new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.BLACK }, new float[] { 0, 0.1f, 0.5f, 0.5f }, TileMode.CLAMP);
mLinearGradient13 = new LinearGradient(0, 0, 0, 400, new int[] { Color.RED, Color.GREEN, Color.BLUE }, null, TileMode.CLAMP);
mLinearGradient21 = new LinearGradient(0, 320, 0, 320 + 200, new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.BLACK }, null, TileMode.MIRROR);
mLinearGradient22 = new LinearGradient(0, 320, 0, 320 + 100, new int[] { Color.RED, Color.GREEN, Color.BLUE }, null, TileMode.MIRROR);
mLinearGradient23 = new LinearGradient(0, 320, 0, 320 + 100, new int[] { Color.RED, Color.GREEN, Color.BLUE }, new float[] { 0, 0.1f, 0.5f }, TileMode.MIRROR);
mLinearGradient31 = new LinearGradient(0, 640, 0, 640 + 200, new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.BLACK }, null, TileMode.REPEAT);
mLinearGradient32 = new LinearGradient(0, 640, 0, 640 + 100, new int[] { Color.RED, Color.GREEN, Color.BLUE }, null, TileMode.REPEAT);
mLinearGradient33 = new LinearGradient(0, 640, 0, 640 + 100, new int[] { Color.RED, Color.GREEN, Color.BLUE }, new float[] { 0, 0.1f, 0.5f }, TileMode.REPEAT);
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
//重复最后一个像素
mPaint.setStrokeWidth(2);
mPaint.setShader(mLinearGradient11);
canvas.drawRect(0, 0, 200, 300, mPaint);//200-300之间全部是最后的黑色
mPaint.setShader(mLinearGradient12);
canvas.drawRect(210, 0, 410, 300, mPaint);//0.5之后全部是黑色了
mPaint.setShader(mLinearGradient13);
canvas.drawRect(420, 0, 620, 300, mPaint); // LinearGradient的高度小于要绘制的矩形的高度时才会有重复、镜像等效果
canvas.drawLine(0, 310, 720, 310, mPaint);
//镜像效果
mPaint.setShader(mLinearGradient21);
canvas.drawRect(0, 320, 200, 620, mPaint);
mPaint.setShader(mLinearGradient22);
canvas.drawRect(210, 320, 410, 620, mPaint);
mPaint.setShader(mLinearGradient23);
canvas.drawRect(420, 320, 620, 620, mPaint);
canvas.drawLine(0, 630, 720, 630, mPaint);
![](https://i-blog.csdnimg.cn/blog_migrate/bf3329911b9286e106173947012e6bfb.jpeg)
//重复效果
mPaint.setShader(mLinearGradient31);
canvas.drawRect(0, 640, 200, 940, mPaint);
mPaint.setShader(mLinearGradient32);
canvas.drawRect(210, 640, 410, 940, mPaint);
mPaint.setShader(mLinearGradient33);
canvas.drawRect(420, 640, 620, 940, mPaint);
}
}
渲染器示例代码
public
class
MyShaderView
extends
View {
// Shader渲染器,专门用来渲染图像以及一些几何图形
private BitmapShader bitmapShader; //bitmap渲染器,主要用来渲染图像
private LinearGradient linearGradient; //线性渲染
private RadialGradient radialGradient; //环形渲染
private SweepGradient sweepGradient; //扫描渐变,围绕一个中心点扫描渐变就像电影里那种雷达扫描,用来梯度渲染
private ComposeShader composeShader; //混合渲染器,可以和其他几个子类组合起来使用
private Bitmap bitmap;
/**Bitmap的宽高*/
private int width, height;
private Paint paint;
/**渐变颜色数组*/
private final int[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };
/**用来指定颜色数组的相对位置,若为null则均匀分布*/
private final float[] positions = null;
public MyShaderView(Context context) {
super(context);
bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.icon)).getBitmap();
width = bitmap.getWidth();
height = bitmap.getHeight();
paint = new Paint();
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE); //背景色
//将Bitmap截取为圆形、圆角矩形
canvas.drawBitmap(bitmap, 0, 0, paint);//原图
bitmapShader = new BitmapShader(bitmap, TileMode.MIRROR, TileMode.MIRROR);
paint.setShader(bitmapShader);
canvas.drawOval(new RectF(width, 0, width * 2, height), paint);//圆形渲染图
canvas.drawRoundRect(width * 2, 0, width * 3, height, 30, 30, paint);//矩形渲染图
//绘制线性渐变的矩形
linearGradient = new LinearGradient(0, 200, 400, 400, colors, positions, TileMode.REPEAT);// 渐变起 初点/终点坐标 x/y 位置;
paint.setShader(linearGradient);
canvas.drawRect(0, 200, 400, 400, paint);//渲染范围最好和图形大小一致
![](https://i-blog.csdnimg.cn/blog_migrate/229181ed04466ecfde993a596df99fe1.jpeg)
//绘制环形渐变的圆和矩形
radialGradient = new RadialGradient(100, 500, 100, colors, positions, TileMode.REPEAT);// 中心坐标、半径
paint.setShader(radialGradient);
canvas.drawCircle(100, 500, 100, paint);
canvas.drawRect(200, 400, 300, 600, paint);
radialGradient = new RadialGradient(300, 500, 100, colors, positions, TileMode.MIRROR);
paint.setShader(radialGradient);
canvas.drawCircle(400, 500, 100, paint);
canvas.drawRect(500, 400, 600, 600, paint);
//绘制梯形渐变的矩形
sweepGradient = new SweepGradient(300, 710, colors, positions);//中心坐标
paint.setShader(sweepGradient);
canvas.drawRect(0, 610, 600, 810, paint);
//绘制混合渐变(线性与环形混合)的矩形
linearGradient = new LinearGradient(0, 820, 300, 1120, colors, positions, TileMode.REPEAT);
composeShader = new ComposeShader(linearGradient, bitmapShader, PorterDuff.Mode.SRC_OVER);
paint.setShader(composeShader);
canvas.drawRect(0, 820, 600, 1120, paint);
}
}