我必须在按钮上单击旋转ImageView。在第一次点击时,它必须向右旋转,在第二个到左边等。
问题在于,当我尝试第二次旋转“刚刚旋转”的图像时,旋转从原始点开始而不是从“第一次旋转”点开始。
我需要旋转之前旋转产生的图像。下面是我的代码。
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
public 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);
}
}
答案 0 :(得分:2)
在这一行
rAnim = new RotateAnimation(0f,degree, RotateAnimation.RELATIVE_TO_SELF,0.5f, RotateAnimation.RELATIVE_TO_SELF,0.5f);
将0f改为所需的起始角度。