歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發:Frame-By-Frame Animations的使用方法

Android開發:Frame-By-Frame Animations的使用方法

日期:2017/3/1 11:16:15   编辑:Linux編程

Android開發:Frame-By-Frame Animations的使用方法

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot="false">
  4. <item android:drawable="@drawable/nv1" android:duration="500" />
  5. <item android:drawable="@drawable/nv2" android:duration="500" />
  6. <item android:drawable="@drawable/nv3" android:duration="500" />
  7. <item android:drawable="@drawable/nv4" android:duration="500" />
  8. </animation-list>

首先是建在res的drawable目錄下面建一個 anmi_nv.xml文件,再寫上上面的配置。

下面是MainActivity:

  1. package mars.animations05;
  2. import android.app.Activity;
  3. import android.graphics.drawable.AnimationDrawable;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. public class MainActivity extends Activity {
  10. private Button button = null;
  11. private ImageView imageView = null;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. imageView = (ImageView)findViewById(R.id.imageViewId);
  17. button = (Button)findViewById(R.id.buttonId);
  18. button.setOnClickListener(new ButtonListener());
  19. }
  20. private class ButtonListener implements OnClickListener{
  21. public void onClick(View v) {
  22. imageView.setBackgroundResource(R.drawable.anim_nv);
  23. AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
  24. animationDrawable.start();
  25. }
  26. }
  27. }

main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Button android:id="@+id/buttonId" android:layout_width="fill_parent"
  6. android:layout_height="wrap_content" android:layout_alignParentBottom="true"
  7. android:text="測試動畫效果" />
  8. <LinearLayout android:orientation="vertical"
  9. android:layout_width="fill_parent" android:layout_height="fill_parent">
  10. <ImageView android:id="@+id/imageViewId"
  11. android:layout_width="wrap_content" android:layout_height="wrap_content"
  12. android:layout_centerInParent="true" android:layout_marginTop="100dip"
  13. />
  14. </LinearLayout>
  15. </RelativeLayout>
Copyright © Linux教程網 All Rights Reserved