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

 


       

转载于:https://www.cnblogs.com/xxc0505/p/6501254.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值