ShaderView.java:
public class ShaderView extends View { private Paint paint; public ShaderView(Context context, AttributeSet attrs) { super(context, attrs); } public ShaderView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStrokeWidth(5); paint.setTextSize(100); this.setLayerType(View.LAYER_TYPE_SOFTWARE,paint);//设置为SOFTWARE才会实现阴影 paint.setShadowLayer(10,1,1, Color.BLUE);//偏移度很小时则变成发光字体 canvas.drawText("Android开发",100,100,paint); canvas.restore(); paint.setShadowLayer(10,5,5,Color.GREEN);//如果想要取消阴影,则将Radius设为0即可 canvas.drawText("Android,你好",100,400,paint); } }
渐变效果代码:
public class RadialGradientView extends View { private int color[]={Color.GREEN,Color.BLUE,Color.RED}; //private float position[]={0,0.5f,1}; public RadialGradientView(Context context, AttributeSet attrs) { super(context, attrs); } public RadialGradientView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect=new Rect(100,100,600,600); //1.径向渐变 RadialGradient r=new RadialGradient(350,350,250, Color.BLUE,Color.YELLOW, Shader.TileMode.MIRROR); //2.线性渐变 // LinearGradient r=new LinearGradient(100,100,600,600,Color.RED,Color.GREEN, Shader.TileMode.CLAMP); //3.扫描渐变 // SweepGradient r=new SweepGradient(350,350,Color.RED,Color.GREEN); //SweepGradient r=new SweepGradient(350,350,color,null); //4.位图渐变 // Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.boy); // BitmapShader r=new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR); //5.混合渐变 // LinearGradient lg=new LinearGradient(100,100,600,600,Color.RED,Color.GREEN, Shader.TileMode.CLAMP); // Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.boy); // BitmapShader bs=new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR); // ComposeShader r=new ComposeShader(bs,lg, PorterDuff.Mode.SRC_ATOP); //公用部分 Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG); paint.setShader(r); canvas.drawRect(rect,paint); canvas.translate(0,500); canvas.drawOval(new RectF(rect),paint); } }渐变与Matrix
我们做一个旋转的圆, 圆内使用 SweepGradient 渐变填充,看起来像一张光盘。首先, 我们创建了一个 Matrix 对象 mMatrix, mMatrix 定义了以圆点为中心渐变的旋转效
果, 注意不是旋转 Canvas 而是旋转 SweepGradient。 onDraw()方法中不断调用 invalidate()重绘自己, 每重绘一次就旋转 2 度,于是就形成了一个旋转的动画。
SweepMatrixView.java:
public class SweepMatrixView extends View { private Paint mpaint=new Paint(Paint.ANTI_ALIAS_FLAG); private float mRotate; private Matrix matrix=new Matrix(); private Shader mshader; private int[] color={ Color.GREEN,Color.RED,Color.BLUE}; public SweepMatrixView(Context context, AttributeSet attrs) { super(context, attrs); setFocusable(true); setFocusableInTouchMode(true); float x=100; float y=100; mshader=new SweepGradient(x,y,color,null); mpaint.setShader(mshader); } public SweepMatrixView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint =mpaint; float x=100; float y=100; canvas.translate(100,100); canvas.drawColor(Color.WHITE); matrix.setRotate(mRotate,x,y); mshader.setLocalMatrix(matrix); mRotate+=2; if(mRotate>=360){ mRotate=0; } invalidate(); canvas.drawCircle(x,y,100,paint); } }可以实现扫描渐变像光盘一样旋转。