利用RotateAnimation旋转一个图片,当动画停止时,如何让被旋转的图片保持旋转结束时的状态,而不是回到初始图片状态。
更详细一点说就是,我有一张图片,利用RotateAnimation旋转,转了180度的时候停止动画,结果这张图片又直接变回初始的状态而不是倒置的,这个问题要怎样解决,求大牛指点 如果说知道初始的状态,以及每次旋转的步进值,那么就可以在onAnimationEnd的时候,计算或者获得图片旋转的角度,然后用matrix将图片定在那边就好了。
把代码粘上来好了,按照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该怎么用。。。。求指点 下面的代码没有测试过,大概这么个意思。
@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); } |
|