Paint着色器一
参考资料:http://blog.csdn.net/aigestudio/article/details/41799811
Paint有很多重要的方法,之前的颜色过滤器、过渡模式,这里开始Shader着色器
着色器有很5种,这里介绍第一种BitmapShader
1、BitmapShader简介
画布上在绘制时,使用一张图片,来设置不同的模式来着色,图片的起点则是0,0。模式有三种分别是:Shader.TileMode.CLAMP,Shader.TileMode.MIRROR,Shader.TileMode.REPEAT
tile[taɪl] 瓦片,TileMode平铺模式 clamp[klæmp]堆高、夹住 mirror[ˈmɪrə(r)]镜子,反射 repeat[rɪˈpi:t]重复
边缘拉伸,y轴边缘像素复制,再x轴边缘像素复制
public class ShaderView extends View{
private Paint mPaint;
public ShaderView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.b);
// BitmapShader是先应用了Y轴的模式而X轴是后应用的
mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(100, 100, 600, 600, mPaint);
}
}

上下左右镜像
public class ShaderView extends View{
private Paint mPaint;
public ShaderView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.b);
// BitmapShader是先应用了Y轴的模式而X轴是后应用的
mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(100, 100, 600, 600, mPaint);
}
}

上下左右重复
public class ShaderView extends View{
private Paint mPaint;
public ShaderView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.b);
// BitmapShader是先应用了Y轴的模式而X轴是后应用的
mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(100, 100, 600, 600, mPaint);
}
}

2、BitmapShader应用
绘制只显示圆圈的内容,随手指拖动显示
public class BrickView extends View {
private Paint mFillPaint, mStrokePaint;
private BitmapShader mBitmapShader;
private float posX, posY;
public BrickView(Context context, AttributeSet attrs) {
super(context, attrs);
mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
mStrokePaint.setColor(0xFF000000);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setStrokeWidth(5);
mFillPaint = new Paint();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.b);
mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mFillPaint.setShader(mBitmapShader);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.DKGRAY);
canvas.drawCircle(posX, posY, 80, mFillPaint);
canvas.drawCircle(posX, posY, 80, mStrokePaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE){
posX = event.getX();
posY = event.getY();
invalidate();
}
return true;
}
}

浙公网安备 33010602011771号