歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android ImageSwitcher 圖片切換 按鈕點擊切換

Android ImageSwitcher 圖片切換 按鈕點擊切換

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

圖片顯示:

1. MainActivity

  1. package com.gamedog.test;
  2. import Android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.ImageSwitcher;
  7. import android.widget.ImageView;
  8. import android.widget.ViewSwitcher.ViewFactory;
  9. public class MainActivity extends Activity implements ViewFactory
  10. {
  11. private ImageSwitcher switcher;
  12. private Button forward;
  13. private Button next;
  14. // 圖片索引
  15. private static int index = 0;
  16. // 顯示的圖片資源
  17. private static final Integer[] imagelist =
  18. { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6 };
  19. @Override
  20. public void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. forward = (Button) findViewById(R.id.forward);
  25. next = (Button) findViewById(R.id.next);
  26. switcher = (ImageSwitcher) findViewById(R.id.image);
  27. switcher.setFactory(this);
  28. switcher.setImageResource(imagelist[index]);
  29. // 上一張
  30. forward.setOnClickListener(new View.OnClickListener()
  31. {
  32. @Override
  33. public void onClick(View view)
  34. {
  35. index--;
  36. if (index < 0)
  37. {
  38. index = imagelist.length - 1;
  39. }
  40. switcher.setImageResource(imagelist[index]);
  41. }
  42. });
  43. // 下一張
  44. next.setOnClickListener(new View.OnClickListener()
  45. {
  46. @Override
  47. public void onClick(View view)
  48. {
  49. index++;
  50. if (index >= imagelist.length)
  51. {
  52. index = 0;
  53. }
  54. switcher.setImageResource(imagelist[index]);
  55. }
  56. });
  57. }
  58. // 用於顯示圖片
  59. @Override
  60. public View makeView()
  61. {
  62. return new ImageView(this);
  63. }
  64. }

2.布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7. android:orientation="horizontal"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:gravity="center"
  11. android:background="#f0f0f0"
  12. >
  13. <Button
  14. android:id="@+id/forward"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:background="@drawable/left_btn"
  18. android:layout_gravity="center"
  19. />
  20. <ImageSwitcher
  21. android:id="@+id/image"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginLeft="20dp"
  25. android:layout_marginRight="20dp"
  26. />
  27. <Button
  28. android:id="@+id/next"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:background="@drawable/right_btn"
  32. android:layout_gravity="center"
  33. />
  34. </LinearLayout>
  35. </ScrollView>
Copyright © Linux教程網 All Rights Reserved