zoukankan      html  css  js  c++  java
  • android开发三种动画的使用

    ##动画之帧动画(AnimationDrawable)###帧动画就是一张一张的切换图片,从而来实现的动画,具体的步骤如下:- 第一步
    在res-drawable的目录下创建一个xml的文件,root-element为animation-list
        
        

    <?xml version="1.0" encoding="utf-8"?>
        <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/z_1_01" android:duration="100"/>
        <item android:drawable="@drawable/z_1_02" android:duration="100"/>
        <item android:drawable="@drawable/z_1_03" android:duration="100"/>
        <item android:drawable="@drawable/z_1_04" android:duration="100"/>
        <item android:drawable="@drawable/z_1_05" android:duration="100"/>
        <item android:drawable="@drawable/z_1_06" android:duration="100"/>
        <item android:drawable="@drawable/z_1_07" android:duration="100"/>
        </animation-list>

    - 第二步

    在布局中将控件设置为imageview

    - 第三步

            //1.找到控件
            ImageView iv = (ImageView) findViewById(R.id.iv);
            //2.设置背景资源,参数为自己定义的xml文件
            iv.setBackgroundResource(R.drawable.frame);
            //3.设置背景,返回的是animationDrawable
            AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
            //4.开始动画
            anim.start();    

    ##动画之补间动画Tween Animation
    ###也称之为view动画,特点就是控件本身是没有动的,动的是图片
    - 平移translate

    public void translate(View view) {
               /*
               四个参数的:
               TranslateAnimation animation = new TranslateAnimation(
                       0, 50,//fromx tox     x从什么位置到什么位置
                       0, 50); //fromy toy     y从什么位置到什么位置*/
        
                /*
                八个参数的:
                 * One of Animation.ABSOLUTE, 相对于绝对坐标
                 * Animation.RELATIVE_TO_SELF, 相对于自己
                 *  or Animation.RELATIVE_TO_PARENT.相对于父容器
                 */
               /* TranslateAnimation animation = new TranslateAnimation
                        (Animation.ABSOLUTE, 0,
                        Animation.ABSOLUTE, 50, //x的增量
                        Animation.ABSOLUTE, 0,
                        Animation.ABSOLUTE, 0);*/
                /*TranslateAnimation animation = new TranslateAnimation
                        (Animation.RELATIVE_TO_SELF, 0,
                        Animation.RELATIVE_TO_SELF, 1.0f, //相对于自己宽度倍数
                        Animation.RELATIVE_TO_SELF, 0,
                        Animation.RELATIVE_TO_SELF, 0);*/
                TranslateAnimation animation = new TranslateAnimation
                        (Animation.RELATIVE_TO_PARENT, 0,
                        Animation.RELATIVE_TO_PARENT, 0.5f, //移动的距离相当于父容器的一半
                        Animation.RELATIVE_TO_PARENT, 0,
                        Animation.RELATIVE_TO_PARENT, 0);
        
                animation.setDuration(1000);
                animation.setRepeatMode(Animation.REVERSE);
                //重复的次数,如果是具体的值就是n+1次
                animation.setRepeatCount(Animation.INFINITE);
                mIv.startAnimation(animation);
            }

    - 旋转rotate

             //两个参数,从开始的角度到旋转的角度
            //RotateAnimation animation = new RotateAnimation(-30, 60);
            //六个参数
            RotateAnimation animation = new RotateAnimation(-30, 60,
                    Animation.RELATIVE_TO_SELF, 0.5f, //相对于自己的中心点来旋转
                    Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(1000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(Animation.INFINITE);
            mIv.startAnimation(animation);

    - 缩放scale

             public void scale(View view) {
            ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            animation.setDuration(1000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(Animation.INFINITE);
            mIv.startAnimation(animation);
               }

    - 透明度渐变alpha

            public void alpha(View view) {
            //1.0完全不透明,0.0完全透明
            AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);
            animation.setDuration(1000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(Animation.INFINITE);
            mIv.startAnimation(animation);
               }

    - 合集

       public  void set(View v){
            //第一步:创建一个合集的对象
            AnimationSet set = new AnimationSet(false);
            //第二步:设置透明度的动画
            AlphaAnimation animation1 = new AlphaAnimation(0.0f,1.0f);
            animation1.setDuration(1000);
            animation1.setRepeatMode(Animation.REVERSE);
            animation1.setRepeatCount(Animation.INFINITE);
            //第三步:设置平移的动画
            TranslateAnimation animation = new TranslateAnimation
                    (Animation.RELATIVE_TO_PARENT, 0,
                            Animation.RELATIVE_TO_PARENT, 0.5f, //移动的距离相当于父容器的一半
                            Animation.RELATIVE_TO_PARENT, 0,
                            Animation.RELATIVE_TO_PARENT, 0);
    
            animation.setDuration(1000);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setRepeatCount(Animation.INFINITE);
            //第四步:添加到合集里面去
            set.addAnimation(animation);
            set.addAnimation(animation1);
            //第五步:开始合集的动画
            mIv.startAnimation(set);
            }

    ###使用xml文件来实现动画的效果
    - 第一步:在res目录下面创建一个anim的文件夹,root element为translate或者scale等,注意androidStudio可能对一些属性提示不出来,需要自己手动去打出来

            <?xml version="1.0" encoding="utf-8"?>
            <translate xmlns:android="http://schemas.android.com/apk/res/android"
                android:fromXDelta="0"
                android:toXDelta="50"
                android:duration="2000"
                android:repeatCount="infinite"
                android:repeatMode="reverse">
            <!--toXDelta="50"  绝对像素
                toXDelta="100%" 相对于自己
                   toXDelta="50%p" 相对于父容器-->
            </translate>

    - 第二步:利用animationUtils来加载动画

    Animation animation = AnimationUtils.loadAnimation(this, R.anim
                    .translate);
            mIv.startAnimation(animation);

    ###view动画的监听
     

      private Animation.AnimationListener mAnimationListener = new Animation.AnimationListener(){
    
    
            @Override
            public void onAnimationStart(Animation animation) {
    
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束跳转到主界面
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                //动画的淡入淡出
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }


    ##动画之属性动画Property Animation
    ###属性动画的特点就是修改了控件的属性来实现的动画

     //平移
        public void translate(View view) {
            /**
             * 参数1:谁要播放这个动画
             * 参数2:执行什么动画
             * 参数3:动画数值,可写多个
             */
            //mIv.setTranslationX();
            ObjectAnimator animator = ObjectAnimator.ofFloat(mIv,
                    "translationX", 0, 20, 100);
            animator.setDuration(2000);
            animator.setRepeatCount(3);
            animator.setRepeatMode(ObjectAnimator.REVERSE);
            animator.start();
        }

        //旋转

    public void rotate(View view) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "rotation", 0, 90);
    
            animator.setDuration(2000);
    
            animator.setRepeatCount(ObjectAnimator.INFINITE);
            animator.setRepeatMode(ObjectAnimator.REVERSE);
    
            animator.start();
        }

        //缩放
     

       public void scale(View view) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "scaleX", 1, 3);
    
            animator.setDuration(2000);
    
            animator.setRepeatCount(ObjectAnimator.INFINITE);
            animator.setRepeatMode(ObjectAnimator.REVERSE);
    
            animator.start();
        }


        //透明度
        

    public void alpha(View view) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 1.0f, 0.0f);
    
            animator.setDuration(2000);
    
            animator.setRepeatCount(ObjectAnimator.INFINITE);
            animator.setRepeatMode(ObjectAnimator.REVERSE);
    
            animator.start();
        }

        //合集
       

     public void set(View v) {
            //第一步:X轴移动的动画
            ObjectAnimator animator1 = ObjectAnimator.ofFloat(mIv,
                    "translationX", 0, 20, 100);
            animator1.setDuration(2000);
            animator1.setRepeatCount(3);
            animator1.setRepeatMode(ObjectAnimator.REVERSE);
            //第二步:Y轴移动的动画
            ObjectAnimator animator = ObjectAnimator.ofFloat(mIv,
                    "translationY", 0, 20, 100);
            animator.setDuration(2000);
            animator.setRepeatCount(3);
            animator.setRepeatMode(ObjectAnimator.REVERSE);
            //第三步:创建合集的对象
            AnimatorSet set = new AnimatorSet();
            //第四步:选择播放动画的模式(顺序播放还是一起播放)
            set.playTogether(animator,animator1);
    
            //set.playSequentially(animator,animator1);
            //如果是顺序播放就不能够设置重复的属性
    
            //第五步开始动画
            set.start();
        }

    ###利用xml文件来实现属性动画
    - 第一步:在res目录下面创建一个animator的文件夹,root element为objectAnimator

    <?xml version="1.0" encoding="utf-8"?>
            <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                android:propertyName="translationX"
                android:duration="2000"
                android:valueFrom="0"
                android:valueTo="100"
                android:repeatCount="infinite"
                android:repeatMode="reverse">
            
            </objectAnimator>

    - 第二步:

     //第一步:通过AnimatorInflater.loadAnimator()得到ObjectAnimator对象
            ObjectAnimator animator = (ObjectAnimator) AnimatorInflater
                    .loadAnimator(this, R.animator.translation);
            //第二步:设置要执行动画的目标
            animator.setTarget(mIv);
            //第三步:开始动画
            animator.start();


           

  • 相关阅读:
    JS中every()和some()的用法
    Git 各指令的本质
    vue登录3D效果
    vue打包后反编译到源代码(reverse-sourcemap)
    CSS Modules
    有了 HTTP 协议,为什么还需要 Websocket?
    微信小程序的实现原理
    JS
    微信小程序的发布流程
    中国地图
  • 原文地址:https://www.cnblogs.com/xxc0505/p/6501254.html
Copyright © 2011-2022 走看看