Android 中 画图与动画旋转效果(中级)
先看效果 如图 太阳自传 绿方框围着太阳转 传说中的自传与公转
上代码
public class save extends View {
Paint paint;
float f=0;
Matrix mMatrix;
Bitmap bitmap,nbitmap;
public save(Context context) {
super(context);
paint=new Paint();
mMatrix=new Matrix();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(5);
//得到太阳位图
bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.a00)).getBitmap();
//启动线程来刷新旋转角度
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while(! Thread.currentThread().isInterrupted()){
try {
f+=1;
Thread.sleep(300);
// System.out.println(f);
} catch (InterruptedException e) {
Thread.currentThread().interrupted();
}
postInvalidate();//回调ondraw()
}
}
}.start();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setColor(Color.GREEN);
canvas.drawColor(Color.WHITE);
canvas.clipRect(new Rect(10,10,1000,1000));
canvas.save();//画布绑定一个精灵
canvas.rotate(f);//画布旋转
canvas.drawRect(300, 300, 450, 450, paint);//绿方框精灵
canvas.restore();//释放这个精灵
mMatrix.reset();
mMatrix.setRotate(f);
canvas.translate(100, 100);
canvas.drawBitmap(bitmap, mMatrix, null);//画出带旋转矩阵的bitmap
}
}