什么是BitMapShader
顾名思义,BitMapShader 就是用bitmap对绘制的图形进行渲染着色,其实就是用图片对图形进行贴图.
BitmapShader构造函数如下所示
BitmapShader(Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY)
第一个参数是Bitmap对象,该Bitmap决定了用什么图片对绘制的图形进行贴图。
第二个参数和第三个参数都是Shader.TileMode类型的枚举值,有以下三个取值:CLAMP 、REPEAT 和 MIRROR。
- CLAMP
CLAMP表示,当所画图形的尺寸大于Bitmap的尺寸的时候,会用Bitmap四边的颜色填充剩余空间。 - REPEAT
REPEAT表示,当我们绘制的图形尺寸大于Bitmap尺寸时,会用Bitmap重复平铺整个绘制的区域。
示例代码如下所示:
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
paint.setShader(bitmapShader);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
- MIRROR
与REPEAT类似,当绘制的图形尺寸大于Bitmap尺寸时,MIRROR也会用Bitmap重复平铺整个绘图区域,与REPEAT不同的是,两个相邻的Bitmap互为镜像。
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
paint.setShader(bitmapShader);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
利用BitmapShader实现圆角图片:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = getWidth();
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
float bitmapWith = Math.min(bitmap.getWidth(),bitmap.getHeight());
float scale = width/bitmapWith;
Matrix matrix = new Matrix();
matrix.postScale(scale,scale);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
bitmapShader.setLocalMatrix(matrix);
Paint paint = new Paint();
paint.setShader(bitmapShader);
canvas.drawRect(0,0,getRight(),getBottom(),paint);
}
注意两点:
- BitmapShader仅用于对图形着色,如矩形、圆形、椭圆等简单形状。在这些几何形状内部用指定的bitmap进行贴图.
当bitmap的尺寸小于绘制的图形尺寸时,由Shader.TileMode决定了如何进行(填充)贴图 - Shader即是着色器的意思,BitMapShader只是着色器Shader的一种,此外还有:
- LinearGradient 线性渐变着色器
- RadialGradient 放射渐变着色器
- SweepGradient 360度颜色旋转渐变效果
这些着色器都是用来填充几何图形的,或者对图形进行贴图的。
paint.setShader(bitmapShader);
其他Shader介绍:
- LinearGradient
我们可以用LinearGradient创建线性渐变效果,其有两个构造函数:
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)
实例:
public void testLinearGradient(Canvas canvas){
LinearGradient linearGradient = new LinearGradient(100,100,500,500, Color.GREEN,Color.YELLOW,Shader.TileMode.REPEAT);
Paint paint = new Paint();
paint.setShader(linearGradient);
canvas.drawRect(0,0,canvas.getWidth(),canvas.getHeight(),paint);
}
- RadialGradient
我们可以用RadialGradient创建从中心向四周发散的辐射渐变效果,其有两个构造函数:
RadialGradient(float centerX, float centerY, float radius, int centerColor, int edgeColor, Shader.TileMode tileMode)
RadialGradient(float centerX, float centerY, float radius, int[] colors, float[] stops, Shader.TileMode tileMode)
实例:
public void testRadialGradient(Canvas canvas){
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
float centerX = canvasWidth / 2f;
float centerY = canvasHeight / 2f;
float radius = canvasWidth / 4f;
RadialGradient radialGradient = new RadialGradient(centerX, centerY, radius, Color.GREEN, Color.BLUE, Shader.TileMode.MIRROR);
Paint paint = new Paint();
paint.setShader(radialGradient);
canvas.drawRect(0, 0, canvasWidth, canvasHeight, paint);
}
- SweepGradient
SweepGradient可以用来创建360度颜色旋转渐变效果,具体来说颜色是围绕中心点360度顺时针旋转的,起点就是3点钟位置。
SweepGradient有两个构造函数:
SweepGradient(float cx, float cy, int color0, int color1)
SweepGradient(float cx, float cy, int[] colors, float[] positions)
实例:
public void testSweepGradient(Canvas canvas){
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
float centerX = canvasWidth / 2f;
float centerY = canvasHeight / 2f;
float radius = canvasWidth / 4f;
SweepGradient sweepGradient = new SweepGradient(centerX, centerY, Color.GREEN, Color.BLUE);
Paint paint = new Paint();
paint.setShader(sweepGradient);
canvas.drawCircle(centerX, centerY, radius, paint);
}
参考链接:
http://blog.csdn.net/iispring/article/details/50500106
利用BitmapShaper实现圆角图片:
https://www.jianshu.com/p/6b5eef0f6f3d