Android动画详解

来源:互联网 发布:行政转行会计 知乎 编辑:程序博客网 时间:2024/06/07 06:21

一、补间动画
(一)通过XML形式定义动画。
新建动画文件:res→new→Android resource file→Resource type选择
Animation.

1.透明度渐变(AlphaAnimation)

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/linear_interpolator">    <alpha        android:repeatMode="restart"        android:repeatCount="infinite"        android:duration="1000"        android:fromAlpha="0.0"        android:toAlpha="1.0"        /></set>

2旋转动画(RotateAnimation)

<rotate        android:fromDegrees="0"        android:toDegrees="180"        android:pivotX="50%"        android:pivotY="50%"        android:repeatMode="restart"        android:repeatCount="infinite"        android:duration="2000"/>    <!-- android:pivotX="50%"   指定轴心的X坐标-->

3.缩放动画(ScaleAnimation)

<scale        android:repeatMode="restart"        android:repeatCount="infinite"        android:duration="3000"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:toXScale="2.0"        android:toYScale="0.5"        android:pivotX="50%"        android:pivotY="50%"/>    <!--android:fromXScale="1.0" 指定动画开始时X轴的缩放系数-->    <!--android:toXScale="2.0"   指定动画结束时X轴的缩放系数-->

4.平移动画(TranslateAnimation)

<translate        android:fromXDelta="50"        android:fromYDelta="50"        android:toXDelta="200"        android:toYDelta="200"        android:repeatCount="infinite"        android:repeatMode="restart"        android:duration="5000"/>

在代码中:

 Animation ani1 = AnimationUtils.loadAnimation(this,R.anim.alpha_animation);                img01.startAnimation(ani1);

(二)、通过代码创建补间动画

//缩放    public void scale(View view) {        scale_anim = new ScaleAnimation(1f,1.5f,1f,0.5f,Animation.RELATIVE_TO_SELF,                0.5f,Animation.RELATIVE_TO_SELF,0.5f);        scale_anim.setFillAfter(true);  //设置是否保持动画后的状态//        scale_anim.setStartOffset(1000);//设置延时时间        scale_anim.setDuration(2000);        imageView.startAnimation(scale_anim);    }    //旋转    public void rotate(View view) {        rotate_anim = new RotateAnimation(0f,300f,                Animation.RELATIVE_TO_SELF,0.5f,                Animation.RELATIVE_TO_SELF,0.5f);        rotate_anim.setDuration(3000);        //设置是否保持旋转后的状态        rotate_anim.setFillAfter(true);        rotate_anim.setRepeatCount(2);        imageView.startAnimation(rotate_anim);    }    //平移    public void translate(View view) {        translate_anim = new TranslateAnimation(0f,300f,0,0);        translate_anim.setInterpolator(new BounceInterpolator());//设置插值器        translate_anim.setDuration(2000);        translate_anim.setAnimationListener(new MyAnimationListener());        imageView.startAnimation(translate_anim);    }    //渐变    public void alpha(View view) {        //fromAlpha   toAlpha        alpha_anim = new AlphaAnimation(1.0f, 0.2f);        //duration time        alpha_anim.setDuration(3000);        imageView.startAnimation(alpha_anim);    }    //动画集合    public void all(View view) {        translate_anim = new TranslateAnimation(0f,300f,0,0);//        translate_anim.setInterpolator(new BounceInterpolator());        rotate_anim = new RotateAnimation(200f,30f,                Animation.RELATIVE_TO_SELF,0.5f,                Animation.RELATIVE_TO_SELF,0.5f);        rotate_anim.setRepeatCount(2);        AnimationSet set = new AnimationSet(true);        set.addAnimation(translate_anim);        set.addAnimation(rotate_anim);        set.setDuration(3000);        set.setInterpolator(new BounceInterpolator());        imageView.startAnimation(set);    }

二、逐帧动画
新建资源文件:res→new→Android resource file→Resource type选择
Drawable,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="@mipmap/horse1"        android:duration="200" />    <item        android:drawable="@mipmap/horse2"        android:duration="200" />   ......</animation-list>

布局文件中:

 <ImageView            android:id="@+id/image_array"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/frame"/>

注意这里使用backg而非src

然后得到AnimationDrawable对象:
private AnimationDrawable ad;
ad = (AnimationDrawable) ima.getBackground();

 public void begin(View view) {//        ima.setImageResource(R.drawable.frame);//        //拿到AnimationDrawable对象//        ad = (AnimationDrawable) ima.getDrawable();        if(!ad.isRunning()){            ad.start();        }    }    public void pause(View view) {        if(ad.isRunning()){            ad.stop();        }    }

三、属性动画

//渐变    public void alpha(View view) {        ObjectAnimator animator = ObjectAnimator.ofFloat(ima,"alpha",1.0f,0.5f);        animator.setDuration(2000);        animator.start();    }    //平移    public void translate(View view) {        ObjectAnimator animator = ObjectAnimator.ofFloat(ima,"translationY",0.0f,300f);        animator.setDuration(2000);        animator.start();    }    //缩放    public void scale(View view) {        ObjectAnimator animator = ObjectAnimator.ofFloat(ima,"scaleX",1.0f,0.5f);        animator.setInterpolator(new BounceInterpolator());        animator.setDuration(2000);        animator.start();    }    //旋转    public void rotate(View view) {        ObjectAnimator animator = ObjectAnimator.ofFloat(ima,"rotationX",0.0f,720f);        animator.setInterpolator(new BounceInterpolator());        animator.setDuration(2000);        animator.start();    }    //动画集合    public void all(View view) {//        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("translationY",0.0f,300f);//        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("rotationX",0.0f,600f);//        ObjectAnimator anim_all = ObjectAnimator.ofPropertyValuesHolder(ima,holder1,holder2);//        anim_all.setInterpolator(new CycleInterpolator(0.5f)); //设置插值器//        anim_all.setDuration(1000);//        anim_all.start();        /*第二种方式*/        ObjectAnimator anim1 = ObjectAnimator.ofFloat(ima, "translateY", 0f, 300f);        ObjectAnimator anim2 = ObjectAnimator.ofFloat(ima, "rotationY", 0f, 300f);        ObjectAnimator anim3 = ObjectAnimator.ofFloat(ima, "scaleX", 1f, 0.5f);        AnimatorSet set = new AnimatorSet();//        set.playTogether(anim1,anim2,anim3);//一起执行        set.playSequentially(anim1,anim2,anim3);//按顺序执行//        set.play(anim1).after(anim2);set.play(anim2).with(anim1)...//先后顺序        set.setDuration(2000);        set.start();    }
0 0