0

嗨,动画有问题,实际上我想在 ImageView 更改资源时淡化它。它不起作用,我可以更改资源但没有任何动画。

private void checkX() {
    // TODO Auto-generated method stub
    switch (x) {
    case (1): {
        startingAnimation();
        xImg.setImageResource(R.drawable.test_uno);
        endingAnimation();
        break;
    }
    case (2): {
        startingAnimation();
        xImg.setImageResource(R.drawable.test_due);
        endingAnimation();
        break;
    }
    case (3): {
        startingAnimation();
        xImg.setImageResource(R.drawable.test_tre);
        endingAnimation();
        break;
    }
    case (4): {
        startingAnimation();
        xImg.setImageResource(R.drawable.test_quattro);
        endingAnimation();
        break;
    }
    case (5): {
        x = 1;
        checkX();
        break;
    }
    case (0): {
        x = 4;
        checkX();
        break;
    }
    }
}


private void endingAnimation() {
    // TODO Auto-generated method stub
    ObjectAnimator animation2 = ObjectAnimator.ofFloat(xImg, "alpha", 255);
    animation2.setDuration(1000);
    animation2.start();

}

private void startingAnimation() {
    // TODO Auto-generated method stub
    ObjectAnimator animation = ObjectAnimator.ofFloat(xImg, "alpha", 0);
    animation.setDuration(1000);
    animation.start();
}

每次我按下一个按钮(在 X 上加或减 1)时都会调用 checkX。

我如何使动画和 ImageView 资源都发生变化?

4

2 回答 2

0

在调用动画的onAnimationEnd时尝试更改图像源。例如:

animation.addListener(new AnimatorListener() {

    @Override
    public void onAnimationStart(Animator animation) { }

    @Override
    public void onAnimationRepeat(Animator animation) { }

    @Override
    public void onAnimationEnd(Animator animation) {
        // change your ImageView's source here
    }

    @Override
    public void onAnimationCancel(Animator animation) { }
});
于 2013-03-28T11:31:30.947 回答
0

在 ImageView 上应用淡出动画并将 animationListener 设置为它。在动画结束时更改您的视图资源并在其上设置淡入动画

用于淡入动画

Animation fadeIn = new AlphaAnimation(0, 1);
            fadeIn.setInterpolator(new DecelerateInterpolator()); 
            fadeIn.setDuration(1000);
            final AnimationSet animation = new AnimationSet(false); 
            animation.addAnimation(fadeIn);

对于淡出动画

Animation fadeOut = new AlphaAnimation(1, 0);
                fadeOut.setInterpolator(new AccelerateInterpolator()); 
                fadeOut.setDuration(300);
                final AnimationSet animation = new AnimationSet(false);
                animation.addAnimation(fadeOut);

这就是你如何在动画上实现监听器

fadeout.setAnimationListener(new Animation.AnimationListener(){

            @Override
            public void onAnimationStart(Animation arg0) {
            }           

            @Override 
            public void onAnimationRepeat(Animation arg0) {
            }           

            @Override
            public void onAnimationEnd(Animation arg0) {
                //change resource of Imageview here             
            }
        });
于 2013-03-28T11:33:55.273 回答