一、知识点
gallery与spinner同时继承AbsSpinner 一个横向展示,一个纵向展示 一个供展示,一个供选择
二、Gallery+ImageSwitcher
<!-- 定义一个ImageSwitcher组件 -->
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="320dp"
android:layout_height="320dp"
/>
<!-- 定义一个Gallery组件 -->
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:unselectedAlpha="0.6"
android:spacing="3pt"
/>
public class GallerySwitcherActivity extends Activity {
/** Called when the activity is first created. */
private ImageSwitcher switcher;
private Gallery gallery;
private int images[] = new int[] {
R.drawable.img1, R.drawable.img2, R.drawable.img3 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
switcher = (ImageSwitcher) findViewById(R.id.switcher);
switcher.setFactory(new ViewFactory() {
// 直接重写接口
@Override
public View makeView() {
ImageView imageView = new ImageView(GallerySwitcherActivity.this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
});
// 设置动画效果
switcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(1);//默认选择第二个
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
switcher.setImageResource(images[position%images.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
class ImageAdapter extends BaseAdapter {
private Context context;
int mGalleryItemBackground;
public ImageAdapter(Context context) {
this.context = context;
TypedArray typedArray=obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground= typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
//return Integer.MAX_VALUE;//无线循环
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
//设置图片资源
imageView.setImageResource(images[position %images.length]);
//允许调整边框 自适应
imageView.setAdjustViewBounds(true);
//设置布局
imageView.setLayoutParams(new Gallery.LayoutParams(75,100));
//设置Gallery组件的背景风格
imageView.setBackgroundColor(mGalleryItemBackground);
return imageView;
}
}
}
其实这里包含了imageswitcher的配置+gallery实现适配器的方法
最后是在value/attrs.xml中的配置
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
![](https://img-my.csdn.net/uploads/201212/27/1356595506_1563.png)
三、参考优化
代码下载,点击这里
如果您不是看的很明白,可以参考这篇,讲的非常详细,不愧是出书的人。点击参考
如果您想做进一步的优化,也可以参考这里,点击参考