动画在Android中图像的旋转
-
我有,我要不断地围绕一个固定点齿轮的形象。
I have a gear image which I want to continuously rotate about a fixed point.
此前我被包括在我的Android类作为一个ImageView的图像和应用RotateAnimation它实现这一点。
Earlier I was accomplishing this by including the image in my Android class as an ImageView and applying a RotateAnimation to it.
@InjectView(R.id.gear00) ImageView gear00;
RotateAnimation ra07 = new RotateAnimation(0, 359, 129, 186);
ra07.setDuration(10000);
ra07.setRepeatCount(RotateAnimation.INFINITE);
ra07.setInterpolator(new LinearInterpolator());
gear00.setAnimation(ra07);
基本上,我被注射了ImageView的进级和应用旋转动画。
Basically, I was injecting the ImageView into the class and applying a rotation animation.
不过,我没有使用的ImageView了的奢侈品。我必须使用的位图的并旋转画布上。
However, I dont have the luxury of using an ImageView anymore. I have to use a Bitmap and rotate it on the canvas.
我该如何去完成我在以前是做的的OnDraw()的方法,用位图在画布上绕一固定点continiously?
How can I go about accomplishing what I was doing earlier in the onDraw() method with a bitmap rotating about a fixed point continiously on the canvas?
EDIT1:
我试过了我下面的code中提到的建议,一个看起来像一个小以下
I tried one of the suggestions mentioned below my code looks a little like the following
在的onCreate():
in onCreate():
Matrix matrix = new Matrix();
matrix.setRotate(10, 100, 200);
然后在的OnDraw()(其中gear00Scaled是位图,在画布上旋转):
Then in onDraw() (where gear00Scaled is a bitmap to be rotated on the canvas):
canvas.drawBitmap(gear00Scaled,矩阵,新的油漆());
canvas.drawBitmap(gear00Scaled, matrix, new Paint());
另一种方法我试过参与节约画布,旋转,然后还原它:
Another method I tried involved saving the canvas, rotating it, then restoring it:
canvas.save();
canvas.rotate(10);
canvas.drawBitmap(gear00Scaled,100,200,NULL);
canvas.restore();
canvas.save();
canvas.rotate(10);
canvas.drawBitmap(gear00Scaled, 100, 200, null);
canvas.restore();
既不似乎工作虽然!
在您的onCreate()做
In your onCreate() do
Matrix matrix = new Matrix();
而在OnDraw中
And in onDraw
float angle = (float) (System.currentTimeMillis() % ROTATE_TIME_MILLIS)
/ ROTATE_TIME_MILLIS * 360;
matrix.reset();
matrix.postTranslate(-source.getWidth() / 2, -source.getHeight() / 2);
matrix.postRotate(angle);
matrix.postTranslate(centerX, centerY)
canvas.drawBitmap(source, matrix, null);
invalidate(); // Cause a re-draw
ROTATE_TIME_MILLIS是完整的圆时,如2000是2秒。
ROTATE_TIME_MILLIS is the full circle time, e.g. 2000 is 2 seconds.