I have to rotate an ImageView on a Button click. At the first click it have to rotate to right, at the second to left etc.
The problem is that when I try to rotate for second time the "just rotated" image, the rotation start from original point and not from "post first rotation" point.
I need to rotate the image resulting from previous rotation. Below I past the code.
public class Rotate extends Activity {
boolean mDirRight = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rotate);
final ImageView imageArray = (ImageView) findViewById(R.id.ImageViewArray);
imageArray.setImageResource(R.drawable.array01);
imageArray.setAdjustViewBounds(true);
final Button btnRotate = (Button) findViewById (R.id.ButtonRotate);
btnRotate.setOnClickListener(new OnClickListener() {
@Override
pu开发者_如何学JAVAblic void onClick(View v) {
doRotation();
}
});
}
private void doRotation(){
final int rotationRight = 30;
final int rotationLeft = -20;
final RotateAnimation rAnim;
int degree;
if (mDirRight) {
degree = rotationRight;
mDirRight = false;
} else {
degree = rotationLeft;
mDirRight = true;
}
final ImageView image = (ImageView) findViewById(R.id.ImageViewArray);
rAnim = new RotateAnimation(0f, degree, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rAnim.setStartOffset(0);
rAnim.setDuration(2000);
rAnim.setFillAfter(true);
rAnim.setFillEnabled(true);
image.startAnimation(rAnim);
}
}
In this line
rAnim = new RotateAnimation(0f, degree, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
change 0f to the desired starting angle.
精彩评论