self-confidence,the source of all the power

导航

android shader 用法

 转自 http://blog.csdn.net/abcdef314159

http://blog.csdn.net/aigestudio/article/details/41799811

Shader渲染Android提供了5个子类,有BitmapShader,ComposeShader,LinearGradient,RadialGradient,SweepGradient。Shader中有一个TileMode,共有3种模式,

CLAMP:当图片小于绘制尺寸时要进行边界拉伸来填充

REPEAT:当图片小于绘制尺寸时重复平铺

MIRROR:当图片小于绘制尺寸时镜像平铺

我们先来看一下BitmapShader,

  1. public class ShaderView extends View {  
  2.     private Bitmap mBitmap;  
  3.     private Shader mBitmapShader = null;  
  4.     private Paint mPaint;  
  5.   
  6.     public ShaderView(Context context, AttributeSet attrs) {  
  7.         super(context, attrs);  
  8.         init();  
  9.     }  
  10.   
  11.     private void init() {  
  12.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  13.         mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);  
  14.         mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT,  
  15.                 Shader.TileMode.MIRROR);  
  16.     }  
  17.   
  18.     @Override  
  19.     protected void onDraw(Canvas canvas) {  
  20.         super.onDraw(canvas);  
  21.         mPaint.setShader(mBitmapShader);  
  22.         canvas.drawRect(0, 0, mBitmap.getWidth() * 4, mBitmap.getHeight() * 4,  
  23.                 mPaint);  
  24.     }  
  25.   
  26. }  

运行结果为


我们看到左右平铺,上下镜像,在来改一下,只需要把上面的mBitmapShader改一下就可

  1. mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,  
  2.         Shader.TileMode.MIRROR);  


看到上下镜像,左右拉伸,再来看一下LinearGradient

  1. public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],  
  2.            TileMode tile)  

float x0 :渐变的x坐标起点
float y0 :渐变的y坐标起点
float x1 :渐变的x坐标终点
float y1 :渐变的y坐标终点
int colors[]:渐变的颜色数组
float positions[]:颜色的相对位置
TileMode tile:上面的3种模式

  1. public class ShaderView extends View {  
  2.     private Shader mLinearGradient = null;  
  3.     private Paint mPaint;  
  4.   
  5.     public ShaderView(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.         init();  
  8.     }  
  9.   
  10.     private void init() {  
  11.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  12.         mLinearGradient = new LinearGradient(0, 0, 100, 100, new int[] {  
  13.                 Color.RED, Color.YELLOW, Color.BLACK, Color.WHITE },  
  14.                 new float[] { 0, .1F, .8F, .9F }, Shader.TileMode.REPEAT);  
  15.     }  
  16.   
  17.     @Override  
  18.     protected void onDraw(Canvas canvas) {  
  19.         super.onDraw(canvas);  
  20.         mPaint.setShader(mLinearGradient);  
  21.         canvas.drawRect(0, 0, 900, 1000, mPaint);  
  22.     }  
  23. }  


我们看到上面的小数数组是渐变的相对位置,如果positions为空,这均匀分布,修改一下

  1. mLinearGradient = new LinearGradient(0, 0, 100, 100, new int[] {  
  2.         Color.RED, Color.YELLOW, Color.BLACK, Color.WHITE }, null,  
  3.         Shader.TileMode.REPEAT);  

再来修改一下模式,因为最后一个是白色容易和手机屏幕混淆,所以添加了一个绿色

  1. mLinearGradient = new LinearGradient(0, 0, 100, 100,  
  2.         new int[] { Color.RED, Color.YELLOW, Color.BLACK, Color.WHITE,  
  3.                 Color.GREEN }, null, Shader.TileMode.CLAMP);  


我们看到最后一个颜色是拉伸,再修改一下

  1. mLinearGradient = new LinearGradient(0, 0, 100, 100,  
  2.         new int[] { Color.RED, Color.YELLOW, Color.BLACK, Color.WHITE,  
  3.                 Color.GREEN }, null, Shader.TileMode.MIRROR);  

镜像模式,所以感觉有点对称。在看一下LinearGradient的另一个构造方法,

  1. LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,  
  2.             TileMode tile)  

其中color0是颜色的起始点,color1是颜色的终止点,我们就演示一个看看

  1. mLinearGradient = new LinearGradient(0, 0, 100, 100, Color.RED,  
  2.         Color.YELLOW, Shader.TileMode.MIRROR);  


再来看一下RadialGradient

  1. RadialGradient(float x, float y, float radius,  
  2.                           int colors[], float positions[], TileMode tile)  

它实现的是环形渐变,
x:渐变的中心x坐标

y:渐变的中心y坐标

radius:渐变的半径

colors:梯度渐变的颜色数组

positions:和LinearGradient类似,用来指定颜色数组的相对位置

  1. public class ShaderView extends View {  
  2.     private Shader mRadialGradient = null;  
  3.     private Paint mPaint;  
  4.   
  5.     public ShaderView(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.         init();  
  8.     }  
  9.   
  10.     private void init() {  
  11.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  12.         mRadialGradient = new RadialGradient(200, 400, 100, new int[] {  
  13.                 Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN }, null,  
  14.                 Shader.TileMode.REPEAT);  
  15.     }  
  16.   
  17.     @Override  
  18.     protected void onDraw(Canvas canvas) {  
  19.         super.onDraw(canvas);  
  20.         mPaint.setShader(mRadialGradient);  
  21.         canvas.drawRect(0, 0, 900, 1000, mPaint);  
  22.     }  
  23. }  


运行结果如上,再来修改一下

  1. mRadialGradient = new RadialGradient(200, 400, 100, new int[] {  
  2.         Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN }, null,  
  3.         Shader.TileMode.CLAMP);  


再来修改一下

  1. mRadialGradient = new RadialGradient(200, 400, 100, new int[] {  
  2.         Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN }, null,  
  3.         Shader.TileMode.MIRROR);  


还有个构造方法,这里就不在演示

  1. RadialGradient(float x, float y, float radius,  
  2.                           int color0, int color1, TileMode tile)  

下面看一下SweepGradient

  1. SweepGradient(float cx, float cy,  
  2.                          int colors[], float positions[])  

cx,cy:扫描中心的x,y坐标

colors:渐变的数组颜色
positions:渐变的相对位置

  1. public class ShaderView extends View {  
  2.     private Shader mSweepGradient = null;  
  3.     private Paint mPaint;  
  4.   
  5.     public ShaderView(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.         init();  
  8.     }  
  9.   
  10.     private void init() {  
  11.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  12.         mSweepGradient = new SweepGradient(400, 400, new int[] { Color.YELLOW,  
  13.                 Color.RED, Color.BLUE, Color.GREEN }, new float[] { 0, .2F,  
  14.                 .6F, .9F });  
  15.     }  
  16.   
  17.     @Override  
  18.     protected void onDraw(Canvas canvas) {  
  19.         super.onDraw(canvas);  
  20.         mPaint.setShader(mSweepGradient);  
  21.         canvas.drawCircle(400, 400, 300, mPaint);  
  22.     }  
  23. }  



还有最后一个ComposeShader,这个是组合的Shader

  1. public class ShaderView extends View {  
  2.     private Shader mSweepGradient = null;  
  3.     private Shader mBitmapShader = null;  
  4.     private Shader mComposeShader = null;  
  5.     private Paint mPaint;  
  6.     private Bitmap mBitmap;  
  7.   
  8.     public ShaderView(Context context, AttributeSet attrs) {  
  9.         super(context, attrs);  
  10.         init();  
  11.     }  
  12.   
  13.     private void init() {  
  14.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  15.         mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);  
  16.         mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT,  
  17.                 Shader.TileMode.MIRROR);  
  18.         mSweepGradient = new SweepGradient(mBitmap.getWidth() * 2,  
  19.                 mBitmap.getWidth() * 2, new int[] { Color.YELLOW,  
  20.                 Color.RED, Color.BLUE, Color.GREEN }, new float[] { 0, .2F,  
  21.                 .6F, .9F });  
  22.         mComposeShader = new ComposeShader(mBitmapShader, mSweepGradient,  
  23.                 PorterDuff.Mode.DARKEN);  
  24.     }  
  25.   
  26.     @Override  
  27.     protected void onDraw(Canvas canvas) {  
  28.         super.onDraw(canvas);  
  29.         mPaint.setShader(mComposeShader);  
  30.         canvas.drawRect(0, 0, mBitmap.getWidth() * 4, mBitmap.getHeight() * 4,  
  31.                 mPaint);  
  32.     }  
  33. }  

posted on 2016-09-29 18:31  漩涡鸣人  阅读(1125)  评论(0编辑  收藏  举报