packagecom.xiaoma.www;
importandroid.app.Activity;
importandroid.os.Bundle;
importjava.io.File;
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.res.Resources;
importandroid.content.res.TypedArray;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.WindowManager;
importandroid.view.View.OnClickListener;
importandroid.view.animation.Animation;
importandroid.view.animation.AnimationUtils;
importandroid.widget.AdapterView;
importandroid.widget.BaseAdapter;
importandroid.widget.Gallery;
importandroid.widget.ImageSwitcher;
importandroid.widget.ImageView;
importandroid.widget.Toast;
importandroid.widget.ViewSwitcher;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.Gallery.LayoutParams;
/**
* @Title: GalleryAddSDPhotoActivity.java
* @Package com.xiaoma.www
* @Description: Gallery与ImageSwitch测试
* @author MZH
* @version V2.2
*/
publicclassGalleryAddSDPhotoActivityextendsActivityimplements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
privateList ImageList;
privateString[] list;
privateImageSwitcher mSwitcher;
publicvoidonCreate(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(newString[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(newOnClickListener() {
publicvoidonClick(View v) {
Toast.makeText(GalleryAddSDPhotoActivity.this,
"点击了ImageSwitch中的图片",
Toast.LENGTH_SHORT).show();
}
});
Gallery g = (Gallery) findViewById(R.id.mygallery);
/**
* 添加ImageAdapter并设定给Gallery对象
*/
g.setAdapter(newImageAdapter(this, getInSDPhoto()));
/**
* 监听器设置
*/
g.setOnItemSelectedListener(this);
/* 设定一个itemclickListener事件 */
g.setOnItemClickListener(newOnItemClickListener() {
publicvoidonItemClick(AdapterView> parent, View v,
intposition,longid) {
Toast.makeText(GalleryAddSDPhotoActivity.this,
//注意下小细节 ,position+1如果不用括号的话,定位就错啦,朋友可以取掉试下
"点击了Gallery画廊中的第 "+(position+1)+"张图片",
Toast.LENGTH_SHORT).show();
}
});
}
/**
* 获取SD卡中图片文件的方法实现
* @return
*/
privateList getInSDPhoto() {
/**
* 设定图片所在路径
*/
List it = newArrayList();
/**
* 此处小马直接把图片push进了SD卡根路径下,如果朋友们要放到其它文件下,
* 可先判断下SD卡存在时将路径指定到自己的路径下就可以了,试试吧,吼吼
*/
File f = newFile("/sdcard/");
File[] files = f.listFiles();
/**
* 将所有文件存入ArrayList中,这个地方存的还是文件路径哦
*/
for(inti =0; i
File file = files[i];
if(getAllImage(file.getPath()))
it.add(file.getPath());
}
returnit;
}
/**
* 获取所有图片文件的实现
* @param fName
* @return
*/
privatebooleangetAllImage(String fName) {
booleanre;
/* 取得扩展名 */
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;
}
returnre;
}
/**
* 改写BaseAdapter自定义一ImageAdapter class
* @Title: ImageAdapter.java
* @Package com.xiaoma.www
* @Description: Gallery 适配器实现
* @author MZH
* @version V2.2
*/
//适配器中常用的方法小马就不加注释了
publicclassImageAdapterextendsBaseAdapter {
/* 声明变量 */
intmGalleryItemBackground;
privateContext mContext;
privateList lis;
/**
* ImageAdapter的构造符
* @param c
* @param list
*/
publicImageAdapter(Context context, List 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();
}
publicintgetCount() {
returnlis.size();
}
publicObject getItem(intposition) {
returnposition;
}
publiclonggetItemId(intposition) {
returnposition;
}
/* 几定要重写的方法getView,传并几View对象 */
publicView getView(intposition, View convertView, ViewGroup parent) {
ImageView i = newImageView(mContext);
//使用图片工厂载入并设置图片
Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());
i.setImageBitmap(bm);
/**
* 设定ImageView宽高,跟了下FIX_XY,竟然是枚举,好激动,小马好久没看到枚举啦
* 试了下ScaleType中除FIT_XY以外的其它选项,效果图分别如果下:
*/
i.setScaleType(ImageView.ScaleType.FIT_XY);