简体   繁体   English

Android:使图库图像无限循环

[英]Android: making gallery infinite loop of images

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? 我该如何完成?

The main idea is that in your getView method, you have to use 主要思想是,在您的getView方法中,您必须使用

position = position % imagesArray.length;
if (position < 0)
    position = position + imagesArray.length;

imagesArray is the array that holds the images in your res folder. imagesArray是将图像保存在res文件夹中的数组。 For example: 例如:

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 另外,一些开发人员已经完成了这种功能,您可以在他们的博客上找到资源

如果要设置在右侧显示的图像,只需设置g.setSelection(image)

我的第一个猜测是更改适配器数据,即,如果检测到位于“右边缘”,则获取第一个图像并将其添加到末尾,然后获取第二个图像,依此类推...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM