/*
* LineerGradient, RadialGradient, SweepGradient 等类是Shader的 子类,Shader类定义了一个与
* 之对应的LocalMatrix,通过这个矩阵可以实现对Shader的坐标变换。通过将shader设置到Paint中我们就可以
* 用这个画笔绘制出特殊效果的图形。本例实现了动画效果。
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private class SampleView extends View {
private Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
private float mRotate;
private Matrix mMatrix = new Matrix();
private Shader mShader;
float x = 160;
float y = 160;
public SampleView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
// 定义一个扫描类型的渐变
mShader = new SweepGradient(x, y, new int[] { Color.GREEN,
Color.RED, Color.BLUE, Color.GREEN }, null);
mPaint.setShader(mShader);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
//设置旋转矩阵
mMatrix.setRotate(mRotate, x, y);
//将旋转矩阵应用于shader
mShader.setLocalMatrix(mMatrix);
mRotate+=3;
if(mRotate>360){
mRotate=0;
}
canvas.drawCircle(x, y, 80, mPaint);
invalidate();
}
}
}
08-24
08-24