简体   繁体   English

如何将动画添加到现有的UI组件?

[英]How can i add animations to existing UI components?

How would Be able to add animations to normal buttons within an android application, I want the button to float from the button of the activity towards the top and then disappear. 如何将动画添加到android应用程序中的普通按钮,我希望该按钮从活动的按钮向顶部浮动,然后消失。

I have looked into the animations library but it seems to me that it is intended for use with external animation that are imported into the project! 我已经看过动画库,但是在我看来,它打算与导入到项目中的外部动画一起使用!

Many thanks! 非常感谢!

Try this: 尝试这个:

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

You might need some calculation for the "value", or use translationYBy() with a distance. 您可能需要对“值”进行一些计算,或者使用带有一定距离的translationYBy()。

in you res fodlder create new folder with name anim create new xml file there with slide_up.xml and put this code to it 在您的资源库中,创建一个名称为anim新文件夹,并在其中使用slide_up.xml创建新的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>

then load animation like this 然后像这样加载动画

        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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swing 组件中显示 Flash 动画? - how I can display Flash animations in Swing components? 如何更改特定类型的所有组件的UI? - How can I change the UI of all components of a certain type? 如何将 MVVM 与 App/activity 和 AsyncTask 的 UI 组件一起使用 - How can I use MVVM with the UI components of the App/activity and AsyncTask 使用NetBeans UI设计器,如何从一个项目复制UI组件并将其粘贴到另一个项目中? - Using NetBeans UI designer, how can I copy UI components from one project and paste them into another? 如何向NetBeans添加新的Java组件? - How can I add new Java components to NetBeans? 我怎么做几个动画1后1 - how can i do few animations 1 after 1 如何在运行时将组件添加到使用Netbeans可视编辑器创建的Swing UI中? - How do I add components at run time to a Swing UI created with Netbeans visual editor? 如何在Java中将方法添加到现有对象? - How can I add method to an existing object in Java? 如何将信息添加到arraylist中的现有元素? - How can I add an information to an existing element in an arraylist? 如何在现有按钮和按钮之间添加其他LinearLayout? - How can I add an additional LinearLayout between an existing one and a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM