本文介绍了安卓:使图像库无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是画廊在我的项目中,我已经添加了四象,我希望它是来自右侧和左侧无限。我如何做到这一点?
I'm using a gallery in my project in which I have added four images and I want it to be infinite from both right side and left side. How do I accomplish this?
推荐答案
其主要思想是,在你的 getView
方法,你必须使用
The main idea is that in your getView
method you have to use
position = position % imagesArray.length;
if (position < 0)
position = position + imagesArray.length;
imagesArray 是保存在你的资源文件夹中的图像为例数组:
imagesArray is the array that holds the images in your res folder for exemple:
public class CircularGallery extends Activity {
/** Called when the activity is first created. */
private Integer[] imagesArray = { R.drawable.picture1, R.drawable.picture2, R.drawable.picture3, R.drawable.picture4, R.drawable.picture5, R.drawable.picture6 , R.drawable.picture7 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
Toast.makeText(CircularGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
if (position >= imagesArraylength) {
position = position % mImageIds.length;
}
return position;
}
public long getItemId(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
i.setImageResource(imagesArray[position]);
i.setLayoutParams(new Gallery.LayoutParams(80, 80));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public int checkPosition(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
}}
也有一些开发商已经做了这样的功能,你可以找到他们的博客源
Also some developers have done such a functionality and you can find sources on their blogs
的
http://blog.blundell-apps.com/infinite-scrolling-gallery /
这篇关于安卓:使图像库无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!