【问题标题】:How implement and use switch when have Animation有动画时如何实现和使用开关
【发布时间】:2018-01-14 11:09:55
【问题描述】:

这是我的活动:

public class MainActivity extends Activity implements Animation.AnimationListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    animation1 = AnimationUtils.loadAnimation(this,R.anim.to_middle);
    animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
    animation2.setAnimationListener(this);
    animation1.setAnimationListener(this);
}
@Override
public void onAnimationEnd(Animation animation) {

    if (animation == animation1) {
        if (isBackOfCardShowing) {
            imageview1.setImageResource(R.drawable.dog);
        } else {
            imageview1.setImageResource(R.drawable.card);
        }
        imageview1.clearAnimation();

    } else {
        isBackOfCardShowing=!isBackOfCardShowing;
    }

我的项目中有动画并在我的活动中实现AnimationListener。我如何在onAnimationEnd 方法中使用开关?并将这段代码放在第一种情况下?

if (animation == animation1) {
            if (isBackOfCardShowing) {
                imageview1.setImageResource(R.drawable.dog);
            } else {
                imageview1.setImageResource(R.drawable.card);
            }
             imageview1.clearAnimation();

        } else {
            isBackOfCardShowing=!isBackOfCardShowing;
        }

我在互联网上搜索,但没有找到有关此的教程

【问题讨论】:

    标签: android animation switch-statement implements


    【解决方案1】:

    switch 语句只能是 int。 您可以创建动画的枚举并执行

    public enum ANIM {ANIMATION0,ANIMATION1,ANIMATION2
    //ANIMATION3,4,5...
    }
    
    public void onAnimationEnd(Animation animation) {
        int ANIM = getANIM(animation);
        switch(ANIM) {
           case ANIMATION0:
                break;
           case ANIMATION1:
                if (isBackOfCardShowing) {
                    imageview1.setImageResource(R.drawable.dog);
                } else {
                    imageview1.setImageResource(R.drawable.card);
                }
                imageview1.clearAnimation();
    
                } else {
                isBackOfCardShowing=!isBackOfCardShowing;
                }
                break;
           case ANIMATION2:
                break;
           //ANIMATION3,4,5...
    
        }
    }
    public int getANIM(Animation animation) {
        Animation animation0 = whatever0;
        Animation animation1 = whatever1;
        Animation animation2 = whatever2;
        if (animation == animation0) {
            return 0;
        } else if (animation == animation1) {
            return 1;
        } else if (animation == animation2) {
            return 2;
        }
        //else if ANIMATION3,4,5...
    }
    

    【讨论】:

    • 当我写这行“int ANIM = getANIM(Animation,animation)”android studio 显示这条消息“表达式预期”
    • 感谢您的帮助,如果一个放 int 动画是 object 并且 android studio 显示此消息“Required :int”
    • 否,因为“case ANIMATION1:”这一行有错误
    • 第一行有没有:public enum ANIM {ANIMATION0,ANIMATION1,ANIMATION2}
    • 您的枚举声明与您已经做出的另一个声明正在崩溃。请像我一样把它变成大写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    相关资源
    最近更新 更多