Gallery显示,读取SD卡内的图片

在图片当道的如下,还是有图更有说服力。
小子不才,所以这个demo是我东找西找然后自己凑的一个例子。内有失当之处,多包涵多指正!
等了三天才能发帖子,这点JAVAEYE真的应该改变一下了,要不然把我这样的孩子憋生啥样了。。。
1。首先定义xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#55000000"
>
<ImageSwitcher //此控件与Gallery会经常用的,切记
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="350dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
<Gallery
android:id="@+id/mygallery"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp"
/>


</RelativeLayout>
2。Activity中要自定义显示
package com.ldci.second.mypnone;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
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.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;

public class BgPictureShowActivity 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.pictureshow);

ImageList=getSD();
list = ImageList.toArray(new String[ImageList.size()]);

/*设定Switcher*/
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
/*设定载入Switcher的模式*/
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
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(BgPictureShowActivity.this, "点击了", Toast.LENGTH_SHORT).show();

}

});

Gallery g = (Gallery) findViewById(R.id.mygallery);

/*新增几ImageAdapter并设定给Gallery对象*/
g.setAdapter(new ImageAdapter(this,getSD()));

g.setOnItemSelectedListener(this);

/*设定一个itemclickListener事件*/
g.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,
View v, int position, long id)
{
Toast.makeText(BgPictureShowActivity.this, "dianjiale ", Toast.LENGTH_SHORT).show();
}
});
}

private List<String> getSD()
{
/* 设定目前所在路径 */
List<String> it=new ArrayList<String>();
File f=new File("/sdcard/");
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;
}

/*改写BaseAdapter自定义一ImageAdapter class*/
public class ImageAdapter extends BaseAdapter
{
/*声明变量*/
int mGalleryItemBackground;
private Context mContext;
private List<String> lis;

/*ImageAdapter的构造符*/
public ImageAdapter(Context c,List<String> li)
{
mContext = c;
lis=li;
/* 使用res/values/attrs.xml中的<declare-styleable>定义
* 的Gallery属性.*/
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
/*取得Gallery属性的Index id*/
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
/*让对象的styleable属性能够反复使用*/
a.recycle();
}

/*几定要重写的方法getCount,传回图片数目*/
public int getCount()
{
return lis.size();
}
/*一定要重写的方法getItem,传回position*/
public Object getItem(int position)
{
return position;
}

/*一定要重写的方法getItemId,传并position*/
public long getItemId(int position)
{
return position;
}

/*几定要重写的方法getView,传并几View对象*/
public View getView(int position, View convertView,
ViewGroup parent)
{
/*产生ImageView对象*/
ImageView i = new ImageView(mContext);
/*设定图片给imageView对象*/
Bitmap bm = BitmapFactory.decodeFile(lis.
get(position).toString());
i.setImageBitmap(bm);
/*重新设定图片的宽高*/
i.setScaleType(ImageView.ScaleType.FIT_XY);
/*重新设定Layout的宽高*/
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("A",String.valueOf(position));

mSwitcher.setImageURI(Uri.parse(photoURL));

}

public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}

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;
}
}
这段代码很长,看的时候肯定会头疼加DAN疼,但是一定要挺住啊,MARS大神一再告诫咱们,这个行业的特点就是要有耐心。 :oops:
3。对了,还有一个非常重要的事情,就是在values文件夹下面,新建一个xml,内书:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
最后就成功了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值