如何使用动画移动和淡出任何视图

6

好的,我有一个视图,使用以下代码进行动画处理:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.productPage_parentLayout_RL);

ImageView iv = new ImageView( ProductPage.this );
imageLoader.DisplayImage(FULL_PRODUCT_INFO.getFirstImage(), iv);

Animation animation = new TranslateAnimation(0, helper.getDeviceWidth() - 100,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);

rl.addView(iv);

使用这段代码,视图从屏幕的左侧开始移动到接近屏幕的末尾,但我还想在最后淡出视图并隐藏/销毁它。我试着搜索任何相关的内容,但找不到任何信息。感谢您提供的帮助。
2个回答

10

尝试这个

ObjectAnimator move=ObjectAnimator.ofFloat(iv, "translationY",100f);
                    move.setDuration(3000);
    ObjectAnimator alpha1=ObjectAnimator.ofFloat(iv, "alpha",0.5f);
                    alpha1.setDuration(1000);

                    ObjectAnimator alpha2=ObjectAnimator.ofFloat(iv, "alpha",0);
                    alpha2.setDuration(2000);
    AnimatorSet animset=new AnimatorSet();
                    animset.play(alpha2).before(alpha1).with(move);
                    animset.start();

1
ObjectAnimator是针对API 11及更高版本的。我需要从API 8开始使用它。 - Tolga Demir

4
    Animation a = new AlphaAnimation(1.0f, 0.0f);
        a.setDuration(1000);
        a.setFillAfter(true);
        iv.startAnimation(a);

AnimationSet set = new AnimationSet(true);
        set.addAnimation(animation)
        set.addAnimation(a)
        set.setFillAfter(true);
        iv.startAnimation(set);

    set.setAnimationListener(new AnimationListener() {

                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    public void onAnimationEnd(Animation animation) {
                        iv.setVisibility(View.INVISIBLE);

                    }
                });

几乎...我也使用了这种方法,但它需要在移动时淡出并在到达末尾时隐藏。 - Tolga Demir
在这种情况下,您需要使用AnimationSet。 - Eddy K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接