首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Gallery相本两种读取图片的方法

2012-09-06 
Gallery相册两种读取图片的方法看了几个教程,学会 了两种方法实现Gallery相册,第一种是直接在res文件夹下

Gallery相册两种读取图片的方法

看了几个教程,学会 了两种方法实现Gallery相册,第一种是直接在res文件夹下放图片进行读取,第二种是读取sd卡的图片。

?

?

首先,写好布局main.xml

?

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><Gallery     android:id="@+id/gallery"     android:layout_height="143px"     android:layout_width="fill_parent"     /><ImageView    android:layout_width="239px"    android:layout_height="218px"    android:layout_x="38px"    android:layout_y="184px"    android:id="@+id/ImageView_photo" ></ImageView>   </LinearLayout>

?

?其二,在values文件夹下新建一个attr.xml

??<?xml version="1.0" encoding="utf-8"?>

<resources>     <declare-styleable name="Gallery">       <attr name="android:galleryItemBackground" />     </declare-styleable>   </resources>  

? ?其三,写一个类,ImageAdapter.java

?

??package com.chaowen;

import java.io.File;import java.util.ArrayList;import java.util.List;import android.R.color;import android.app.Activity;import android.content.Context;import android.content.res.Resources;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.text.AndroidCharacter;import android.util.DisplayMetrics;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {    private Context ctx;    int mGalleryItemBackground;    private List<String> lis;        //这是第一种方法,直接用res文件夹下的图片    /*public  int[] images = {       R.drawable.img01,R.drawable.img02,       R.drawable.img03,R.drawable.img04    };*/        public ImageAdapter(Context ctx,List<String> li){    try {this.ctx = ctx;lis = li ;            //使用res/values/attr.xml中的<declare-styleable>定义的Gallery属性TypedArray a = ctx.obtainStyledAttributes(R.styleable.Gallery);//取得Gallery属性的Index idmGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, Color.GREEN);        //让对象的styleable属性能够反复使用a.recycle();    } catch (Exception e) {  e.printStackTrace();}    }        @Overridepublic int getCount() {return lis.size();}@Overridepublic Object getItem(int position) {  return position;}    @Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View converView, ViewGroup parent) {try {ImageView v = new ImageView(this.ctx);//设定图片给ImageView对象Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());v.setImageBitmap(bm);/*v.setImageResource(this.images[position]);*///重新设定图片的宽高v.setScaleType(ImageView.ScaleType.FIT_XY);//重新设定layout的宽高v.setLayoutParams(new Gallery.LayoutParams(128,128));v.setBackgroundResource(mGalleryItemBackground);return v;} catch (Exception e) {    e.printStackTrace();}return null;}    }

?

最后,在主Activity类的代码如下

package com.chaowen;import java.io.File;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Toast;public class GalleryDemo extends Activity {    /** Called when the activity is first created. */private Gallery mGallery;private ImageView imageView;private ImageAdapter imageAdapter;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                 mGallery =(Gallery) findViewById(R.id.gallery);         imageView = (ImageView)findViewById(R.id.ImageView_photo);         imageAdapter = new ImageAdapter(this,getSD());        mGallery.setAdapter(imageAdapter);                mGallery.setOnItemClickListener(new Gallery.OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View v, int position,long id) {//显示该图片是几号                Toast.makeText(GalleryDemo.this,                                    "这是图片:"+position+"号", Toast.LENGTH_SHORT).show();                //设置大图片                String photoURL = getSD().get(position);              //  imageView.setBackgroundResource(imageAdapter.images[position]); 这是用res资源下的图片的方法进行放大                imageView.setImageURI(Uri.parse(photoURL));}                });    }                /** * 获取sd卡的图片 * @return */private List<String> getSD()   {     /* 设定目前所在路径 */     List<String> it=new ArrayList<String>();          File f=new File("/sdcard/dcim/Camera/");      File[] files=f.listFiles();     /* 将所有文件存入ArrayList中 */     for(int i=0;i<files.length;i++)     {       File file=files[i];       if(getImageFile(file.getPath()))         it.add(file.getPath());     }     return it;   }      //获得文件的类型private boolean getImageFile(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;   } }
?

?

?

?

?

?

?

?

热点排行