2015-11-01 90 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

相关问题