繁体   English   中英

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

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

如何将动画添加到android应用程序中的普通按钮,我希望该按钮从活动的按钮向顶部浮动,然后消失。

我已经看过动画库,但是在我看来,它打算与导入到项目中的外部动画一起使用!

非常感谢!

尝试这个:

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

在您的资源库中,创建一个名称为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>

然后像这样加载动画

        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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM