Main
/*
* 图片浏览器
*/public class MainActivity
extends Activity
implements ViewFactory{
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private int[] imageid
=new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery
=(Gallery) findViewById(R.id.gallery1);
imageSwitcher
=(ImageSwitcher) findViewById(R.id.imageSwitcher);
GalleryAdapter galleryAdapter
=new GalleryAdapter(
this, imageid);
gallery.setAdapter(galleryAdapter);
imageSwitcher.setFactory(
this);
//设置进入出去动画 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this, android.R.anim.fade_out));
gallery.setOnItemClickListener(
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView
<?> parent, View view,
int position,
long id) {
//position从1开始 imageSwitcher.setImageResource(imageid[position
%imageid.length]);
}
});
}
@Override
public View makeView() {
ImageView
imageView=new ImageView(
this);
//设置imageswitch背景色 imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
Adapter
public class GalleryAdapter
extends BaseAdapter{
private Context context;
private int[] imageid;
int mGalleryItemBackground;
public GalleryAdapter(Context context,
int[] imageid) {
this.context
=context;
this.imageid
=imageid;
//图像的显示风格 TypedArray typedArray
= context.obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground
= typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground,
0);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(
int position) {
return imageid[position];
}
@Override
public long getItemId(
int position) {
return position;
}
@Override
public View getView(
int position, View convertView, ViewGroup parent) {
ImageView imageView
=new ImageView(context);
imageView.setImageResource(imageid[position
%imageid.length]);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
res/values/attrs.xml
<?xml version=
"1.0" encoding=
"utf-8"?>
<resources>
<
declare-styleable
name=
"Gallery">
<attr
name=
"android:galleryItemBackground" />
</
declare-styleable>
</resources>
实现gallery无限
循环,点击item,在imageswitch
显示图片
来自为知笔记(Wiz)