android简单动画


    public class MainActivity extends Activity {
        private ImageView iv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv = (ImageView) findViewById(R.id.iv);
        }

        /**
         * 透明度渐变的动画
         * 
         * @param view
         */
        public void alpha(View view) {
            AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
            // 动画播放的时间长度
            aa.setDuration(2000);
            // 设置重复播放的次数
            aa.setRepeatCount(Animation.INFINITE);
            // 设置重复播放的模式
            aa.setRepeatMode(Animation.REVERSE);
            // 让iv播放aa动画
            iv.startAnimation(aa);
        }

        /**
         * 平移动画
         * 
         * @param view
         */
        public void trans(View view) {
            TranslateAnimation ta = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f,
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f);
            // 动画播放的时间长度
            ta.setDuration(2000);
            // 设置重复播放的次数
            ta.setRepeatCount(Animation.INFINITE);
            // 设置重复播放的模式
            ta.setRepeatMode(Animation.REVERSE);
            // 让iv播放aa动画
            iv.startAnimation(ta);
        }

        /**
         * 缩放动画
         */
        public void scale(View view) {
            ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            // 动画播放的时间长度
            sa.setDuration(2000);
            // 设置重复播放的次数
            sa.setRepeatCount(Animation.INFINITE);
            // 设置重复播放的模式
            sa.setRepeatMode(Animation.REVERSE);
            // 让iv播放aa动画
            iv.startAnimation(sa);
        }

        /**
         * 旋转动画
         */
        public void rotate(View view) {
            RotateAnimation ra = new RotateAnimation(0, 360,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            // 动画播放的时间长度
            ra.setDuration(2000);
            // 设置重复播放的次数
            ra.setRepeatCount(Animation.INFINITE);
            // 设置重复播放的模式
            ra.setRepeatMode(Animation.REVERSE);
            // 让iv播放aa动画
            iv.startAnimation(ra);
        }

        /**
         * 动画合集 集合
         */
        public void set(View view) {
            AnimationSet set = new AnimationSet(false);
            RotateAnimation ra = new RotateAnimation(0, 360,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            // 动画播放的时间长度
            ra.setDuration(2000);
            // 设置重复播放的次数
            ra.setRepeatCount(Animation.INFINITE);
            // 设置重复播放的模式
            ra.setRepeatMode(Animation.REVERSE);
            ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            // 动画播放的时间长度
            sa.setDuration(2000);
            // 设置重复播放的次数
            sa.setRepeatCount(Animation.INFINITE);
            // 设置重复播放的模式
            sa.setRepeatMode(Animation.REVERSE);
            // 让iv播放aa动画
            TranslateAnimation ta = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f,
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1f);
            // 动画播放的时间长度
            ta.setDuration(2000);
            // 设置重复播放的次数
            ta.setRepeatCount(Animation.INFINITE);
            // 设置重复播放的模式
            ta.setRepeatMode(Animation.REVERSE);
            set.addAnimation(ta);
            set.addAnimation(sa);
            set.addAnimation(ra);
            iv.startAnimation(set);
        }

    }

xml写法

  public class MainActivity extends Activity {
        private ImageView iv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv = (ImageView) findViewById(R.id.iv);
        }
        /**
         * 透明度渐变的动画
         * @param view
         */
        public void alpha(View view){
            Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
            iv.startAnimation(aa);
        }

        /**
         * 平移动画
         * @param view
         */
        public void trans(View view){
            Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);
            iv.startAnimation(ta);
        }
        /**
         * 缩放动画
         */
        public void scale(View view){
            Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
            iv.startAnimation(sa);
        }
        /**
         * 旋转动画
         */
        public void rotate(View view){
            Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
            iv.startAnimation(ra);
        }
        /**
         * 动画合集 集合
         */
        public void set(View view){
            Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
            iv.startAnimation(set);
        }

    }

透明渐变

<?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="2000"
        android:repeatCount="infinite"
        android:repeatMode="reverse" >
    </alpha>

平移

    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="0"
        android:toXDelta="100%"
        android:fromYDelta="0"
        android:toYDelta="100%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:duration="2000" >

    </translate>

旋转

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        android:repeatCount="2"
        android:repeatMode="reverse" >

    </rotate>

缩放


    <?xml version="1.0" encoding="utf-8"?>
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="20%"
        android:toXScale="200%"
        android:fromYScale="20%"
        android:toYScale="200%"
        android:duration="2000"
        android:repeatCount="2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatMode="reverse" >

    </scale>

集合

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true" >

        <rotate
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="2000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="2"
            android:repeatMode="reverse"
            android:toDegrees="360" >
        </rotate>

        <scale
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="2000"
            android:fromXScale="20%"
            android:fromYScale="20%"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="2"
            android:repeatMode="reverse"
            android:toXScale="200%"
            android:toYScale="200%" >
        </scale>

    </set>

属性动画

     ImageView iv = (ImageView) findViewById(R.id.iv);
            iv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "被点击了", 0).show();
                }
            });

            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationY", new float[]{0,2,4,6,8,10,15,20,25,30,50,80,100,150});
            oa.setDuration(2000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.RESTART);
            oa.start();
   public class MainActivity extends Activity {
        private ImageView iv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv = (ImageView) findViewById(R.id.iv);
        }

        /**
         * 透明度渐变的动画
         * 
         * @param view
         */
        public void alpha(View view) {
            // iv.setAlpha(alpha);
            // iv.getAlpha();
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", new float[] {
                    0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f });
            oa.setDuration(3000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.REVERSE);
            oa.start();
        }

        /**
         * 平移动画
         * 
         * @param view
         */
        public void trans(View view) {
            // iv.setTranslationX(translationX);
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
                    new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
            oa.setDuration(3000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.REVERSE);
            oa.start();
        }

        /**
         * 缩放动画
         */
        public void scale(View view) {
            // iv.setScaleX(scaleX)
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", new float[] {
                    1f, 2f, 3f, 4f, 5f, 6f });
            oa.setDuration(3000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.REVERSE);
            oa.start();
        }

        /**
         * 旋转动画
         */
        public void rotate(View view) {
            // iv.setRotation(rotation);
            // iv.getRotation()
            // iv.setRotationX(rotationX);
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY",
                    new float[] { 90f, 180f, 270f, 360f });
            oa.setDuration(3000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.REVERSE);
            oa.start();
        }

        /**
         * 动画合集 集合
         */
        public void set(View view) {
            AnimatorSet set = new AnimatorSet();
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
                    new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
            oa.setDuration(3000);
            ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY",
                    new float[] { -10f, -20f, -30f, -40f, -60f, -80f });
            oa2.setDuration(3000);
            set.playTogether(oa, oa2);
            set.start();
        }
    }

xml方式

    public class MainActivity extends Activity {
        private ImageView iv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv = (ImageView) findViewById(R.id.iv);
        }

        /**
         * 透明度渐变的动画
         * 
         * @param view
         */
        public void alpha(View view) {
            Animator animator = AnimatorInflater.loadAnimator(this,
                    R.animator.alpha);
            animator.setTarget(iv);
            animator.start();
        }

        /**
         * 平移动画
         * 
         * @param view
         */
        public void trans(View view) {  
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
                    new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
            oa.setDuration(3000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.REVERSE);
            oa.start();
        }

        /**
         * 缩放动画
         */
        public void scale(View view) {
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", new float[] {
                    1f, 2f, 3f, 4f, 5f, 6f });
            oa.setDuration(3000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.REVERSE);
            oa.start();
        }

        /**
         * 旋转动画
         */
        public void rotate(View view) {
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY",
                    new float[] { 90f, 180f, 270f, 360f });
            oa.setDuration(3000);
            oa.setRepeatCount(ObjectAnimator.INFINITE);
            oa.setRepeatMode(ObjectAnimator.REVERSE);
            oa.start();
        }

        /**
         * 动画合集 集合
         */
        public void set(View view) {
            AnimatorSet set = new AnimatorSet();
            ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX",
                    new float[] { 10f, 20f, 30f, 40f, 60f, 80f });
            oa.setDuration(3000);
            ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY",
                    new float[] { -10f, -20f, -30f, -40f, -60f, -80f });
            oa2.setDuration(3000);
            set.playTogether(oa, oa2);
            set.start();
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:propertyName="alpha"
        android:repeatCount="3"
        android:repeatMode="reverse"
        android:valueFrom="0.0"
        android:valueTo="1.0" >

    </objectAnimator>

推荐阅读更多精彩内容

  • 一直不愿意承认,其实我有一个非常严重的毛病,那就是:懒惰。 熟悉我的人就会说,我看你很勤快呀, 经常跑步锻炼身体;...
    老黄爱分享阅读 36评论 0 0
  • 昨天早早地就去图书馆学习了 但是一直在玩手机 刷微博 看热点 浪费了我一个早上美好的时间 可能因为我比较八卦 一直...
    小小小grow阅读 24评论 0 0
  • 四川的夏天 总是这样 艳阳高照 烈日炎炎 突入其来的狂风暴雨也让你渐渐习惯 每当暴雨来临 路上总是人们匆匆奔跑的身...
    J1Ang阅读 11评论 0 0
  • 从事产业园区招商、孵化器运营两年多时间,基本小白一枚,不过今天也斗胆聊聊孵化器的话题,希望能够与大家探讨学习。 自...
    石帅阅读 4,473评论 2 5