一,com.bean包 下 PicBean.java
public class PicBean implements Serializable{
private String fname;
private String path;
public PicBean() {
super();
// TODO Auto-generated constructor stub
}
public PicBean(String fname, String path) {
super();
this.fname = fname;
this.path = path;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
二,com.adapter包下 PicListAdapter.java
public class PicListAdapter extends BaseAdapter {
private Context context;
private List<PicBean> lspic;
private LayoutInflater layoutInflater;
public PicListAdapter(Context context, List<PicBean> lspic) {
super();
this.context = context;
this.lspic = lspic;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lspic.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return lspic.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
private class ViewHolder{
public ImageView iv;
}
@Override
public View getView(int position, View convertView, ViewGroup root) {
ViewHolder vh=null;
if(convertView==null){
vh=new ViewHolder();
convertView=layoutInflater.inflate(R.layout.gallery_item_layout, null);
vh.iv=(ImageView) convertView.findViewById(R.id.gallery_item_iv);
convertView.setTag(vh);
}else{
vh=(ViewHolder) convertView.getTag();
}
PicBean pb=lspic.get(position);
try {
//獲取位圖對象,將其添加到ImageView上
Bitmap bm=BitmapFactory.decodeStream(context.getAssets().open(pb.getPath()));
vh.iv.setImageBitmap(bm);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
}
三,com.action 包下ImageSwitcherActivity.java
public class ImageSwitcherActivity extends Activity implements ViewFactory{
private ImageSwitcher imageSwitcher;
private Gallery gallery;
private List<PicBean> lspb;
private PicListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageswitcher_layout);
imageSwitcher=(ImageSwitcher) findViewById(R.id.is_imageSwitcher);
gallery=(Gallery) findViewById(R.id.is_gallery);
//給ImageeSwitcher設置視圖工廠
imageSwitcher.setFactory(this);
lspb=new PicUtil().getdata(this);
adapter=new PicListAdapter(this, lspb);
gallery.setAdapter(adapter);
//給Gallery添加選項選中事件的監聽
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
final PicBean pb=lspb.get(arg2);
try {
//獲取位圖對象
Bitmap bm=BitmapFactory.decodeStream(ImageSwitcherActivity.this.getAssets().open(pb.getPath()));
//創建位圖桌面對象
BitmapDrawable drawable=new BitmapDrawable(bm);
imageSwitcher.setImageDrawable(drawable);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public View makeView() {
ImageView iv=new ImageView(this);
//設置ImageView的等比例方法
iv.setScaleType(ImageView.ScaleType.FIT_XY);
//設置ImageView在ImageSwitcher中的位置,ImageView在ImageSwitcher中的寬和高於於ImageSwitcher相同
iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return iv;
}
}
四,com.util包下PicUtil.java
package com.util;
import android.content.Context;
import com.bean.*;
import java.io.IOException;
import java.util.*;
public class PicUtil {
public List<PicBean> getdata(Context context){
List<PicBean> lspic=new ArrayList<PicBean>();
try {
String[] fnames=context.getAssets().list("pic");
for (String fname : fnames) {
PicBean pb=new PicBean(fname, "pic/"+fname);
lspic.add(pb);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("lspic-->"+lspic.size());
return lspic;
}
}
五,XML文件
1.imageswitcher_layout.xml
<RelativeLayout 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/is_imageSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ImageSwitcher>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
>
<Gallery
android:id="@+id/is_gallery"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:spacing="15dp"
/>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/gallery_item_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
六,圖片路徑 放在assets下pic文件夾內
自己寫的,絕對OK能用!