Android为动画开发提供了大量的类和接口。大多数类和接口在android.animation包中提供。
Android动画使你可以在运行时更改对象属性和行为。有多种方法可以在android中制作动画。
AnimationDrawable类提供了开始和结束动画的方法。甚至,你也可以使用基于时间的动画。
让我们看一下android动画的简单示例。
activity_main.xml
你只需要查看。
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
/>
仅具有图像视图。
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/anm"
>
MainActivity类
package com.srcmini.animation;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView anm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logo);
anm = (ImageView)findViewById(R.id.anm);
anm.setBackgroundResource(R.drawable.animation);
// the frame-by-frame animation defined as a xml file within the drawable folder
/*
* NOTE: It's not possible to start the animation during the onCreate.
*/
}
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation =
(AnimationDrawable) anm.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
}
你需要在res / drawable-hdpi目录中创建animation.xml文件。
你需要有很多图像。在这里,我们使用14张图像,所有14张图像都位于res / drawable-mdpi目录中。
android:oneshot="false">
输出: