英文:
How to repeat Android animation in java?
问题
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = ((ImageView) findViewById(R.id.imageView));
iv.setRotationY(0f);
//iv.animate().setDuration(10);
iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
iv.animate().rotationY(360f).setListener(null);
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
}
英文:
i have this code here. I want that the Imageview do the animation 5 times. How i need to set the setRepeatCount? How to inilize it?
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = ((ImageView) findViewById(R.id.imageView));
iv.setRotationY(0f);
//iv.animate().setDuration(10);
iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
iv.animate().rotationY(360f).setListener(null);
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
答案1
得分: 3
以下是您要翻译的内容:
方法setRepeatCount属于Animation类,您正在使用Animator。
尝试这样做:
int times = 5;
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = findViewById(R.id.imageView);
iv.setRotationY(0f);
//iv.animate().setDuration(10);
final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
viewPropertyAnimator.rotationY(90f);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
times--;
if (times > 0) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
viewPropertyAnimator.rotationY(360f);
viewPropertyAnimator.start(); //重新开始
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
viewPropertyAnimator.start(); //初始化
}
[1]: https://developer.android.com/reference/android/view/animation/Animation
[2]: https://developer.android.com/reference/android/animation/Animator
英文:
The method setRepeatCount belongs to Animation class, you are using Animator.
Try this:
int times = 5;
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = findViewById(R.id.imageView);
iv.setRotationY(0f);
//iv.animate().setDuration(10);
final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
viewPropertyAnimator.rotationY(90f);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
times--;
if (times > 0) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
viewPropertyAnimator.rotationY(360f);
viewPropertyAnimator.start(); //Restart
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
viewPropertyAnimator.start(); //Init
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论