我正在尝试使用以下代码旋转部分放置在屏幕上的图像:
final float ROTATE_FROM = 0.0f;
final float ROTATE_TO = 360.0f;
RotateAnimation r = new RotateAnimation(ROTATE_FROM, ROTATE_TO,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
r.setDuration(300000);
r.setRepeatCount(Animation.INFINITE);
r.setInterpolator(this, android.R.anim.linear_interpolator);
imv = (ImageView) findViewById(R.id.imageView1);
imv.startAnimation(r);
但我得到的是图像旋转,部分是屏幕外部留空的部分。
结果是什么:
a demopic http://www.11sheep.com/temp/p1.png
有人可以给我一个代码段吗?
答案 0 :(得分:1)
这是代码
Animation anim =new FlipAnim(0, 90, card.getWidth()/2, card.getHeight()/2);
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
//
Animation anim2 =new FlipAnim(-90, 0, card.getWidth()/2, card.getHeight()/2);
card.startAnimation(anim2);
}
});
card.startAnimation(anim);
这是代码 FlipAnim
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class FlipAnim extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private Camera mCamera;
public FlipAnim(float fromDegrees, float toDegrees,
float centerX, float centerY) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
setDuration(400);
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}