歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中ImageSwitcher結合Gallery展示SD卡中的資源圖片

Android中ImageSwitcher結合Gallery展示SD卡中的資源圖片

日期:2017/3/1 10:24:07   编辑:Linux編程

本文主要是寫關於ImageSwitcher結合Gallery組件如何展示SDCard中的資源圖片,相信大家都看過API Demo 中也有關於這個例子的,但API Demo 中的例子是展示工程中Drawable目錄下的資源圖片,這樣調用系統的API比較容易實現,但我們在開發項目過程中,但有些圖片還不能完全確定下來,例如需要展示相機拍照的圖片,SDCard中某個目錄下的資源圖片等功能。其實系統中也提供相應的API給我們應用去實現該功能,下面就用異於API Demo中例子方式展示下如何實現該功能。

【1】我們先看下該例子代碼的結構圖:


下面就直接上各個文件的代碼了,不在這裡詳細解釋了,最後會看到實現的效果圖的..呵呵

【2】res/layout/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. android:background="#55000000" >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="center"
  11. android:text="Welcome to Andy.Chen's Blog!"
  12. android:textSize="20sp"/>
  13. <ImageSwitcher
  14. android:id="@+id/switcher"
  15. android:layout_width="wrap_content"
  16. android:layout_height="350dip"
  17. android:layout_alignParentLeft="true"
  18. android:layout_alignParentRight="true" />
  19. <Gallery
  20. android:id="@+id/mygallery"
  21. android:layout_width="fill_parent"
  22. android:layout_height="80dp"
  23. android:layout_alignParentBottom="true"
  24. android:layout_alignParentLeft="true"
  25. android:gravity="center_vertical"
  26. android:spacing="16dp" />
  27. </LinearLayout>
【3】res/values/attrs.xml 文件源碼:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="Gallery">
  4. <attr name="android:galleryItemBackground" />
  5. </declare-styleable>
  6. </resources>
【4】ImageSwitcherAndGalleryDemoActivity.java 源碼:(這個類的源碼比較多,希望大家耐心看)
  1. package com.andyidea.imagedemo;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.content.res.TypedArray;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.os.Environment;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.view.ViewGroup;
  17. import android.view.animation.AnimationUtils;
  18. import android.widget.AdapterView;
  19. import android.widget.AdapterView.OnItemClickListener;
  20. import android.widget.AdapterView.OnItemSelectedListener;
  21. import android.widget.BaseAdapter;
  22. import android.widget.Gallery;
  23. import android.widget.Gallery.LayoutParams;
  24. import android.widget.ImageSwitcher;
  25. import android.widget.ImageView;
  26. import android.widget.Toast;
  27. import android.widget.ViewSwitcher.ViewFactory;
  28. /**
  29. * ImageSwitcher和Gallery如何展示SD卡中的資源圖片
  30. * @author Andy.Chen
  31. * @email:[email protected]
  32. */
  33. public class ImageSwitcherAndGalleryDemoActivity extends Activity
  34. implements OnItemSelectedListener,ViewFactory{
  35. private List<String> imagePathList;
  36. private String[] list;
  37. private ImageSwitcher mSwitcher;
  38. private Gallery mGallery;
  39. /** Called when the activity is first created. */
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.main);
  44. imagePathList=getImagePathFromSD();
  45. list = imagePathList.toArray(new String[imagePathList.size()]);
  46. /* 設定Switcher */
  47. mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
  48. mSwitcher.setFactory(this);
  49. /* 設定載入Switcher的模式 */
  50. mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
  51. android.R.anim.fade_in));
  52. /* 設定輸出Switcher的模式 */
  53. mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
  54. android.R.anim.fade_out));
  55. mSwitcher.setOnClickListener(new OnClickListener() {
  56. public void onClick(View v) {
  57. Toast.makeText(ImageSwitcherAndGalleryDemoActivity.this, "你點擊了ImageSwitch上的圖片",
  58. Toast.LENGTH_SHORT).show();
  59. }
  60. });
  61. mGallery = (Gallery) findViewById(R.id.mygallery);
  62. /* 新增幾ImageAdapter並設定給Gallery對象 */
  63. mGallery.setAdapter(new ImageAdapter(this, getImagePathFromSD()));
  64. mGallery.setOnItemSelectedListener(this);
  65. /* 設定一個itemclickListener事件 */
  66. mGallery.setOnItemClickListener(new OnItemClickListener() {
  67. public void onItemClick(AdapterView<?> parent, View v,
  68. int position, long id) {
  69. Toast.makeText(ImageSwitcherAndGalleryDemoActivity.this, "你點擊了Gallery上的圖片",
  70. Toast.LENGTH_SHORT).show();
  71. }
  72. });
  73. }
  74. /** 從SD卡中獲取資源圖片的路徑 */
  75. private List<String> getImagePathFromSD() {
  76. /* 設定目前所在路徑 */
  77. List<String> it = new ArrayList<String>();
  78. //根據自己的需求讀取SDCard中的資源圖片的路徑
  79. String imagePath = Environment.getExternalStorageDirectory().toString()+"/hknational/image";
  80. File mFile = new File(imagePath);
  81. File[] files = mFile.listFiles();
  82. /* 將所有文件存入ArrayList中 */
  83. for (int i = 0; i < files.length; i++) {
  84. File file = files[i];
  85. if (checkIsImageFile(file.getPath()))
  86. it.add(file.getPath());
  87. }
  88. return it;
  89. }
  90. /** 判斷是否相應的圖片格式 */
  91. private boolean checkIsImageFile(String fName) {
  92. boolean isImageFormat;
  93. /* 取得擴展名 */
  94. String end = fName
  95. .substring(fName.lastIndexOf(".") + 1, fName.length())
  96. .toLowerCase();
  97. /* 按擴展名的類型決定MimeType */
  98. if (end.equals("jpg") || end.equals("gif") || end.equals("png")
  99. || end.equals("jpeg") || end.equals("bmp")) {
  100. isImageFormat = true;
  101. } else {
  102. isImageFormat = false;
  103. }
  104. return isImageFormat;
  105. }
  106. /* 改寫BaseAdapter自定義一ImageAdapter class */
  107. public class ImageAdapter extends BaseAdapter {
  108. /* 聲明變量 */
  109. int mGalleryItemBackground;
  110. private Context mContext;
  111. private List<String> lis;
  112. /* ImageAdapter的構造符 */
  113. public ImageAdapter(Context c, List<String> li) {
  114. mContext = c;
  115. lis = li;
  116. /*
  117. * 使用res/values/attrs.xml中的<declare-styleable>定義 的Gallery屬性.
  118. */
  119. TypedArray mTypeArray = obtainStyledAttributes(R.styleable.Gallery);
  120. /* 取得Gallery屬性的Index id */
  121. mGalleryItemBackground = mTypeArray.getResourceId(
  122. R.styleable.Gallery_android_galleryItemBackground, 0);
  123. /* 讓對象的styleable屬性能夠反復使用 */
  124. mTypeArray.recycle();
  125. }
  126. /* 重寫的方法getCount,傳回圖片數目 */
  127. public int getCount() {
  128. return lis.size();
  129. }
  130. /* 重寫的方法getItem,傳回position */
  131. public Object getItem(int position) {
  132. return position;
  133. }
  134. /* 重寫的方法getItemId,傳並position */
  135. public long getItemId(int position) {
  136. return position;
  137. }
  138. /* 重寫方法getView,傳並幾View對象 */
  139. public View getView(int position, View convertView, ViewGroup parent) {
  140. /* 產生ImageView對象 */
  141. ImageView i = new ImageView(mContext);
  142. /* 設定圖片給imageView對象 */
  143. Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());
  144. i.setImageBitmap(bm);
  145. /* 重新設定圖片的寬高 */
  146. i.setScaleType(ImageView.ScaleType.FIT_XY);
  147. /* 重新設定Layout的寬高 */
  148. i.setLayoutParams(new Gallery.LayoutParams(136, 88));
  149. /* 設定Gallery背景圖 */
  150. i.setBackgroundResource(mGalleryItemBackground);
  151. /* 傳回imageView對象 */
  152. return i;
  153. }
  154. }
  155. @Override
  156. public View makeView() {
  157. ImageView iv = new ImageView(this);
  158. iv.setBackgroundColor(0xFF000000);
  159. iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
  160. iv.setLayoutParams(new ImageSwitcher.LayoutParams(
  161. LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  162. return iv;
  163. }
  164. @Override
  165. public void onItemSelected(AdapterView<?> parent, View view, int position,
  166. long id) {
  167. // TODO Auto-generated method stub
  168. String photoURL = list[position];
  169. Log.i("A", String.valueOf(position));
  170. mSwitcher.setImageURI(Uri.parse(photoURL));
  171. }
  172. @Override
  173. public void onNothingSelected(AdapterView<?> parent) {
  174. // TODO Auto-generated method stub
  175. }
  176. }
【5】程序運行效果圖如下:

至此大功告成了!

本文為 Andy.Chen原創,歡迎大家轉載,轉載請注明出處,謝謝大家關注。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved