Android-ImageSwitcher-詳解

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0305/966.html


繼承關係,ImageSwitcher和TextSwitcher的繼承關係是一樣的。兩個重要的父類:ViewSwitcher和ViewAnimator
繼承於ViewSwitcher,說明具備了切換功能
繼承於ViewAnimator,說明具備了動畫功能




ImageSwitcher原理


ImageSwitcher的內容在Gallery中已經有所講解,這邊系統的詳解一下
ImageSwitcher粗略的理解就是ImageView的選擇器
ImageSwitcher的原理:ImageSwitcher有兩個子View:ImageView,當左右滑動的時候,就在這兩個ImageView之間來回切換來顯示圖片
下面我們來看看Android自帶的source,以便更深的理解這個原理:
既然有兩個子ImageView,那麼我們要創建兩個ImageView給ImageSwitcher。創建ImageSwitcher是通過工廠來實現的,看下面代碼

imageSwicher.setFactory(this);


爲imageSwitcher設置ViewFactory
@Override 
public View makeView() { 
    ImageView imageView = new ImageView(this); 
    imageView.setImageResource(arrayPictures[pictureIndex]); 
    return imageView; 
}


實現ViewFactory的makeView()方法,makeView()方法就是負責給ImageSwitcher創建兩個字ImageView
下面再來看看setFactory()方法的具體代碼
public void setFactory(ViewFactory factory) { 
    mFactory = factory; 
    obtainView(); 
    obtainView(); 
}


可以看到在setFactory的同時,調用了兩遍obtainView()方法,obtainView()方法就是給ImageSwitcher添加子ImageView的,調用兩遍就是添加了兩個子ImageView
再來看看obtainView()方法的具體代碼
private View obtainView() { 
    View child = mFactory.makeView(); 
    LayoutParams lp = (LayoutParams) child.getLayoutParams(); 
    if (lp == null) { 
        lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    } 
    addView(child, lp); 
    return child; 
}


可以看到obtainView()方法的的職責就是:通過makeView()方法創建View,然後把創建出來的View添加到ImageSwitcher上
再來看看下面的方法
public void setImageResource(int resid) 

    ImageView image = (ImageView)this.getNextView(); 
    image.setImageResource(resid); 
    showNext(); 
}


此方法就是用來顯示下一張圖片的,我們可以看到這個方法裏面調用了getNextView()方法和showNext()方法,那麼我們來看看這兩個方法的具體代碼
public View getNextView() { 
    int which = mWhichChild == 0 ? 1 : 0; 
    return getChildAt(which); 
}
public void showNext() { 
    setDisplayedChild(mWhichChild + 1); 
}


getNextView()方法是在兩個子ImageView之間切換,showNext()方法是負責顯示這兩個子View中的哪一個


也就是說,現用getNextView()方法得到下一個View,然後重新設置這個View的imageResource,最後通過showNext()方法將下一個View顯示出來


好了,ImageSwitcher的原理講完了。下面附上一個Demo



ImageSwitcher實例

main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <ImageSwitcher
  7. android:id="@+id/imageSwicher"
  8. android:layout_width="fill_parent"
  9. android:layout_height="0dip"
  10. android:layout_weight="1"
  11. android:background="@android:color/white"
  12. android:gravity="center" >
  13. </ImageSwitcher>
  14. </LinearLayout>

ImageSwicherDemoActivity.java

  1. package com.tianjf;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.View.OnTouchListener;
  7. import android.view.animation.AnimationUtils;
  8. import android.widget.ImageSwitcher;
  9. import android.widget.ImageView;
  10. import android.widget.ViewSwitcher.ViewFactory;
  11. /**
  12. * 一個左右滑動瀏覽圖片的Demo
  13. *
  14. * @author tianjf
  15. *
  16. */
  17. public class ImageSwicherDemoActivity extends Activity implements ViewFactory,
  18. OnTouchListener {
  19. private ImageSwitcher imageSwicher;
  20. // 圖片數組
  21. private int[] arrayPictures = { R.drawable.bg001, R.drawable.bg002,
  22. R.drawable.bg003, R.drawable.bg004 };
  23. // 要顯示的圖片在圖片數組中的Index
  24. private int pictureIndex;
  25. // 左右滑動時手指按下的X座標
  26. private float touchDownX;
  27. // 左右滑動時手指鬆開的X座標
  28. private float touchUpX;
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. imageSwicher = (ImageSwitcher) findViewById(R.id.imageSwicher);
  34. // 爲ImageSwicher設置Factory,用來爲ImageSwicher製造ImageView
  35. imageSwicher.setFactory(this);
  36. // 設置ImageSwitcher左右滑動事件
  37. imageSwicher.setOnTouchListener(this);
  38. }
  39. @Override
  40. public View makeView() {
  41. ImageView imageView = new ImageView(this);
  42. imageView.setImageResource(arrayPictures[pictureIndex]);
  43. return imageView;
  44. }
  45. @Override
  46. public boolean onTouch(View v, MotionEvent event) {
  47. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  48. // 取得左右滑動時手指按下的X座標
  49. touchDownX = event.getX();
  50. return true;
  51. } else if (event.getAction() == MotionEvent.ACTION_UP) {
  52. // 取得左右滑動時手指鬆開的X座標
  53. touchUpX = event.getX();
  54. // 從左往右,看前一張
  55. if (touchUpX - touchDownX > 100) {
  56. // 取得當前要看的圖片的index
  57. pictureIndex = pictureIndex == 0 ? arrayPictures.length - 1
  58. : pictureIndex - 1;
  59. // 設置圖片切換的動畫
  60. imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
  61. android.R.anim.slide_in_left));
  62. imageSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
  63. android.R.anim.slide_out_right));
  64. // 設置當前要看的圖片
  65. imageSwicher.setImageResource(arrayPictures[pictureIndex]);
  66. // 從右往左,看下一張
  67. } else if (touchDownX - touchUpX > 100) {
  68. // 取得當前要看的圖片的index
  69. pictureIndex = pictureIndex == arrayPictures.length - 1 ? 0
  70. : pictureIndex + 1;
  71. // 設置圖片切換的動畫
  72. // 由於Android沒有提供slide_out_left和slide_in_right,所以仿照slide_in_left和slide_out_right編寫了slide_out_left和slide_in_right
  73. imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this,
  74. R.anim.slide_out_left));
  75. imageSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,
  76. R.anim.slide_in_right));
  77. // 設置當前要看的圖片
  78. imageSwicher.setImageResource(arrayPictures[pictureIndex]);
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. }

ImageSwitcher實例

main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <ImageSwitcher
  7. android:id="@+id/imageSwicher"
  8. android:layout_width="fill_parent"
  9. android:layout_height="0dip"
  10. android:layout_weight="1"
  11. android:background="@android:color/white"
  12. android:gravity="center" >
  13. </ImageSwitcher>
  14. </LinearLayout>
發佈了6 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章