Gallery的小技巧

Gallery一般用户显示图像列表,与GridView不同,他只能水平显示一行,而且支持水平华东效果。但是本身并不支持循环显示图像,也就是说,当显示到最后一个图像时,图像类表就不在想左移动了。要实现这个效果也不难,我们知道Gallery的图像来自于他的adapter,于是我们可以在BaseAdapter类中的getCount中返回一个很大数,因为getView方法的调用与getCount方法的返回值有关,getView方法的position参数绝对不会大于n-1,所以我们令getCount方法返回一个Inter.MAX_VALUE,这样系统会认为这个adapter对象中有非常多的View对象。

尽管如此,但实际应用出问题了,因为position可能会大于实际的View数目,所以我们在实际应用的时候,如果是用一个数组存储资源的,那下标可以投机取巧,将position取数组长度的余。

现在附上书上的一个例子:

Main.java

package net.blogjava.mobile; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; 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.AdapterView.OnItemSelectedListener; import android.widget.Gallery.LayoutParams; import android.widget.ViewSwitcher.ViewFactory; public class Main extends Activity implements OnItemSelectedListener, ViewFactory { private Gallery gallery; private ImageSwitcher imageSwitcher; private ImageAdapter imageAdapter; //图像自己搞 private int[] resIds = new int[] { R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4, R.drawable.item5, R.drawable.item6, R.drawable.item7, R.drawable.item8, R.drawable.item9, R.drawable.item10, R.drawable.item11, R.drawable.item12, R.drawable.item13, R.drawable.item14, R.drawable.item15 }; private int count = 3; public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; public ImageAdapter(Context context) { mContext = context; TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery); mGalleryItemBackground = typedArray.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0); } public int getCount() { return Integer.MAX_VALUE; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(resIds[position % resIds.length]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(136, 88)); imageView.setBackgroundResource(mGalleryItemBackground); return imageView; } } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { imageSwitcher.setImageResource(resIds[position % resIds.length]); } @Override public void onNothingSelected(AdapterView<?> parent) { } @Override public View makeView() { ImageView imageView = new ImageView(this); imageView.setBackgroundColor(0xFF000000); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return imageView; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gallery = (Gallery) findViewById(R.id.gallery); imageAdapter = new ImageAdapter(this); gallery.setAdapter(imageAdapter); gallery.setOnItemSelectedListener(this); imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher); imageSwitcher.setFactory(this); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); } } 

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_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" /> <ImageSwitcher android:id="@+id/imageswitcher" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" /> </LinearLayout>  

你可能感兴趣的:(android,object,layout,存储,Class,encoding)