业务层提供数据:

实体类:

package com.ghg.Entity;import android.graphics.Bitmap;public class BitmapInfo {private String path;private String title;private Bitmap bitmap;public String getPath() {return path;}public void setPath(String path) {this.path = path;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Bitmap getBitmap() {return bitmap;}public void setBitmap(Bitmap bitmap) {this.bitmap = bitmap;}}

构造数据源:

package com.ghg.Bitmapbiz;import java.io.File;import java.util.ArrayList;import android.graphics.BitmapFactory;import com.ghg.BitmapService.BitmapService;import com.ghg.Entity.BitmapInfo;/** * 从文件中读取所有图片添加到list集合中,构成数据源 * @author gaohong * */public class BitmapBiz {public ArrayList<BitmapInfo> getBitmapInfos(String pathName){ArrayList<BitmapInfo> list=null;if(pathName!=null){//根据路径创建文件File dir=new File(pathName);if (dir.exists()&&dir.isDirectory()) {list=new ArrayList<BitmapInfo>();//初始化list集合String[] files=dir.list();//获取目录下所有文件名//遍历files并依次构建BitmapInfo对象for(String file:files){BitmapInfo info=new BitmapInfo();info.setTitle(file.substring(0, file.lastIndexOf(".")));info.setPath(pathName+file);info.setBitmap(BitmapService.getBitmap(info.getPath(), 80, 80));list.add(info);}}}return list;}}

工具类提供bitmap:

package com.ghg.BitmapService;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.BitmapFactory.Options;public class BitmapService {//通过路径名获得位图public static Bitmap getBitmap(String pathName){return BitmapFactory.decodeFile(pathName);}//通过路径名获得位图的缩略图public static Bitmap getBitmap(String pathName,int scale){Options opts=new Options();opts.inSampleSize=scale;return BitmapFactory.decodeFile(pathName, opts);}//通过文件名获得缩略图,指定缩略后的尺寸,宽,高各为多少public static Bitmap getBitmap(String pathName,int width,int height){Options opts=new Options();opts.inJustDecodeBounds=true;//仅读取边界信息BitmapFactory.decodeFile(pathName, opts);//读完后的信息保存到opts对象中int x=opts.outWidth/width;int y=opts.outHeight/height;int scale=x>y?x:y;opts.inJustDecodeBounds=false;return getBitmap(pathName, scale);}}

适配器提供视图项:

package com.ghg.BitmapAdapter;import java.util.ArrayList;import com.ghg.Entity.BitmapInfo;import com.ghg.Gallery.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class Adapter extends BaseAdapter {private ArrayList<BitmapInfo> list;private LayoutInflater inflater;public Adapter(Context context,ArrayList<BitmapInfo> list){this.inflater=LayoutInflater.from(context);this.list=list;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn list.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder=null;if (convertView==null) {//系统不提示自定义的资源,可以导入自己R资源,R前面加上R所在的包名;convertView=inflater.inflate(R.layout.item, null);holder=new ViewHolder();holder.galleryItem=(ImageView) convertView.findViewById(R.id.iv_calleryItem);holder.pictureTitle=(TextView) convertView.findViewById(R.id.tv_title);convertView.setTag(holder);}else {holder=(ViewHolder) convertView.getTag();}//从数据源读取数据,绘制Gallery的item项;BitmapInfo info=list.get(position);holder.galleryItem.setImageBitmap(info.getBitmap());holder.pictureTitle.setText(info.getTitle());return convertView;} class ViewHolder{private ImageView galleryItem;private TextView pictureTitle;}}

activity中显示Gallery:

package com.ghg.Gallery;import com.ghg.BitmapAdapter.Adapter;import com.ghg.BitmapService.BitmapService;import com.ghg.Bitmapbiz.BitmapBiz;import com.ghg.Entity.BitmapInfo;import android.app.Activity;import android.graphics.Bitmap;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.Gallery;import android.widget.ImageView;public class Day0704_GalleryDemoActivity extends Activity {private static final String PATHNAME="/mnt/sdcard/imgs/";    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        initView();        initListener();    }        ImageView imageView;    private void initListener() {    imageView=(ImageView) findViewById(R.id.iv_topPic);gallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubBitmapInfo info=(BitmapInfo) adapter.getItem(position);Bitmap bm=BitmapService.getBitmap(info.getPath());imageView.setImageBitmap(bm);}@Overridepublic void onNothingSelected(AdapterView<?> parent) {// TODO Auto-generated method stub}});}    Gallery gallery;    BitmapBiz biz;    Adapter adapter;private void initView() {// TODO Auto-generated method stubgallery=(Gallery) findViewById(R.id.gallery);biz=new BitmapBiz();adapter=new Adapter(this, biz.getBitmapInfos(PATHNAME));gallery.setAdapter(adapter);}}
用到的布局文件:


main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageView         android:id="@+id/iv_topPic"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1.0"        />    <Gallery         android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:spacing="10dp"        /></LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="80dp"    android:layout_height="80dp" >    <ImageView         android:id="@+id/iv_calleryItem"        android:layout_width="80dp"        android:layout_height="80dp"        android:scaleType="fitCenter"        />    <TextView         android:id="@+id/tv_title"        android:layout_width="80dp"        android:layout_height="wrap_content"        android:layout_gravity="bottom"        android:gravity="center_horizontal"        /></FrameLayout>



更多相关文章

  1. 解决Cocos2d-x3.1编译生成Android程序出现Android(安卓)NDK:Abor
  2. 文件读写
  3. Android(安卓)Uri转换成真实File路径
  4. mac下android studio安装的几个文件路径
  5. 如何获取Android设备名称(常用ADB命令介绍)
  6. android 从矢量图SVG获取位图bitmap
  7. android涂鸦
  8. Android小米(miui)获取通话记录为null解决办法
  9. Android(安卓)http协议实现文件下载

随机推荐

  1. Eclipse 开发 Android, Hello, DatePicke
  2. Java finished with non-zero exit value
  3. Android(安卓)Socket网络通信
  4. Android(安卓)中设计模式 ----原型模式
  5. android 开发 实例 下部主导航(1)
  6. Android(安卓)Keep screen on(保持屏幕唤
  7. Android(安卓)adb截屏命令
  8. Android桌面快捷方式的实现
  9. Android获取屏幕宽高的方法
  10. Android获取系统的内存使用率