android揭露动画

2020-10-26  本文已影响0人  仍旧热忱_3619

android揭露动画

  1. MainActivity中修改

    public class MainActivity extends Activity {     
     private static final int MODE_IN = 0x1;
        private static final int MODE_OUT = 0x2;
        private LinearLayout mCircularRevealView;//用于展示圆形揭示层的布局
        @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            Button button = findViewById(R.id.button);
            //点击按钮开启动画
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    createCircularRevealAnim(MODE_IN);
                }
            });
    
        }
        /**
         * 创建圆形揭示层动画
         */
        private void createCircularRevealAnim(int mode) {
            //设置圆心坐标和半径
            int mCx = (mCircularRevealView.getLeft() + mCircularRevealView.getRight()) / 2;//获取x坐标
            int mCy = (mCircularRevealView.getTop() + mCircularRevealView.getBottom()) / 2;//获取y坐标
            //设置圆角半径
            int mRadius = Math.max(mCircularRevealView.getWidth() / 2, mCircularRevealView.getHeight() / 2);
            Animator anim;//声明一个动画
            if (mode == MODE_IN) {
                //揭露动画创建,五个参数
                //param1:执行动画的视图;param2:动画开始的x坐标;param3:动画开始的y坐标;param4:动画开始的圆角半径;param5:动画结束的圆角半径
                anim = ViewAnimationUtils.createCircularReveal(mCircularRevealView, mCx, mCy, 0, mRadius);
            } else {
                anim = ViewAnimationUtils.createCircularReveal(mCircularRevealView, mCx, mCy, mRadius, 0);
            }
            //添加监听器来保证开始动画之前,布局不会显示,也可以添加动画退出监听,让布局隐藏
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    super.onAnimationStart(animation);
                    Log.d(TAG , "动画开始");
                    mCircularRevealView.setVisibility(View.VISIBLE);
                }
            });
            anim.setDuration(500);//设置动画时长
            anim.start();//开启动画
    
        }
     //初始化视图实例
        private void initView() {
            mCircularRevealView = findViewById(R.id.circular_reveal_view);
        }
    }
    
  2. 修改布局文件activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
     <!--揭露布局-->
        <LinearLayout
            android:id="@+id/circular_reveal_view"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#333"
            android:visibility="invisible"
            android:orientation="vertical">
        </LinearLayout>
        
        <!--触发按钮-->
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="button"
            />
    </LinearLayout>
    

总结:

主要方法:

创建揭露动画:

ViewAnimationUtils.createCircularReveal(
     View view, int centerX,  int centerY, float startRadius, float endRadius
);

监听动画:

Animator anim;

 anim.addListener(new AnimatorListenerAdapter() {
            //动画开始时调用
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
            
            //动画结束时调用
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });

主要方法就一个

private void createCircularRevealAnim(int mode) {
        //设置圆心坐标和半径
        int mCx = (mCircularRevealView.getLeft() + mCircularRevealView.getRight()) / 2;
        int mCy = (mCircularRevealView.getTop() + mCircularRevealView.getBottom()) / 2;
        int mRadius = Math.max(mCircularRevealView.getWidth() / 2, mCircularRevealView.getHeight() / 2);
        
        Animator anim;
        if (mode == MODE_IN) {
            anim = ViewAnimationUtils.createCircularReveal(mCircularRevealView, mCx, mCy, 0, mRadius);
        } else {
            anim = ViewAnimationUtils.createCircularReveal(mCircularRevealView, mCx, mCy, mRadius, 0);
        }
        
        anim.addListener(new AnimatorListenerAdapter() {
            //动画开始时调用
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mCircularRevealView.setVisibility(View.VISIBLE);
            }

            //动画结束时调用
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
        
        anim.setDuration(500);
        anim.start();
}
上一篇下一篇

猜你喜欢

热点阅读