简体   繁体   English

Android Gallery控件-需要无限循环的图像

[英]Android Gallery control - need infinite loop of images

Without starting from the middle of the stack of images how do I rotate them in the infinite loop left to right and right to left? 不从图像堆栈的中间开始,如何在无限循环中从左到右和从右到左旋转它们? I tried setSelection(position) but for some reason I get that method called few times and inconsistently. 我尝试了setSelection(position),但是由于某种原因,我多次调用该方法,而且不一致。 My images increment has to be saved int he app state so it makes it a bit more complicated. 我的图像增量必须保存在应用程序状态中,这样会使它变得更加复杂。

@Override
    public void setSelection(int position){

        int sectionPos = getCurrentPositionFromState();

        if (sectionPos == (this._images - 1)){  
            setCurrentPositionFromState(0);
            sectionPos = 0;
        }
        else {
            setCurrentPositionInState(sectionPos +1);
        }
        if (sectionPos <= (this._images - 1) ){
            super.setSelection(sectionPos);
        }
    }

gallery.setOnItemSelectedListener(new OnItemSelectedListener() {     
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {

                gallery.setSelection(position);


            @Override
            public void onNothingSelected(AdapterView parent) {


            }
        });

I should also mention that I have an onFling() overriden like so: 我还应该提到我有一个onFling()重写,像这样:

@Override
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
         return super.onFling(e1, e2, 0, velocityY);
       }

There are a few ways to go about it, but in all cases, it's never technically "infinite." 有几种解决方法,但是在所有情况下,从技术上讲,它从来都不是“无限的”。 I got the basis of mine from snooping around for a while. 我偷偷摸摸了一段时间,有了我的基础。

First we need to put a gallery into the xml file in the layout. 首先,我们需要在布局的xml文件中放置一个图库。 So after creating the file put in a little snippet like this: 因此,在创建文件后,请放入如下代码段:

<!-- Gallery To show images Gallery  --> 
< Gallery
    android:id="@+id/galleryView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="15dip"/>

Now lets set up a Vector of different images so we can loop through. 现在让我们设置不同图像的矢量,以便我们循环浏览。 We will be getting these images from our resources, but if you want to get them from somewhere else then just insert that instead. 我们将从资源中获取这些图像,但是如果您想从其他位置获取它们,则只需插入即可。 We are going to insert this into onCreate(). 我们将其插入到onCreate()中。 This will allow us to have a vector of id's that we can reference when getting images to put into the gallery later: 这将使我们拥有一个id的向量,以便稍后将图像放入图库时可以参考该向量:

public Vector<Integer> mPhotoVector = new Vector<Integer>();

public void setPhotos() {
    for(as many photos as you want){
        int imageResource = getResources().getIdentifier("imageNAme", "drawable", getPackageName());
        mPhotoVector.add(imageResource);
    }

Where galView is my gallery view from the layout. galView是布局中我的画廊视图。 And as we finish up this project we need to call the gallery view from the xml and initialize it to a gallery variable we will have in the code. 在完成该项目时,我们需要从xml调用Gallery视图,并将其初始化为代码中将包含的gallery变量。

LoopingGalleryAdapter adapter;
Gallery galView;

adapter = new LoopingGalleryAdapter(this, mPhotoVector);
galView = (LoopingGallery)mLayoutView.findViewById(R.id.galleryView);
galView.setAdapter(adapter);
galView.setSelection((galView.getCount() / 2));

The setSelection() will have us looking at the middle of the gallery, this makes it appear to be "infinite" since there are now 1073741823 elements to each side. setSelection()将让我们看一下画廊的中间部分,这使得它似乎是“无限的”,因为现在每一侧都有1073741823元素。

Next we need to create the adapter. 接下来,我们需要创建适配器。 The basis is to make the largest gallery you can, so next we just add in our own little getCount method. 基础是使最大的画廊成为可能,所以接下来我们仅添加我们自己的小getCount方法。 This will create a gallery that is too large for the user to (plausibly) scroll to the end of. 这将创建一个画廊,该画廊太大了,用户无法(可能)滚动到末尾。

Lastly on the adapter we have the meat of the project which requires us to set where our position is. 最后,在适配器上,我们有项目的重点,这要求我们设置位置。 This is the key. 这是关键。 Once the position is set to the middle-ish of the gallery, it seems as though you have an infinite loop of images on either side. 将位置设置为图库的中间位置后,似乎两侧都有无限循环的图像。

I started mine off a little like this: 我像这样开始我的工作:

public class LoopingGalleryAdapter extends BaseAdapter {

     private ImageView iv;
     private Context mContext;
     public PhotoVector mPhotoVector = null;
     int mGalleryItemBackground;

      public LoopingGalleryAdapter(Context c, PhotoVector aVector) {
          this.mContext = c;
          mPhotoVector = aVector;
      }

      public int getCount() {
        return Integer.MAX_VALUE;
      }

      @Override
      public Object getItem(int position) {
          return position;
      }

      public ImageView getImage(){
        return iv;
      }

      @Override
      public long getItemId(int position) {
          return position;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          iv = new ImageView(mContext);
          private final int middle = 1073741823; //this is the middle index of the gallery in galView 

          /************************************************************************
          *if you have a vector/array/arrayList of photos you would like to display
          ************************************************************************/
          if((position - middle) >= 0) { relativePosition = (position-middle) % mPhotoVector.size(); }
          else { relativePosition = mPhotoVector.size() - (Math.abs(position - middle) % mPhotoVector.size()); }

          Drawable draw = mContext.getResources().getDrawable(mPhotoVector.get(relativePosistion));

          /*********************************************
          *otherwise you can just insert a photo like so
          *********************************************/
          Drawable draw = mContext.getResources().getDrawable(R.drawable.what_you_want);

          iv.setImageDrawable(draw);
          return iv;
      }
}

And we are now done! 现在我们完成了!

Additionally, one of the tricks I have found quite useful, if not necessary when getting images from the internet is to keep a count of how many times you have gone through getView() and after 10-20 times clear your cache so you don't throw a OutOfMemoryError by doing: 此外,我发现的一项技巧很有用,如果不需要从互联网上获取图像,则无需计数,它是保持计数您通过getView()次数,并在10-20次之后清除缓存,因此您无需通过执行以下操作抛出OutOfMemoryError

   if(counter >= 20){
       galView.destroyDrawingCache();
       counter = 0;
   }

Or have getView() throw a OutOfMemoryError , catch it and return an empty ImageView (iv) and then clear then call galView.destroyDrawingCache(); 或者让getView()抛出OutOfMemoryError ,捕获它并返回一个空的ImageView(iv),然后清除然后调用galView.destroyDrawingCache();

A shameless self plug, just wrote an Infinite Scrolling Gallery tutorial: 一个无耻的自我插件,刚刚写了《无限滚动画廊》教程:

http://blog.blundellapps.com/infinite-scrolling-gallery/ http://blog.blundellapps.com/infinite-scrolling-gallery/

Source code can be downloaded also, you choose the image size. 您也可以下载源代码,选择图像大小。

You can use images on your SD card or images in your /resources/drawable directory. 您可以使用SD卡上的图像或/ resources / drawable目录中的图像。

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

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