目前android有三种动画类型:tween动画、frame动画和property动画
1、tween动画:
AlphaAnimation
|
渐变透明度动画效果
|
ScaleAnimation
|
渐变尺寸伸缩动画效果
|
TranslateAnimation
|
画面转换位置移动动画效果
|
RotateAnimation
|
画面转移旋转动画效果
|
可以在工程下res文件夹->anim文件夹(需要自己新建)下新建xml文件进行定义动画比如:
<span style="font-family:KaiTi_GB2312;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate />
<alpha/>
<scale/>
<rotate/>
</set></span>
再在java文件中调用Animation animation = AnimationUtils.loadAnimation(context,resid)方法获取到。
当然也可以在ja件中动态实例化对应的动画:
AlphaAnimation animation_Alpha= new AlphaAnimation(0.1f, 1.0f);
2、frame动画
可以将
XML文件放在/res/drawable/目录下<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/pic_state1" android:duration="200" />
<item android:drawable="@drawable/pic_state2" android:duration="200" />
<item android:drawable="@drawable/pic_state3" android:duration="200" />
</animation-list>
然后在java文件中获取到:
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setBackgroundResource(R.drawable.drawable_anim);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
再用AnimationDrawable的start和stop方法控制动画开始和停止。
需要注意几个问题:
-
要在代码中调用Imageview的setBackgroundResource方法,如果直接在XML布局文件中设置其src属性当触发动画时会出问题。
-
SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start(),或者利用handler.post()方法。
3、property动画
字面意思理解是属性动画,也就是说在执行动画的过程中可以改变当前执行动画对象的某个属性来控制动画的执行。这个动画在android3.0以后出来的,弥补了tween动画的某些缺陷,可以实现类似3D的旋转等等。
我实现了一个按钮的移动,当然按钮的监听会跟着按钮一起移动,而用tween动画移动后按钮监听还是在按钮的初始位置
上代码:
/**
* teen动画和property动画的实例
* @author hand
*
*/
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 用tween动画移动按钮,点击事件只发生在按钮原来的位置
* @param v
*/
public void clickBtn1(View v){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_x);
findViewById(R.id.tween_animation_btn).startAnimation(animation);
}
/**
* 用property动画移动按钮,点击事件跟随按钮位置变化而变化
* @param v
*/
@SuppressLint("NewApi")
public void clickBtn2(View v){
Button btn = (Button)findViewById(R.id.property_animation_btn);
Animator animator = AnimatorInflater.loadAnimator(this,R.animator.translate2);
animator.setTarget(btn);
animator.start();
}
}
其中
R.animator.translate2在res/animator(自己新建一个)目录下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
android:duration="2000"
android:propertyName="x"
android:valueFrom="0"
android:valueTo="450">
</objectAnimator>
</set>
activity_main.xml文件如下;
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hd.property_animation_demo.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/tween_animation_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tween动画"
android:onClick="clickBtn1"/>
<Button
android:id="@+id/property_animation_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="property动画"
android:onClick="clickBtn2"/>
</RelativeLayout>
以上主要是
property_animation_btn实现了点击自己移动,而上面的tween_animation_btn的按钮是用tween动画实现的移动,大家可以试试,两者区别明显:
图片不能上传了,就简单说一下:用tween动画实现移动以后,按钮停在动画结束为止,可以按钮监听停留在原位置没有随着按钮移动而移动;而property动画按钮监听随着按钮移动而移动到了动画结束的位置。
源码很简单,关键理解property动画的应用
分享到:
相关推荐
Android动画总结
Android帧动画和补间动画总结,帧动画和补间动画代码实现。
介绍Android动画AnimationSet,RotateAnimation等基本用法。
android移动开发总结动画效果,实现火焰动画
此Demo主要介绍Android属性动画的基本用法 Demo的博客地址:http://blog.csdn.net/u012814441/article/details/49026367
Android动画介绍-总结
Share Element 基于Android 系统的动画总结
NULL 博文链接:https://329716228.iteye.com/blog/1490689
Android属性动画示例Demo,附带个人总结的属性动画相关知识树。
Android三种动画的基本实现总结,详细了解请移步:http://blog.csdn.net/zxc514257857/article/details/76036753
Android动画合集,对Android动画所进行的最基础的总结,适合初学者,并没有什么酷炫的效果。
android动画大全,经过自己总结反思,很高兴能与大家分享
属性动画的demo,里面总结了属性动画的比较常用的一些效果,移动,旋转,文字弹跳,文字下坠等。
介绍Android动画AnimationSet,RotateAnimation等基本用法。
AdvanceAnimationAndroid动画总结总结了 Tween动画,属性动画,帧动画,CircularReveal,Activity转场动画,5.0新转场动画,5.0转场动画分为Explode、Slide、Fade、Share四种模式,矢量动画以及多种动画效果以及案例...
Android动画效果编程基础 Android的animation由四种类型组成 ndroid动画总结 逐帧动画(frame-by-frame animation),这种动画和GIF一样,一帧一帧的显示来组成动画效果;
文章开篇总结性文章:《Android 一共有多少种动画?准确告诉你!》 Ⅰ. View Animation / 视图动画 English explanation can go to read this article Ⅱ. Drawable Animation / 帧动画 / Frame 动画 English ...
【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 ... 【项目质量】: 所有源码都经过严格测试,...