由于最近小馬要使用下Gallery與ImageSwitch實作照片的預覽功能,特此找個時間寫了個小DEMO測試了下,完整的哦,直接開始,今天好激動呢,吼吼,先分段講下,最後貼出源代碼,看代碼之前先往你的模拟器“/sdcard/”下PUSH幾張圖檔哦,作為工程中使用的圖檔資源,看代碼:
package com.xiaoma.www;
import android.app.Activity;
import android.os.Bundle;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;
/**
* @Title: GalleryAddSDPhotoActivity.java
* @Package com.xiaoma.www
* @Description: Gallery與ImageSwitch測試
* @author MZH
* @version V2.2
*/
public class GalleryAddSDPhotoActivity extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
private List<String> ImageList;
private String[] list;
private ImageSwitcher mSwitcher;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉狀态欄,全屏顯示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
/**
* 取得SD卡上的圖檔檔案,并将圖檔完整路徑儲存到ImageList中,是完整路徑哦
*/
ImageList = getInSDPhoto();
/**
* 将取得的路徑集合ImageList轉換成數組并存入list中
* List集合中的toArray()方法經常用在集合與數組轉換的,吼吼
*/
list = ImageList.toArray(new String[ImageList.size()]);
// 設定Switcher
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
* 設定載入Switcher的模式
* AnimationUtils此類小馬我不知道是幹嗎的,查了源碼,用工具,大緻意思如果下:
* Defines common utilities for working with animations
* 即:定義動畫通用的工具類,不懂了一個詞一個詞的查
*
* loadAnimation()此方法見名字就知道是什麼意思了
* Loads an {@link Animation} object from a resource
* 通過 資源檔案載入
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
//下面這個參數小馬看了下源碼,他們把這個int值轉化成了十六進制載入?小馬不明白為這樣一定要
//轉換成十六進制啊?看到小馬問題的朋友如果知道話請指點一下,小馬先謝過
android.R.anim.fade_in));
* 設定輸出Switcher的模式
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mSwitcher.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(GalleryAddSDPhotoActivity.this,
"點選了ImageSwitch中的圖檔",
Toast.LENGTH_SHORT).show();
}
});
Gallery g = (Gallery) findViewById(R.id.mygallery);
* 添加ImageAdapter并設定給Gallery對象
g.setAdapter(new ImageAdapter(this, getInSDPhoto()));
* 監聽器設定
g.setOnItemSelectedListener(this);
/* 設定一個itemclickListener事件 */
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(GalleryAddSDPhotoActivity.this,
//注意下小細節 ,position+1如果不用括号的話,定位就錯啦,朋友可以取掉試下
"點選了Gallery畫廊中的第 "+(position+1)+"張圖檔",
}
/**
* 擷取SD卡中圖檔檔案的方法實作
* @return
*/
private List<String> getInSDPhoto() {
* 設定圖檔所在路徑
List<String> it = new ArrayList<String>();
* 此處小馬直接把圖檔push進了SD卡根路徑下,如果朋友們要放到其它檔案下,
* 可先判斷下SD卡存在時将路徑指定到自己的路徑下就可以了,試試吧,吼吼
File f = new File("/sdcard/");
File[] files = f.listFiles();
* 将所有檔案存入ArrayList中,這個地方存的還是檔案路徑哦
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (getAllImage(file.getPath()))
it.add(file.getPath());
}
return it;
* 擷取所有圖檔檔案的實作
* @param fName
private boolean getAllImage(String fName) {
boolean re;
/* 取得擴充名 */
String end = fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();
/* 按擴充名的類型決定MimeType */
if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
re = true;
} else {
re = false;
return re;
* 改寫BaseAdapter自定義一ImageAdapter class
* @Title: ImageAdapter.java
* @Package com.xiaoma.www
* @Description: Gallery 擴充卡實作
* @author MZH
* @version V2.2
//擴充卡中常用的方法小馬就不加注釋了
public class ImageAdapter extends BaseAdapter {
/* 聲明變量 */
int mGalleryItemBackground;
private Context mContext;
private List<String> lis;
* ImageAdapter的構造符
* @param c
* @param list
public ImageAdapter(Context context, List<String> list) {
mContext = context;
lis = list;
/*
* 使用styleable.xml設定Gallery屬性
* 下面TypedArray類小馬也不知道是什麼,眼饞跟進去看了下,貼下源碼
* /**
* Container for an array of values that were retrieved with
* {@link Resources.Theme#obtainStyledAttributes(AttributeSet,
* int[], int, int)}
* or {@link Resources#obtainAttributes}. Be
* sure to call {@link #recycle} when done with them.
*
* The indices used to retrieve values from this structure correspond to
* the positions of the attributes given to obtainStyledAttributes.
* 看了下,大體意思這句就講清楚了: Container for an array of values that were retrieved
* 是說:存放從數組中讀取出的值(而且這些值可以反複使用的哦)的容器
*/
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
//讓對象的styleable屬性能夠反複使用
//源碼中:Be sure to call {@link #recycle} when done with them.
a.recycle();
public int getCount() {
return lis.size();
public Object getItem(int position) {
return position;
public long getItemId(int position) {
/* 幾定要重寫的方法getView,傳并幾View對象 */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
//使用圖檔工廠載入并設定圖檔
Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());
i.setImageBitmap(bm);
/**
* 設定ImageView寬高,跟了下FIX_XY,竟然是枚舉,好激動,小馬好久沒看到枚舉啦
* 試了下ScaleType中除FIT_XY以外的其它選項,效果圖分别如果下:
i.setScaleType(ImageView.ScaleType.FIT_XY);
貼圖小馬就一次性貼出來,這樣容易看出它們之間的差别:
FIT_XY效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201112/033750386.jpg"></a>
FIT_START效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201112/033852898.jpg"></a>
FIT_END效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201112/033929103.jpg"></a>
FIT_CENTER效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201112/034026365.jpg"></a>
CENTER_INSIDE效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201112/034049981.jpg"></a>
CENTER_CROP效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201112/034112973.jpg"></a>
CENTER效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201112/034132206.jpg"></a>
下面來看下半部分代碼:
//重新設定Layout的寬高
* 這個地方小馬有個疑問,我設定Gallery全屏的時候畫面會拉伸,
* 有什麼辦法可以不讓他拉伸還可以讓它全屏啊?指點下小馬,謝謝
* i.setLayoutParams( new Gallery.LayoutParams(
* WindowManager.LayoutParams.MATCH_PARENT,
* WindowManager.LayoutParams.MATCH_PARENT));
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
// 設定Gallery背景圖
i.setBackgroundResource(mGalleryItemBackground);
// 傳回imageView對象
return i;
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String photoURL = list[position];
Log.i("XiaoMa", String.valueOf(position));
mSwitcher.setImageURI(Uri.parse(photoURL));
public void onNothingSelected(AdapterView<?> parent) {
* 實作ViewSwitcher.ViewFactory接口必須實作的方法
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
為供朋友們學習交流,如果覺得在這兒看代碼不太友善的話,可以下載下傳小馬上傳的附件,裡面是所有代碼,在這篇文章中如果看到有地方小馬有錯誤的,請直接批評指正,還有小馬在中間提的問題,知情人士請指點下小馬,先謝謝啦,吼吼。。O_O
<a href="http://down.51cto.com/data/2359572" target="_blank">附件:http://down.51cto.com/data/2359572</a>
本文轉自華華世界 51CTO部落格,原文連結:http://blog.51cto.com/mzh3344258/752580,如需轉載請自行聯系原作者