public class BitmapShader extends Shader
BitmapShader, Shader家族的 專門處理圖片渲染的
構造方法:
public BitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY)
bitmap:原圖
tile直譯為 瓷磚,瓦片。這里的TileMode 可看成是 鋪圖的模式。
tileX, tileY:x/y 方向鋪圖的模式
public enum TileMode {
CLAMP (0),
REPEAT (1),
MIRROR (2);
TileMode(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
} CLAMP:假設超出原始bounds(即原圖的邊界),則反復邊緣上的color
REPEAT:反復bitmap
MIRROR:反復bitmap。與REPEAT不同的時,它是鏡像反復,即:反向反復
例:
public class BitmapShaderView extends View {
private BitmapShader mBitmapShader;
private ShapeDrawable mShapeDrawable;
public BitmapShaderView(Context context, Bitmap bitmap) {
super(context);
mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mShapeDrawable = new ShapeDrawable(new OvalShape());
mShapeDrawable.getPaint().setShader(mBitmapShader);
// mShapeDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); //原圖大小
mShapeDrawable.setBounds(0, 0, bitmap.getWidth() * 2, bitmap.getHeight() * 2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.CYAN);
mShapeDrawable.draw(canvas);
}
} 在Activity中,setContentView(new BitmapShaderView(context, bitmap));
原圖 效果圖
x和y 邊緣反復
改:mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.REPEAT);
效果
x方向鏡像反復;y方向反復
改:mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
效果:
x方向反復。y方向鏡像反復
