利用RotateAnimation旋转一个图片,当动画停止时,如何让被旋转的图片保持旋转结束时的状态,而不是回到初始图片状态。
更详细一点说就是,我有一张图片,利用RotateAnimation旋转,转了180度的时候停止动画,结果这张图片又直接变回初始的状态而不是倒置的,这个问题要怎样解决,求大牛指点

解决方案 »

  1.   

    如果说知道初始的状态,以及每次旋转的步进值,那么就可以在onAnimationEnd的时候,计算或者获得图片旋转的角度,然后用matrix将图片定在那边就好了。
      

  2.   

    特别是怎样用matrix将图片定在那里,旋转的角度我已经知道了
      

  3.   

    把代码粘上来好了,按照5楼的办法来做
    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该怎么用。求指点
      

  4.   

    下面的代码没有测试过,大概这么个意思。@Override
    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);
    }
      

  5.   

    解决了,上面的几种办法试来试去效果都不好
    自己看API才知道在RotateAnimation里面有一个方法叫fillafter(),将其值设为true就可以将动画结束时的图象生效,很简单很好用
      

  6.   

    恭喜你,发现CSDN越来越水了,没有能人解决.没有人愿意花时间.
      

  7.   

    setFillAfter(true)就会停留在动画最后一帧的效果了
      

  8.   

    不要这么用animationlistener,要在你的那个动画上加
    rAnimation.setAnimatinoListener(new AnimationListener{});
      

  9.   

    setFillAfter(true) 没用啊 ,没有停
      

  10.   

    @Override
    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;
    }勉强解决问题····试试看吧
      

  11.   

    animation.setFillBefore(true);
    animation.setFillAfter(true);图片的位置处于旋转后的位置了
      

  12.   

    animation.setFillAfter(true); 这个函数可以设置动画结束之后维持最后的位置!