单击按钮时动画不会第二次应用
Animation not apply second time on button click
我有一个问题,我的动画没有在按钮单击时第二次应用。我用过 this library.I 想用动画旋转图像。但问题是它只能工作一次。
这是我点击按钮时的代码
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewAnimator.animate(imageView)
.rotation(rotationAngle).duration(1000)
.repeatMode(ValueAnimator.RESTART)
.start();
if(rotationAngle==360)
{
rotationAngle=180;
}
else
{
rotationAngle += 180;
}
}
});
我测试了这段代码,它似乎工作正常。如果您希望始终顺时针旋转图像,请尝试下一步:
rotationAngle 初始值
private float rotationAngle=179.99f;
.99f - 因为 360 度是 0 度,你需要更少的旋转来同向旋转
然后设置onClickListener:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewAnimator.animate(imageView)
.rotation(rotationAngle).duration(1000)
.repeatMode(ValueAnimator.RESTART)
.start();
if(rotationAngle>=360)
{
rotationAngle=179.99f;
}
else
{
rotationAngle += 180;
}
}
});
使用下面的代码工作正常,我也测试过
public class MainActivity extends AppCompatActivity {
private int rotationAngle = 180;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv_testing);
findViewById(R.id.btn_click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewAnimator.animate(tv)
.rotation(rotationAngle).duration(1000)
.repeatMode(ValueAnimator.RESTART)
.start();
if(rotationAngle==360)
{
rotationAngle=180;
}
else
{
rotationAngle += 180;
}
}
});
}
}
希望对你也有帮助
你得到这个是因为你使用了
ViewAnimator.animate(imageView)
.rotation(rotationAngle).duration(1000)
你应该使用
ViewAnimator.animate(imageView).rotationBy(rotationAngle)....
它只工作一次的原因是如果你使用旋转,它会旋转到自 activity 创建以来的旋转度数(你的情况是 180)而不是当前位置。
我有一个问题,我的动画没有在按钮单击时第二次应用。我用过 this library.I 想用动画旋转图像。但问题是它只能工作一次。
这是我点击按钮时的代码
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewAnimator.animate(imageView)
.rotation(rotationAngle).duration(1000)
.repeatMode(ValueAnimator.RESTART)
.start();
if(rotationAngle==360)
{
rotationAngle=180;
}
else
{
rotationAngle += 180;
}
}
});
我测试了这段代码,它似乎工作正常。如果您希望始终顺时针旋转图像,请尝试下一步: rotationAngle 初始值
private float rotationAngle=179.99f;
.99f - 因为 360 度是 0 度,你需要更少的旋转来同向旋转
然后设置onClickListener:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewAnimator.animate(imageView)
.rotation(rotationAngle).duration(1000)
.repeatMode(ValueAnimator.RESTART)
.start();
if(rotationAngle>=360)
{
rotationAngle=179.99f;
}
else
{
rotationAngle += 180;
}
}
});
使用下面的代码工作正常,我也测试过
public class MainActivity extends AppCompatActivity {
private int rotationAngle = 180;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv_testing);
findViewById(R.id.btn_click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewAnimator.animate(tv)
.rotation(rotationAngle).duration(1000)
.repeatMode(ValueAnimator.RESTART)
.start();
if(rotationAngle==360)
{
rotationAngle=180;
}
else
{
rotationAngle += 180;
}
}
});
}
}
希望对你也有帮助
你得到这个是因为你使用了
ViewAnimator.animate(imageView)
.rotation(rotationAngle).duration(1000)
你应该使用
ViewAnimator.animate(imageView).rotationBy(rotationAngle)....
它只工作一次的原因是如果你使用旋转,它会旋转到自 activity 创建以来的旋转度数(你的情况是 180)而不是当前位置。