gpt4 book ai didi

android - 如何每次将动画旋转 90 度?

转载 作者:行者123 更新时间:2023-11-30 01:06:53 35 4
gpt4 key购买 nike

我想在收到点击时设置旋转 90 度的动画。第一次点击工作正常。但是从第二次点击开始,它不起作用:它从第二次点击开始旋转 180 度。

RotateAnimation anim = new RotateAnimation(0, 45, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);

anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(0);
anim.setFillAfter(true);
anim.setDuration(200);

btnPlus.startAnimation(anim);

最佳答案

试试这个

public class MainActivity extends Activity {

private ImageView iv;
private Button btn;
float startDegress =-90;
float endDegress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
iv = (ImageView) findViewById(R.id.iv);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startDegress+=90;
endDegress+=90;
RotateAnimation anim = new RotateAnimation(startDegress, endDegress, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);

anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(0);
anim.setFillAfter(true);
anim.setDuration(200);

iv.startAnimation(anim);
}
});
}
}

另一种方式
公共(public)类 MainActivity 扩展 Activity {

private ImageView iv;
private Button btn;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
iv = (ImageView) findViewById(R.id.iv);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(i>3){
i=i%3;
}
ObjectAnimator oa2= ObjectAnimator.ofFloat(iv, "rotation", i*90,(i+1)*90);
oa2.setDuration(1000);
oa2.start();
i++;
}
});
}

关于android - 如何每次将动画旋转 90 度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38811298/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com