您的位置:首页 > 移动开发 > Android开发

动画的简单介绍

2013-11-08 15:04 891 查看
一、帧动画

res/drawable/frame_animaiton.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">

<item android:drawable="@drawable/a1" android:duration="1000"/>
<item android:drawable="@drawable/a2" android:duration="1000"/>
<item android:drawable="@drawable/a3" android:duration="1000"/>
<item android:drawable="@drawable/a4" android:duration="1000"/>

</animation-list></span>
oneshot的含义是:动画是否只播放一次

duration的含义是:该图片播放持续的时间

Animation.java

<span style="font-size:18px;">public class MainActivity extends Activity {
private ImageView imageView;
private Button button;
private AnimationDrawable ad;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
imageView.setBackgroundResource(R.drawable.frame_animation);
ad = (AnimationDrawable) imageView.getBackground(); //获得AnimationDraable对象
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ad.start(); //动画开始播放
}
});
}
}</span>
还有一点值得注意的是,getBackground这个方式异步的,它会在新的线程上运行,所以如果把ad.start()直接放在

imageView.getBackground下面就很有可能播放不出来了。原因是主线程执行ad.start()的方法优先于另外的一个线程执行getBackground加载图片。

二、Tween动画

(1)透明度

AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f); //从透明到不透明
animation1.setDuration(2000); //动画显示的时间
animation1.setRepeatCount(1); //重复一次,一共播放两次
animation1.setRepeatMode(Animation.REVERSE); //重复的动作为反转
animation1.setFillAfter(true); //停止在最后的位置
imageView.startAnimation(animation1);
(2)缩放

ScaleAnimation animation2 = new ScaleAnimation(0.5f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//参数的含义:X轴从50%-->200% Y轴从20%-->200% ,缩放点是相对于自身的50%,即中点
animation2.setDuration(2000);
animation2.setRepeatCount(1);
animation2.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation2);
(3)旋转

<span style="font-size:18px;">RotateAnimation animation3 = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //从0度旋转到180度,旋转的轴心是相对于自身的50%,即中心
animation3.setDuration(2000);
animation3.setRepeatCount(1);
animation3.setRepeatMode(Animation.REVERSE);
</span><pre name="code" class="java">imageView.startAnimation(animation3);



(4)平移

TranslateAnimation animation4 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
//相对于父窗体,X的起始位置是0(即图标当前所在的位置),X的移动距离是半个屏幕的像素
//相对于父窗体,Y的起始位置是0.5f(即图标当前所在的位置往下半个屏幕为起点),Y的移动距离是半个屏幕的像素
animation4.setDuration(2000);
animation4.setRepeatCount(1);
animation4.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation4);
(5)动画组合的效果

AnimationSet set = new AnimationSet(false);

RotateAnimation animation5 = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation5.setDuration(2000);
animation5.setRepeatCount(1);
animation5.setRepeatMode(Animation.REVERSE);

TranslateAnimation animation6 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
animation6.setDuration(2000);
animation6.setRepeatCount(1);
animation6.setRepeatMode(Animation.REVERSE);

set.addAnimation(animation5);
set.addAnimation(animation6);

imageView.startAnimation(set);

三、XML定义Tween动画

(1)res/anim/translate_animation

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="20%p"
android:toXDelta="80%p"
android:fromYDelta="20%p"
android:fillAfter="true"
android:toYDelta="80%p"
android:duration="2000">
</translate></span>
20%p的意思是相对于父控件的20%,20%就是相对于自身的20%

fillAfter的意思是保持为动画最后的样子

<span style="font-size:18px;">Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.translate_animation);
imageView.startAnimation(animations);</span>


(2)res/anim/set_animation

<span style="font-size:18px;"><set>

<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="true"
android:fromXDelta="20%p"
android:fromYDelta="20%p"
android:toXDelta="80%p"
android:toYDelta="80%p" >
</translate>

<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="true"
android:fromAlpha="1.0"
android:toAlpha="0.2" >
</alpha>

</set></span>


<span style="font-size:18px;">Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.set_animation);
imageView.setAnimation(animation1);</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息