在平常的开发当中我们经常会碰见动画。绚丽的动画会使我们的APP更加有吸引力。比如我们在切换Activity的时候。使用自定义的动画肯定会比平常生硬的切换要好看的多。在了解动画的重要性之后,我也是下定决心要好好的深入的了解一下。不过做什么事都得有一个过程。肯定是由浅入深、循序渐进的。这个初识android动画系列的。就是讲解了动画的一些基本属性和使用。步入正轨
动画的分类
动画大体分为三种类型帧动画(Frame)、补间动画(Tween)、属性动画(Property)。其中属性动画是在3.0添加的。
帧动画的概念定义:
帧动画算是android动画里面比较简单的一种动画。意思是什么呢?就是把好多张图片放在一块,一张一张的轮流显示。就像放电影似的。
核心类是AnimotionDrawable 官方API 点击打开链接
逐帧动画创建一个最简单的方法是在一个XML文件中定义动画,放置在res /anim的文件夹中,并将它设置为一个视图对象的背景。然后调用start()运行动画。XML中定义的AnimationDrawable包含一个< animation-list >元素和一系列嵌套的< item >标记。每一项定义了一个帧的动画。我们来看一个例子。
<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/wheel0" android:duration="50" />
<item android:drawable="@drawable/wheel1" android:duration="50" />
<item android:drawable="@drawable/wheel2" android:duration="50" />
<item android:drawable="@drawable/wheel3" android:duration="50" />
<item android:drawable="@drawable/wheel4" android:duration="50" />
<item android:drawable="@drawable/wheel5" android:duration="50" />
</animation-list>
下面是对其中一些属性的介绍
XML属性
android:drawable:
要引用的资源。
android:duration:
持续的时间(以毫秒为单位)来显示这个框架。
android:oneshot:
如果设置成true,动画将只运行一次,然后停止。
android:variablePadding:
如果设置成true,允许drawable'padding填充改变基于选择的当前状态。
android:visible:
可见可拉的提供初始可见性状态;默认值是错误的。
然后在我们的Activity中添加下面的代码就可去显示。
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
是不是很简单。
示例:
先看看效果图吧
<?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/bird1" android:duration="200"/>
<item android:drawable="@drawable/bird2" android:duration="200"/>
<item android:drawable="@drawable/bird3" android:duration="200"/>
<item android:drawable="@drawable/bird14" android:duration="200"/>
</animation-list>
<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">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="118dp"
android:layout_marginTop="102dp"
android:src="@anim/my_frame_list" />
<Button
android:id="@+id/startPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginLeft="41dp"
android:layout_marginTop="75dp"
android:text="开始播放" />
<Button
android:id="@+id/stopPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/startPlay"
android:layout_alignBottom="@+id/startPlay"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/startPlay"
android:text="停止播放" />
</RelativeLayout>
public class MainActivity extends Activity implements OnClickListener{
private ImageView imageView1;
private Button startPlay;
private Button stopPlay;
private AnimationDrawable animationDawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
animationDawable=(AnimationDrawable) imageView1.getDrawable();
}
private void findViews() {
imageView1 = (ImageView)findViewById( R.id.imageView1 );
startPlay = (Button)findViewById( R.id.startPlay );
stopPlay = (Button)findViewById( R.id.stopPlay );
startPlay.setOnClickListener(this);
stopPlay.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startPlay:
animationDawable.start();
break;
case R.id.stopPlay:
animationDawable.stop();
break;
}
}
}
第二种方式:使用java代码
animationDawable.addFrame(getResources().getDrawable(R.drawable.bird1), 100);
animationDawable.addFrame(getResources().getDrawable(R.drawable.bird2), 100);
animationDawable.addFrame(getResources().getDrawable(R.drawable.bird3), 100);
animationDawable.addFrame(getResources().getDrawable(R.drawable.bird14), 100);
animationDawable.setOneShot(false);
imageView1.setImageDrawable(animationDawable);
就这么多。很简单。所以一般我们用不到。哈哈!上面给出了官方的API地址。上面的介绍也是非常的简单。