(X轴Mirror镜像,Y轴Repeat重复)
本篇博客是承接 Android 图片特效处理:Shader着色器、渲染 博客,对Shader的几种子类做进一步介绍,看下他们的各自的效果。
在上次的博客中根据API可以知道BitmapShader有三种Mode,但是并未给出具体的实例,这里对BitmapShader的三种Mode区别一下,看下他们分别的效果。
1、要使用BimapShader我们就需要创建一个BitmapShader(其中有个Bitmap参数,因此还需要一张Bitmap)
mbitmap=BitmapFactory.decodeResource(getResources(), R.drawable.yourimg);
BitmapShader mshader=new BitmapShader(mbitmap, TileMode.CLAMP, TileMode.CLAMP);
2、将BitmapShader与Paint进行绑定
mpaint.setShader(mshader);
3、onDraw方法中绘制
mbitmap=BitmapFactory.decodeResource(getResources(), R.drawable.yourimg);
4、小区别:
下面是我们的Bitmap创建时使用的两张图片,一张比较小的,一张比较大的。之前的介绍可以将CLAMP看做是一种拉伸,通过BitmapShader在图片较小的时候,实现的是拉伸图片的右下角的颜色进行填充,就形成了我们看到的第一幅图的样式。
当图片比较大的时候,会选择一部分展示。
使用步骤与BitmapShader的CLAMP相同,只是mode修改一下
BitmapShader mshader=new BitmapShader(mbitmap, TileMode.REPEAT, TileMode.REPEAT);
BitmapShader mshader=new BitmapShader(mbitmap, TileMode.MIRROR , TileMode.MIRROR );
镜像效果:
通过BitmapShader的几种mode的图片样式的展现,相信你一看就明白了,需要注意一点的就是我们的CLAMP(拉伸),它是根据图片大小进行绘制的。
public class MyBitmapShader extends View{
private int width;
private int height;
private Paint mpaint;
private Bitmap mbitmap;
public MyBitmapShader(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyBitmapShader(Context context, AttributeSet attrs) {
super(context, attrs);
mbitmap=BitmapFactory.decodeResource(getResources(), R.drawable.yourimg);
mpaint=new Paint();
//X轴Mirror镜像,Y轴Repeat重复效果见本博客最开始的图片展示
BitmapShader mshader=new BitmapShader(mbitmap, TileMode.MIRROR, TileMode.REPEAT);
mpaint.setShader(mshader);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(200,200, 200, mpaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
}
}