帧动画
顾名思义,就是将一张一张的图片,连续快速的播放就变成动画了,就像小时候露天看的胶片电影一样.
步骤:一在drawable目录下建一个xml文件 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/g1" android:duration="200" />
<item android:drawable="@drawable/g2" android:duration="200" />
<item android:drawable="@drawable/g3" android:duration="200" />
<item android:drawable="@drawable/g4" android:duration="200" />
<item android:drawable="@drawable/g5" android:duration="200" />
<item android:drawable="@drawable/g6" android:duration="300" />
<item android:drawable="@drawable/g7" android:duration="300" />
<item android:drawable="@drawable/g8" android:duration="300" />
<item android:drawable="@drawable/g9" android:duration="200" />
<item android:drawable="@drawable/g10" android:duration="200" />
<item android:drawable="@drawable/g11" android:duration="200" />
</animation-list>
其中,drawable对应一张张的图片,duration表示每张图片呈现的时间.oneshot设置为true时表示只循环一次
步骤二:java代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView iv = (ImageView) findViewById(R.id.iv); //把帧动画的资源文件指定为iv的背景 iv.setBackgroundResource(R.drawable.frameanimation); //获取iv的背景 AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); ad.start(); }
补间动画
组件由原始状态向终极状态转变时,为了让过渡更自然,而自动生成的动画. Android目前支持的补间动画有RotateAnimation(旋转)、ScaleAnimation(缩放)、AlphaAnimation(透明度)、TranslateAnimation(位移)、AnimationSet(组合)这5种。补间动画有个概念叫做插值器.还是从小时候看电影说起, 有的时候我们会发现有些电影带子,播放的时候很快,像快进一样.这是因为播放的速率比一般的速率要快了.这种播放速率的改变在安卓中动画中有个专有名词, Interpolator 中文翻译叫做插值器.主要用来改变安卓动画的变化速率.常用的插值器有以下几LinearInterpolator(匀速)、AccelerateInterpolator(加速)AccelerateDecelerateInterpolator(先加速再减速)、BounceInterpolator(反弹数次后停止)、DecelerateInterpolator(减速)使用方法:animation.setInterpolator(new LinearInterpolator());//设置一个匀速的插值器
1.旋转动画:2.缩放动画public void rotate(View v){ /* * RotateAnimation:fromDegrees、toDegrees表示开始、结束的角度(0度为水平方向右侧的开始角度), * pivotXValue、pivotYValue代表旋转的中心位置,[0.0f-1.0f], * pivotXType、pivotYType表示旋转的类型(Animation.ABSOLUTE,、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT) * 当type为Animation.ABSOLUTE时,这个个值为具体的像素值,当type为Animation.RELATIVE_TO_SELF 或 Animation.RELATIVE_TO_PARENT * 这个个值为比例值,取值范围是[0f, 1.0f] */ ra = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); iv.startAnimation(ra); }
3.透明动画public void scale(View v){ /** * 构造方法如下 * fromX、toX 开始结束的X轴缩放比率[0.0f-1.0f], * fromY、toYtoY开始结束的Y轴缩放比率[0.0f-1.0f], * pivotXValue、pivotYValue代表旋转的中心位置,[0.0f-1.0f], * pivotXType、pivotYType表示旋转的类型 * (Animation.ABSOLUTE,、 * Animation.RELATIVE_TO_SELF、 * Animation.RELATIVE_TO_PARENT) * 当type为Animation.ABSOLUTE时,这个个值为具体的像素值,当type为Animation.RELATIVE_TO_SELF或Animation.RELATIVE_TO_PARENT,这个个值为比例值,取值范围是[0f, 1.0f] * */ // sa = new ScaleAnimation(fromX, toX, fromY, toY, iv.getWidth() / 2, iv.getHeight() / 2); sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); //填充动画的结束位置 sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); sa.setFillAfter(true); iv.startAnimation(sa); }
4.位移动画public void alpha(View v){ aa = new AlphaAnimation(0, 1); aa.setDuration(2000); sa.setRepeatCount(1);//-1表示无限循环 iv.startAnimation(aa); }
5.动画集合:就是一个动画集合,可以将多个动画添加到一起,让这些动画一起运动.public void translate(View v){ /** * 构造方法如下 * fromXType、toXType、fromYType、toYType(Animation.ABSOLUTE,、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT) * 当type为Animation.ABSOLUTE时,这个个值为具体的像素值,当type为Animation.RELATIVE_TO_SELF或Animation.RELATIVE_TO_PARENT, * 这个个值为比例值,取值范围是[0f, 1.0f] * */ // ta = new TranslateAnimation(10, 100, 20, 200); ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 1.5f); //设置播放时间 ta.setDuration(2000); //设置重复次数 ta.setRepeatCount(1); ta.setRepeatMode(Animation.REVERSE); iv.startAnimation(ta); }
补充:安卓的补间动画不光可以在代码中书写,还可以在xml文件中配置.在res文件下新建anim文件夹,然后新建xml文件,就可以出现如下图public void fly(View v){ AnimationSet set = new AnimationSet(false);//参数表示,是否使用同一个插值器 set.addAnimation(ta); set.addAnimation(sa); set.addAnimation(ra); set.addAnimation(aa); iv.startAnimation(set); }
![]()
属性动画
* 补间动画,只是一个动画效果,组件其实还在原来的位置上,xy没有改变
* 属性动画,真正改变了元素的属性,比如控件的大小
1.位移动画:* 第一个参数target指定要显示动画的组件 * 第二个参数propertyName指定要改变组件的哪个属性 * 第三个参数values是可变参数,就是赋予属性的新的值 * 传入0,代表x起始坐标:当前x + 0 * 传入100,代表x终点坐标:当前x + 100 //具有get、set方法的成员变量就称为属性 ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;
public void translate(View v){ // TranslateAnimation ta = new TranslateAnimation(0, 150, 0, 0); // ta.setDuration(2000); // ta.setFillAfter(true); // iv.startAnimation(ta); //target:动画作用于哪个组件 ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100); oa.setDuration(2000); oa.setRepeatCount(1); oa.setRepeatMode(ValueAnimator.REVERSE); oa.start(); }
2.缩放动画public void scale(View v){ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2); oa.setDuration(2000); oa.start(); }
3.透明动画4.旋转动画public void alpha(View v){ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.6f, 0.2f, 1); oa.setDuration(2000); oa.start(); }
5.动画集合public void rotate(View v){ ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY", 0, 180, 90, 360); oa.setDuration(2000); oa.setRepeatCount(1); oa.setRepeatMode(ValueAnimator.REVERSE); oa.start(); }
补充:属性动画的xml使用方式:public void fly(View v){ AnimatorSet set = new AnimatorSet(); ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100); oa1.setDuration(2000); oa1.setRepeatCount(1); oa1.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY", 10, 70, 20, 100); oa2.setDuration(2000); oa2.setRepeatCount(1); oa2.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2); oa3.setDuration(2000); oa3.setRepeatCount(1); oa3.setRepeatMode(ValueAnimator.REVERSE); ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360); oa4.setDuration(2000); oa4.setRepeatCount(1); oa4.setRepeatMode(ValueAnimator.REVERSE); //设置挨个飞 // set.playSequentially(oa1, oa2, oa3, oa4); //设置一起飞 set.playTogether(oa1, oa2, oa3, oa4); set.start(); }
1.在res/animator文件夹下新建xml文件,使用ObjectAnimator2.在java代码中使用<objectAnimator android:propertyName="translationX" android:duration="200" android:repeatCount="1" android:repeatMode="reverse" android:valueFrom="-100" android:valueTo="100" > </objectAnimator>
好了,关于安卓动画的介绍暂时先到这里了public void xml(View v){ Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator); //设置作用于哪个组件 at.setTarget(iv); at.start(); }