2013-01-02 34 views
4

好吧任何视图,所以我有一个观点在那里它被用下面的代码动画:如何移动和淡出与动画

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
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); 

        } 
       }); 
+0

差不多......我也使用过这种方法,但它在移动时需要淡出,并在到达末尾时隐藏起来。 –

+0

在这种情况下,您需要使用AnimationSet –

6

试试这个

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(); 
+0

ObjectAnimator适用于API 11及更高版本。我需要从API 8开始使用它。 –