Android中的动画(待续)
2015-08-11 19:35
686 查看
Android中的动画分为三种:帧动画(Frame Animation)、补间动画(Tween Animation)和属性动画(Property Animation)。
girl.xml
MainActivity.java
特性:智能应用与View对象,而且只支持一部分属性。它只是改变了View对象绘制的位置,而没有改变View对象本身的范围
方法一、逻辑代码
MainActivity.java
activity_main.xml
方法二、配置xml文件
在res文件夹中创建anim文件夹进行配置,最后在MainActivity中调用AnimationUtils.loadAnimation方法即可。
MainActivity.java
帧动画
就是通过一系列drawable依次显示来模拟动画效果。girl.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/girl_1" android:duration="200"/> <item android:drawable="@drawable/girl_2" android:duration="200"/> <item android:drawable="@drawable/girl_3" android:duration="200"/> <item android:drawable="@drawable/girl_4" android:duration="200"/> <item android:drawable="@drawable/girl_5" android:duration="200"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_8" android:duration="200"/> <item android:drawable="@drawable/girl_9" android:duration="200"/> <item android:drawable="@drawable/girl_10" android:duration="200"/> <item android:drawable="@drawable/girl_11" android:duration="200"/> </animation-list>
MainActivity.java
public class MainActivity extends Activity { private ImageView iv; private AnimationDrawable mAnimationDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); // 把xml文件的动画资源设置为iv背景 iv.setBackgroundResource(R.drawable.girl); // 获取设置的动画资源。 执行可能需要花费一定的时间 mAnimationDrawable = (AnimationDrawable) iv.getBackground(); } public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { mAnimationDrawable.start(); //异步操作 return true; } return super.onTouchEvent(event); } }
补间动画
Android中共有四种补间动画:透明度alpha、位移translate、缩放scale、旋转rotate。并且,可以使用set来定义以上四种动作的混合效果。我们可以通过逻辑代码来实现补间动画,也可以通过配置xml文件来实现相同效果。特性:智能应用与View对象,而且只支持一部分属性。它只是改变了View对象绘制的位置,而没有改变View对象本身的范围
方法一、逻辑代码
MainActivity.java
public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //透明度动画 AlphaAnimation public void alpha(View view){ AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f); aa.setDuration(2000); aa.setRepeatCount(1); //重复一次,总共两次 //aa.setRepeatCount(Animation.INFINITE); //无限循环播放 aa.setRepeatMode(Animation.REVERSE); //倒序播放 aa.setFillAfter(true); //停留在最后的效果 iv.startAnimation(aa); //开始动画 } //位移动画 TranslateAnimation public void trans(View view){ TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, //父窗体宽度的50% Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); ta.setDuration(2000); ta.setRepeatCount(1); ta.setRepeatMode(Animation.REVERSE); iv.startAnimation(ta); } //缩放动画 ScaleAnimation public void scale(View view){ ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000< ee16 /span>); sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); sa.setFillAfter(true); iv.startAnimation(sa); } //旋转动画 RotateAnimation public void rotate(View view){ RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); iv.startAnimation(ra); } //动画组合 AnimationSet public void set(View view){ AnimationSet set = new AnimationSet(false); TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); ta.setDuration(2000); ta.setRepeatCount(1); ta.setRepeatMode(Animation.REVERSE); ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); set.addAnimation(ra); //set.addAnimation(ta); set.addAnimation(sa); iv.startAnimation(set); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:onClick="rotate" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋转" /> <Button android:onClick="scale" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="缩放" /> <Button android:onClick="trans" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="位移" /> <Button android:onClick="alpha" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="透明度" /> <Button android:onClick="set" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="组合动画" /> </LinearLayout> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /> </RelativeLayout>
方法二、配置xml文件
在res文件夹中创建anim文件夹进行配置,最后在MainActivity中调用AnimationUtils.loadAnimation方法即可。
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" android:fillAfter="true" > </alpha>
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="-50%p" android:toXDelta="50%p" android:fromYDelta="0" android:toYDelta="0" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse"> </translate>
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" > </rotate>
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.1" android:toXScale="2.0" android:fromYScale="0.1" android:toYScale="2.0" android:duration="2000" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" > </scale>
<?xml version="1.0" encoding="utf-8"?> <set> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromAlpha="0.0" android:repeatCount="1" android:repeatMode="reverse" android:toAlpha="1.0" > </alpha> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toDegrees="360" > </rotate> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0.1" android:fromYScale="0.1" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toXScale="2.0" android:toYScale="2.0" > </scale> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="-50%p" android:fromYDelta="0" android:repeatCount="1" android:repeatMode="reverse" android:toXDelta="50%p" android:toYDelta="0" > </translate> </set>
MainActivity.java
public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //透明度动画 public void alpha(View view){ Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha); iv.startAnimation(aa); } //位移动画 public void trans(View view){ Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans); iv.startAnimation(ta); } //缩放动画 public void scale(View view){ Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale); iv.startAnimation(sa); } //旋转动画 public void rotate(View view){ Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate); iv.startAnimation(ra); } //动画组合 public void set(View view){ Animation set = AnimationUtils.loadAnimation(this, R.anim.set); iv.startAnimation(set); } }
属性动画
特性:可改变空间的属性,如位置、大小等。(待续)
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories