Android动画的基本使用
2016-05-05 22:01
465 查看
1 帧动画
1 简介
从其名字就可知道,帧动画是由一些列的图片按顺序播放出来,但是比较容易引起OOM,所以要避免使用过多尺寸较大的图片。2 使用
不同于View动画,帧动画使用的是AnimationDrawable类。res/drawable包
XML定义动画:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <!--false表示不止执行一次;true表示只执行一次--> <item android:drawable="@drawable/cowboy" android:duration="500"/> <item android:drawable="@mipmap/ic_launcher" android:duration="500"/> <item android:drawable="@drawable/cowboy" android:duration="500"/> </animation-list>
启动动画:
mImageView.setBackgroundResource(R.drawable.frame_animation); AnimationDrawable drawable= (AnimationDrawable) mImageView.getBackground(); drawable.start();
2 补间动画
1 分类
也叫View动画,主要有AlphaAnimation、 ScaleAnimation、 TranslateAnimation、 RotateAnimation四种,代码实现上也都差不多。1 AlphaAnimation
AlphaAnimation aa = new AlphaAnimation(0, 1); aa.setDuration(2000L); mImageView.startAnimation(aa)
2 TranslateAnimation
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 200);//参数分别为fromX,toX,fromY,toY ta.setDuration(2000L); mImageView.startAnimation(au);
3 RotateAnimation
//左上角为旋转中心 RotateAnimation ra = new RotateAnimation(0, 360, 100, 100); //view的中心为旋转中心 RotateAnimation raa = new RotateAnimation(0, 180, RotateAnimation.RELATIVE_TO_SELF, 0.5F, RotateAnimation.RELATIVE_TO_SELF, 0.5F); raa.setDuration(2000L); mImageView.startAnimation(raa);
4 ScaleAnimation
//左上角缩放起点 ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2); //view的中心为缩放起点 ScaleAnimation saa = new ScaleAnimation(0, 2, 0, 2, ScaleAnimation.RELATIVE_TO_SELF , 0.5F, ScaleAnimation.RELATIVE_TO_SELF, 0.5F); saa.setDuration(2000L); mImageView.startAnimation(saa);
2 XML补间动画
res/anim包<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"> <!--true 表示动画结束后停留在结束的位置,反之则不停留--> <!--duration属性需要手动添加,不会自动提示,当然也可以在引用的时候设置它。同时这种方式可以编写多个动画效果,但它们是同时启动的。--> <translate android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200" android:toYDelta="200"/> <scale android:duration="2000" android:fromXScale="0.5" android:fromYScale="0.5" android:toXScale="2.0" android:toYScale="2.0"/> </set>
XML动画的引用:
Animation au = AnimationUtils.loadAnimation(this, R.anim.tarnslation_x_anim); mImageView.startAnimation(au);
3 动画集
方式一:AnimationSet as = new AnimationSet(true);//true表示共享插值器 as.setDuration(2000L); as.addAnimation(aa); as.addAnimation(ta); mImageView.startAnimation(as);
方式二就是前面的
xml形式。
4 自定义View动画
1 简介
自定义View动画主要涉及到数学上的矩阵变换,有时候还需要借助 Camera来简化矩阵变换。然而在实际开发中使用的较少,大体了解下其原理步骤即可。2 步骤
继承Animation抽象类;重写initialize和 applyTransformation方法;
在initialize中做一些初始化的工作(例如如果使用Camera,可在此处进行实例化);在 applyTransformation中进行矩阵变换。
3 属性动画
1 基本用法
//属性:translateX,translateY,scaleX,scaleY ObjectAnimator oa = ObjectAnimator.ofFloat(mImageView, "translationX",300F);// 到300 oa.setDuration(2000L); oa.start(); ObjectAnimator oa2 = ObjectAnimator.ofFloat(mImageView, "scaleX", 2); oa2.setDuration(1000L); oa2.start();
2 XML属性动画
res/animator包单动画:
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:propertyName="translationY"//注意此处不同于补间动画的translateY android:valueFrom="0" android:valueTo="400" android:valueType="floatType"> </objectAnimator>
多动画按顺序或者同时启动:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"><!--together同時啓動--> <objectAnimator android:duration="2000" android:propertyName="translationY" android:valueFrom="0" android:valueTo="400" android:valueType="floatType"/> <objectAnimator android:duration="2000" android:propertyName="translationx" android:valueFrom="0" android:valueTo="400" android:valueType="floatType"/> </set>
XML动画的引用:
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.translation_y_animator);//加载文件 animator.setTarget(mImageView);//关联view animator.start();
3 动画集
AnimatorSet aSet = new AnimatorSet(); //aSet.play(oa);//只启动一个动画 //aSet.playTogether(oa, oa2);//同时启动多个动画 //aSet.playSequentially(oa,oa2);//先后依次启动 //aSet.play(oa).after(oa2);//可以设置同时启动,先于、后于启动 //aSet.play(oa).after(3000L);//after可以设置启动时间 aSet.play(oa).after(3000L).after(oa2);//先启动oa2,然后经过3000L-oa2消耗的时间,再启动oa aSet.start();
还可以通过新增的API实现动画集,但是不能设置先后顺序;通过XML方式可以实现先后顺序。
4 Android3.0新增API
Builder的设计模式。// 动画都是同步执行的 mImageView.animate().setDuration(2000L).y(400).alpha(0.5F).start(); //绕X轴旋转,没设置动画结束后停止 mImageView.animate().setDuration(2000L).rotationX(360F).scaleX(2) .withStartAction(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "动画开始了", Toast.LENGTH_SHORT).show(); } }) .withEndAction(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "动画结束", Toast .LENGTH_SHORT).show(); } }); } }) .start(); //再次启动会停止之前的,也就是可以重复启动该动画 //mImageView.animate().rotationXBy(360).setDuration(3000).start();//360表示360度,一圈 //mImageView.animate().yBy(50).start(); //mImageView.animate().zBy(150).start();//垂直桌面,API要求21.
4 对比
补间动画用的是Animation,而属性动画用的是 Animator。AS中,动画资源包,帧动画是 res/drawable, View动画是 res/anim,属性是 res/animator。
补间动画的启动都是 view.startAnimation(XXX);属性动画一般采用 XXX.start(),动画集则采用 XXX.play(XXX)启动。
xml动画文件中,补间动画的 duration需要手动添加,属性动画有提示。
相关文章推荐
- Android 强制弹出输入法后 隐藏不掉或者错乱的问题
- Android去標題時報錯:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendan
- [Android]获得屏幕分辨率的两种方法20160505
- SwipeListView滑动删除Android
- android游戏开发5-5 实现背景图的沿x轴移动
- android的消息机制之学习笔记
- 高德地图No implementation found for long com.autonavi.amap.mapcore.MapCore
- android 关于Button在StateListDrawable 中使用AnimationDrawable的问题
- android studio 学习笔记之 非法字符: '\ufeff'问题解决
- ScrollView属性fillViewport解决android布局不能撑满全屏的问题
- 第11章 Android的线程和线程池
- Android Q&A | setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView....
- 学习android笔记(3):自定义动画
- 学习android笔记(1):隐藏系统自定义titleBar
- 移动开发生态渐成
- Android ----SQLiteDate(Cursor接口)
- 阿里无线的雄心壮志
- Android项目构成之AndroidManifest.xml文件简介
- android之ListView分页加载数据
- 服务android