现在的位置: 首页 > 综合 > 正文

用Gallery和ImageSwitcher显示从SD卡中获得略缩图和大图

2018年04月06日 ⁄ 综合 ⁄ 共 2110字 ⁄ 字号 评论关闭

效果图如下:

上面部分用Gallery实现,下部分用ImageSwitcher实现,单机上面的图片在ImageSwitcher上显示大图。XML代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />

<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ImageSwitcher>

</LinearLayout>

1. 获取SD卡中的所有照片
package>File>File[]>if (fls !=>}

@Override
public>// 获取图片路径
String>Log.v("path--->",>
/*
* //1. 一种略缩图方式 Bitmap>*>*
* /* 2. //另一种得到略缩图的方式:该方法得到的略缩图会根据自己的大小比例进行缩放,效果与Gallary结合,效果不是特别的好。
* BitmapFactory.Options>*>*>* =>*>* BitmapFactory.decodeFile(filepath,options);
*/

// 3. 特定比例显示略缩图:该方法能显示整个图片的略缩图,而不是裁剪性的略缩
/*
* BitmapFactory.Options>*>*
* // 记得把assets目录下的图片拷贝到SD卡中 *
* // 由于设置inJustDecodeBounds为true,因此执行下面代码后bitmap为空 Bitmap>* BitmapFactory.decodeFile(filepath,>*>* 重新读入图片,注意此时已经把options.inJustDecodeBounds设回false>* BitmapFactory.decodeFile(filepath,>*>* System.out.println(width + " " +>// 记得把assets目录下的图片拷贝到SD卡中
// 由于设置inJustDecodeBounds为true,因此执行下面代码后bitmap为空
Bitmap>int>int>float>float>
// 缩放图片的尺寸
float>// 写多大就多大
float>Matrix>matrix.postScale(scaleWidth,>bmpHeight,>alert.setMessage("文件或者文件夹不存在,\n" + "请在sd存储卡根目录建立pic文件夹,并将图片\n"
+ "保存在存储卡的pic文件夹下,\n" + "图片的格式应为jpg或者是gif和png格式,\n"
+ "然后打开本应用。");
alert.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public>
// 设置imageSwitch工厂
is.setFactory((ViewFactory)>// 创建ImageAdapter对象
ImageAdapter>ls);
gallery.setAdapter(sa);
// 设置监听
gallery.setOnItemSelectedListener(this);

}

@Override
public View>// 设置背景颜色
i.setBackgroundColor(0XFF000000);
// 设置精度类型
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
// 设置ImageSwitcher的布局参数(千万不能错)
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

return>try {
// 获取文件流对象
// FileInputStream>// 获取位图资源信息
// Bitmap>Bitmap>// 创建位图画桌
BitmapDrawable>// 将位图画桌设置到ImageSwitcher
is.setImageDrawable(bmdraw);
//>

源码下载地址:http://download.csdn.net/detail/zhuzhudeailnn/6768449

不懂可以留言。。。

抱歉!评论已关闭.