读书人

圆球旋转Anim(主要学习点Matrix知识)

发布时间: 2012-06-30 17:20:13 作者: rapoo

球体旋转Anim(主要学习点Matrix知识)
这点Code主要对View重写进行球体旋转:
知识点:
1.重写View
2.将Drawable资源转化为Bitmap

直接上代码(便于以后查看使用):
Java代码
class MyView extends View {
private Bitmap bitmap1;
private Bitmap bitmap2;
private int digree1 = 0;
private int digree2 = 360;

public MyView(Context context) {
super(context);
setBackgroundColor(Color.WHITE);
InputStream is = getResources().openRawResource(R.drawable.cross);
bitmap1 = BitmapFactory.decodeStream(is);
is = getResources().openRawResource(R.drawable.ball);
bitmap2 = BitmapFactory.decodeStream(is);
}

@Override
protected void onDraw(Canvas canvas) {
Matrix matrix = new Matrix();
if (digree1 > 360)
digree1 = 0;
if (digree2 < 0)
digree2 = 360;
matrix.setRotate(digree1++, 160, 240);
canvas.setMatrix(matrix);
canvas.drawBitmap(bitmap1, 88, 169, null);
matrix.setRotate(digree2--, 160, 240);
canvas.setMatrix(matrix);
canvas.drawBitmap(bitmap2, 35, 115, null);
invalidate();
}

}

读书人网 >移动开发

热点推荐