Gallery full screen images

1

Good morning everyone.

I wanted to show a gallery of images that occupy the whole screen, and I saw the option of viewFlipper. The code works perfectly, but it takes a long time to load when the images to be displayed exceed 5.

How can I optimize this? I have read in English stackOverflow that it is better to use a viewpager but I can not find any example that does not implement the tabs. My die is very simple, I just want the photo to appear in full screen and change the image by sliding your finger to the right or left and when you reach the last pass to the first or the first pass to the last, as a circular tail.

What do you recommend? Thank you very much.

    
asked by wiki 29.10.2017 в 20:03
source

1 answer

3

EDITED

Taking into account that the images are stored locally, in the Drawable folder, I would do the following:

  • You create a layout with a view of type ViewPager.
  • You create another layout with a view of type ImageView, to contain the images of the ViewPager.
  • You create an array-type xml with the references to the drawables.
  • You create a class that extends from PagerAdapter and passes the array created in the previous point.
  • Set the viewPager with the adapter.
  • Here is an example of all the steps.

  • file name: activity_main.xml

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent>
    
        <android.support.v4.view.ViewPager
         android:id="@+id/image_gallery"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>
    
    </android.support.constraint.ConstraintLayout>
    
  • file name: element_image.xml

    <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/item_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    </FrameLayout>
    
  • filename of arrays.xml

    <array name="images">
        <item>@drawable/image_1</item>
        <item>@drawable/image_2</item>
        <item>@drawable/image_3</item>
        <item>@drawable/image_4</item>
    </array>
    

  • file name: ImageAdapter.java

    public class ImageAdapter extends PagerAdapter {
    
            private TypedArray images;
            private LayoutInflater layoutInflater;
    
            public ImageAdapter(Context context) {
                images = context.getResources().obtainTypedArray(R.array.images);
                layoutInflater = (LayoutInflater)
                        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
    
            View itemView = layoutInflater.inflate(R.layout.element_image, container, false);
    
            ImageView image = (ImageView) itemView.findViewById(R.id.item_image);
    
            image.setImageDrawable(images.getDrawable(position));
    
            ((ViewGroup) image.getParent()).removeView(image);
    
            container.addView(image);
    
            return image;
        }
    
        @Override
        public int getCount() {
            return images.length();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((ImageView) object);
        }
    }
    
  • file name: MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        ImageAdapter imageAdapter = new ImageAdapter(this);
    
        ViewPager imageGallery = findViewById(R.id.image_gallery);
        imageGallery.setAdapter(imageAdapter);
    }
    

    }

  • I think the code fits better now than you wanted a greeting.

        
    answered by 29.10.2017 / 22:35
    source