Android中的动画详解系列
逐帧动画其实很简单,下面我们来看一个例子:
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
android:oneshot="false"的意思是循环播放
界面布局:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:onClick="start"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:onClick="stop"/>
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/zhuzhen"
android:layout_gravity="center"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:onClick="start"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:onClick="stop"/>
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/zhuzhen"
android:layout_gravity="center"/>
MainActivity.java
packagecom.example.testanimation;
importandroid.graphics.drawable.AnimationDrawable;
importandroid.os.Bundle;
importandroid.support.v7.app.ActionBarActivity;
importandroid.view.View;
importandroid.widget.ImageView;
publicclassMainActivityextendsActionBarActivity {
privateAnimationDrawable anim;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) findViewById(R.id.iv);
anim = (AnimationDrawable) iv.getBackground();
}
publicvoidstart(View view){
anim.start();
}
publicvoidstop(View view){
anim.stop();
}
}
package com.example.testanimation;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private AnimationDrawable anim;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) findViewById(R.id.iv);
anim = (AnimationDrawable) iv.getBackground();
}
public void start(View view){
anim.start();
}
public void stop(View view){
anim.stop();
}
}
运行效果: