Android RotateAnimation,如何执行更多旋转?
我必须在单击按钮时旋转 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);
}
}
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
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这一行
将 0f 更改为所需的起始角度。
In this line
change 0f to the desired starting angle.