利用RotateAnimation旋转一个图片,当动画停止时,如何让被旋转的图片保持旋转结束时的状态,而不是回到初始图片状态。
更详细一点说就是,我有一张图片,利用RotateAnimation旋转,转了180度的时候停止动画,结果这张图片又直接变回初始的状态而不是倒置的,这个问题要怎样解决,求大牛指点
更详细一点说就是,我有一张图片,利用RotateAnimation旋转,转了180度的时候停止动画,结果这张图片又直接变回初始的状态而不是倒置的,这个问题要怎样解决,求大牛指点
调试欢乐多
package buptsse.wood.android;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;public class TouchAndMove extends Activity implements OnTouchListener,AnimationListener {
/** Called when the activity is first created. */ RotateAnimation rAnimation;
float x1,x2,x;
Bitmap mBitmap;
Matrix mMatrix;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView im = (ImageView) findViewById(R.id.disk);
im.setOnTouchListener(this);
im.setLongClickable(true);
mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.disk)).getBitmap();
mMatrix = new Matrix();
x = 0;
} @Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1=event.getX();
break;
case MotionEvent.ACTION_MOVE:
x2=event.getX();
rotate(x,x2-x1);
break;
case MotionEvent.ACTION_UP:
x+=(x2-x1); //x来存储图像旋转以后的总共的偏移角度 } return false;
} public void rotate(float rawX,float x) {
rAnimation = new RotateAnimation(rawX, rawX+x, Animation.RELATIVE_TO_SELF,
(float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
rAnimation.setDuration(500);
rAnimation.setRepeatCount(0);
rAnimation.setInterpolator(new LinearInterpolator());
findViewById(R.id.disk).startAnimation(rAnimation);
} @Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
mMatrix.setRotate(x);
Canvas canvas = new Canvas();
canvas.drawBitmap(mBitmap,mMatrix, null);
} @Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
} @Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
应该是要在onAnimationEnd里面写些东西吧,不过我试着如上写了一下,没有什么作用,应该是我没写对,不知道matrix该怎么用。求指点
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
ImageView im = (ImageView) findViewById(R.id.disk);
Matrix matrix = im.getImageMatrix(matrix);
matrix.postRotate(180);
im.setImageMatrix(matrix);
}
自己看API才知道在RotateAnimation里面有一个方法叫fillafter(),将其值设为true就可以将动画结束时的图象生效,很简单很好用
rAnimation.setAnimatinoListener(new AnimationListener{});
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Random rn = new Random();
int xuanCount = rn.nextInt(3600) + 1800;
xuanCount = xuanCount + qiCount;
System.out.println("开始前qiCount:" + qiCount + "xuanCount:"
+ xuanCount);
mRotateAnimation = new RotateAnimation(qiCount, xuanCount,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mRotateAnimation.setFillAfter(true);
mRotateAnimation.setFillBefore(false);
// 第一个参数fromDegrees为动画起始时的旋转角度
// 第二个参数toDegrees 为动画旋转到的角度
// 第三个参数pivotXType 为动画在X 轴相对于物件位置类型
// 第四个参数pivotXValue 为动画相对于物件的X 坐标的开始位置
// 第五个参数pivotXType 为动画在Y 轴相对于物件位置类型
// 第六个参数pivotYValue 为动画相对于物件的Y 坐标的开始位置
mRotateAnimation.setDuration(5000);
myYuan.startAnimation(mRotateAnimation);
qiCount = xuanCount % 360;
System.out.println("结束后qiCount:" + qiCount + "xuanCount:"
+ xuanCount);
return super.onTouchEvent(event);
}
return true;
}勉强解决问题····试试看吧
animation.setFillAfter(true);图片的位置处于旋转后的位置了