转载:http://blog.csdn.net/zhy_cheng/article/details/7950957
Android动画之初步(一)
Android Tweened Animation一共有四种
Alpha: 淡入淡出效果
Scale: 缩放效果
Rotate: 旋转效果
Translate:移动效果
使用Tweened Animations的步骤
1.创建一个AnimationSet对象
2.根据需要创建需要的Animation对象
3.根据软件动画的需要,为Animation对象设置相应的数据
4.将Animation对象添加到AnimationSet对象中
5.使控件对象开始执行AnimationSet
Alpha动画
- AnimationSet as=new AnimationSet(true);
- AlphaAnimation al=new AlphaAnimation(1,0);
- //1代表完全不透明,0代表完全透明
- al.setDuration(3000);
- as.addAnimation(al);
- iv.startAnimation(as);
Rotate动画
- AnimationSet as=new AnimationSet(true);
- RotateAnimation al=new RotateAnimation (0,720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
- //前两个参数参数旋转的角度,后面几个参数决定旋转的中心
- //Animation.ABSOLUTE:绝对坐标
- //Animation.RELATIVE_TO_PARENT:相对父控件
- //Animation.RELATIVE_TO_SELF:相对自己
- al.setDuration(3000);
- as.addAnimation(al);
- iv.startAnimation(as);
Scale动画
- AnimationSet as=new AnimationSet(true);
- ScaleAnimation al=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
- //前四个参数是X从多大到多大,Y从多大到多大,后面的参数是缩放的中心点
- al.setDuration(3000);
- as.addAnimation(al);
- iv.startAnimation(as);
Translate动画
- AnimationSet as=new AnimationSet(true);
- TranslateAnimation al=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2);
- al.setDuration(3000);
- as.addAnimation(al);
- iv.startAnimation(as);
Animation还有几个方法
setFillAfter(boolean fillAfter)
如果fillAfter的值为真的话,动画结束后,控件停留在执行后的状态
setFillBefore(boolean fillBefore)
如果fillBefore的值为真的话,动画结束后,控件停留在动画开始的状态
setStartOffset(long startOffset)
设置动画控件执行动画之前等待的时间
setRepeatCount(int repeatCount)
设置动画重复执行的次数
Animation也可以放在XML文件中,这样程序的可维护性提高了。在XML中写动画的步骤如下
1.在res文件夹下面新建一个名为anim的文件夹
2.创建xml文件,并首先加入set标签,改标签如下
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
</set>
3.在该标签当中加入rotate,alpha,scale或者translate标签
4.在代码当中使用AnimationUtils加载xml文件,并生成Animation对象
Alpha动画
- <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.0"
- android:startOffset="500"
- android:duration="2000"
- />
- </set></span>
Animation a=AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(a);
Scale动画
- <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
- <scale
- android:fromXScale="1.0"
- android:toXScale="0.0"
- android:fromYScale="1.0"
- android:toYScale="0.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="2000"
- />
- </set></span>
Rotate动画
- <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
- <rotate
- android:fromDegrees="0"
- android:toDegrees="400"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000"
- />
- </set></span>
Translate动画
- <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
- <translate
- android:fromXDelta="50%"
- android:toXDelta="100%"
- android:fromYDelta="50%"
- android:toYDelta="100%"
- android:duration="3000"
- />
- </set></span>
这里重点提一下android:pivotX和android:pivotY和android:fromXDelta,android:toXDelta
android:pivotX="50"使用绝对坐标
android:pivotX="50%"相对自己
android:pivotX="50%p"相对父控件
Android动画之Interpolator和AnimationSet(三)
AnimationSet可以加入Animation,加入之后设置AnimationSet对加入的所有Animation都有效。
- AnimationSet anim=new AnimationSet(true);
- AlphaAnimation a=new AlphaAnimation(1,0);
- RotateAnimation ra=new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
- anim.addAnimation(a);
- anim.addAnimation(ra);
- anim.setDuration(3000);
- anim.setStartOffset(1000);
- iv.startAnimation(anim);
可以再xml文件中定义多个Animation,这样多个Animation可以一起运行
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:shareInterpolator="true"
- >
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.0"
- android:startOffset="500"
- android:duration="3000"
- />
- <rotate
- android:fromDegrees="0"
- android:toDegrees="400"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000"
- />
- </set>
Interpolator可以定义动画播放的速度
在xml文件中定义Interpolator
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
这样所有的Animation共用一个Interpolator。
在代码中用代码设置如下
anim.setInterpolator(new AccelerateInterpolator());
在new一个AnimationSet中传入true则所有的Animation共用Interpolator。
现在使ImageView中的图片可以动起来
1.在drawable-mdpi文件夹下加入图片,并加入一个xml文件,文件如下
- <?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:drawable="@drawable/a" android:duration="500"/>
- <item android:drawable="@drawable/b" android:duration="500"/>
- <item android:drawable="@drawable/c" android:duration="500"/>
- <item android:drawable="@drawable/d" android:duration="500"/>
- </animation-list>
2.代码如下
- iv.setBackgroundResource(R.drawable.anim);
- AnimationDrawable an=(AnimationDrawable)iv.getBackground();
- an.start();
其实可以用一个线程加Handler来实现动画的,在线程中隔一定时间发送消息,更改ImageView的图片。
Android动画之LayoutAnimationController(五)
LayoutAnimationController可以控制一组控件按照规定显示,有两种方法来实现
1.下面以XML文件实现,先在res下新建anim文件夹,新建一个文件alpha.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:shareInterpolator="true"
- >
- <alpha
- android:fromAlpha="0"
- android:toAlpha="1"
- android:duration="3000"
- />
- </set>
然后新建一个文件layoutanimation.xml
- <?xml version="1.0" encoding="utf-8"?>
- <layoutAnimation
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:delay="0.5"
- android:animationOrder="normal"
- android:animation="@anim/alpha"
- />
在listview中使用下面代码
- <ListView
- android:id="@+id/listView1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layoutAnimation="@anim/layoutanimation"/>
这样就完成了
2.代码实现
- AlphaAnimation alpha=new AlphaAnimation(0, 1);
- alpha.setDuration(3000);
- LayoutAnimationController lac=new LayoutAnimationController(alpha);
- lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
- lv.setLayoutAnimation(lac);
下面是显示的顺序
LayoutAnimationController.ORDER_NORMAL; //顺序显示
LayoutAnimationController.ORDER_REVERSE;//反显示
LayoutAnimationController.ORDER_RANDOM//随机显示
Android动画之AnimationListener(六)
通过AnimationListener可以监听Animation的运行过程
- AnimationSet as=new AnimationSet(true);
- RotateAnimation al=new RotateAnimation(0,-720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
- al.setDuration(3000);
- al.setAnimationListener(new AnimationListener(){
- public void onAnimationStart(Animation animation) {
- // TODO Auto-generated method stub
- }
- public void onAnimationEnd(Animation animation) {
- // TODO Auto-generated method stub
- }
- public void onAnimationRepeat(Animation animation) {
- // TODO Auto-generated method stub
- }
- });
- as.addAnimation(al);
- iv.startAnimation(as);
有三个方法分别是Animation开始的时候调用,完成的时候调用,重复的时候调用。
相关推荐
可以实时滑动实现菜单侧滑,图标根据滑动距离旋转,dialog 可以自定义弹出动画,按钮点击改变图标及字体效果
4 高仿系列 6中经典动画 火车票出票动画 淘宝菜单 优酷导航菜单 图片3D浏览 下拉选择框 扫雷效果 5 增加新版本消息栏提醒功能 6 优化部分代码 下载app源码的朋友需要把 BAnim FilpLibrary BAnim ...
介绍了ObjectAnimator的基本使用方法,感兴趣的可以去看android animation系列的文章 地址:http://blog.csdn.net/QQxiaoqiang1573/article/details/52874751
Android 提供三种动画:帧动画、补间动画和属性动画,本篇文章介绍帧动画以及补间动画的使用,属性动画的使用将在后面的文章中分享,那就来复习一下这两种动画的使用吧。 FrameAnimation FrameAnimation 即逐帧动画...
ViewPager+Animation实现动画引导页
Android项目源码BaseAnimation数百种动画效果集合是一个安卓动画效果集合框架项目,包括Splash动画 (中心打开式效果 )、Flip折叠效果的集合(13种)、NineOld集合(18 )、高仿系列(6种经典动画)、火车票出票动画 淘宝...
Android flutter学习系列类个人中心动画手势学习01案例
属性动画相较帧动画和补间动画更强大,帧动画和补间动画只能应用于 View 及其子类,而属性动画可以修改任何对象的属性值,属性值可在指定的一段时间内自动改变,根据对象属性值的变化进而实现更复杂的动画。...
4 高仿系列 6中经典动画 火车票出票动画 淘宝菜单 优酷导航菜单 图片3D浏览 下拉选择框 扫雷效果 5 增加新版本消息栏提醒功能 6 优化部分代码 下载app源码的朋友需要把 BAnim FilpLibrary BAnim ...
Android进阶篇之引导页系列之强大的SurfaceView实现动画引导页(2)
将一系列图片有序播放,形成动画的效果。其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建animation-list为根节点的资源文件 <animation android:oneshot=false>...
我的Android开发之旅(二):Android三种动画效果的浅入之View...帧动画是通过顺序播放一系列的图片从而产生的动画效果。属性动画是在API11(Android3.0)引进的动画效果,它是通过动态地改变对象的属性从而达到动画
试用Android动画和MotionLayout。 电影应用动画的用例在这个项目中,我正在尝试Android动画和MotionLayout。 我发现一些文章和教程非常有帮助:Google开发人员对MotionLayout系列的介绍Android开发人员在...
这是Pro Android学习系列中2D动画部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512
一款影视综艺动画直播电视原生Android应用APP源码,可用于构建自己的影视播放平台,带有强大的管理面板和播放器,可轻松导入影视剧信息。该代码采用谷歌推荐的NVVM架构的最佳实践。 应用程序功能 带有Android架构...
Android中的动画分为视图动画(View Animation)...2、视图动画并没有真正改变View相应的属性值,这导致了UI效果与实际View状态存在差异,并导致了一系列怪异行为,比如在使用了视图动画TranslateAnimation的View的UI上对
主要为大家详细介绍了Android自定义View系列之Path绘制仿支付宝支付成功动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android之前的补间动画机制其实还算是比较健全的,在android.view.animation包下面有好多的类可以供我们操作,来完成一系列的动画效果,比如说对View进行移动、缩放、旋转和淡入淡出,并且我们还可以借助...
Android核心分析28篇,强烈推荐android初学者,android进阶者看看这个系列教程 Android应用开发者指南:性能优化 android开发教程合集(推荐新手看下这一季教程) 新手入门 会员贡献电子图书整理(内含PDF下载) ...
《嵌入式系统设计与开发系列丛书:Google Android开发技术》介绍Android系统的应用程序设计方法,全书分为8章,内容包括Android系统概述、Java语言、Android应用程序框架、Android单用户界面程序设计、Android多用户...