Android開發:Frame-By-Frame Animations的使用方法
- <?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/nv1" android:duration="500" />
- <item android:drawable="@drawable/nv2" android:duration="500" />
- <item android:drawable="@drawable/nv3" android:duration="500" />
- <item android:drawable="@drawable/nv4" android:duration="500" />
- </animation-list>
首先是建在res的drawable目錄下面建一個 anmi_nv.xml文件,再寫上上面的配置。
下面是MainActivity:
- package mars.animations05;
-
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
-
- public class MainActivity extends Activity {
- private Button button = null;
- private ImageView imageView = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- imageView = (ImageView)findViewById(R.id.imageViewId);
- button = (Button)findViewById(R.id.buttonId);
- button.setOnClickListener(new ButtonListener());
- }
-
- private class ButtonListener implements OnClickListener{
-
- public void onClick(View v) {
- imageView.setBackgroundResource(R.drawable.anim_nv);
- AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
- animationDrawable.start();
- }
-
- }
- }
main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
-
- <Button android:id="@+id/buttonId" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:layout_alignParentBottom="true"
- android:text="測試動畫效果" />
-
-
- <LinearLayout android:orientation="vertical"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
-
- <ImageView android:id="@+id/imageViewId"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_centerInParent="true" android:layout_marginTop="100dip"
- />
- </LinearLayout>
- </RelativeLayout>