2015-11-01 23 views
0

如何將動畫添加到android應用程序中的普通按鈕,我希望按鈕從活動按鈕浮動到頂部,然後消失。如何將動畫添加到現有的UI組件?

我已經看過動畫庫,但在我看來,它是用於與導入到項目中的外部動畫一起使用!

非常感謝!

回答

1

試試這個:

button.animate().translationY(value).setListener(new Animator.AnimatorListener() { 
     @Override 
     public void onAnimationStart(Animator animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animator animation) { 
      button.setVisibility(View.GONE); 
     } 

     @Override 
     public void onAnimationCancel(Animator animation) { 
     } 

     @Override 
     public void onAnimationRepeat(Animator animation) { 
     } 
    }).start(); 

您可能需要一些計算的 「價值」,或用距離使用translationYBy()。

+0

謝謝,我會考慮它,我想我需要設置組件將在我自己旅行的路徑?你認爲這應該是正確的嗎? – Aboudi

+0

不太明白你的意思。你的路線不是直線嗎? –

+0

是的,這是一條直線! – Aboudi

0

水庫fodlder創建新的文件夾名稱anim創建新xml文件存在與slide_up.xml,並把這個代碼,它

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true" > 

    <scale 
     android:duration="500" 
     android:fromXScale="1.0" 
     android:fromYScale="1.0" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:toXScale="1.0" 
     android:toYScale="0.0" /> 

</set> 

然後加載動畫這樣

 Animation animSlideUp; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_fadein); 

      txtMessage = (TextView) findViewById(R.id.txtMessage); 
      btnStart = (Button) findViewById(R.id.btnStart); 

      // load the animation 
      animSlideUp = AnimationUtils.loadAnimation(getApplicationContext(), 
        R.anim.slide_up); 

      // set animation listener 
      animSlideUp.setAnimationListener(this); 

      // button click event 
      btnStart.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        txtMessage.setVisibility(View.VISIBLE); 

        // start the animation 
        txtMessage.startAnimation(animSlideUp); 
       } 
      }); 

     } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     // Take any action after completing the animation 

     // check for fade in animation 
     if (animation == animSlideUp) { 
      Toast.makeText(getApplicationContext(), "Animation Stopped", 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 

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

    } 

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

    } 
+0

太棒了!我猜slide_up函數將允許組件浮動到頂部?非常感謝 – Aboudi

相關問題