ViewPager is too slow to scroll (using Picasso)

I get all the images from my gallery (pictures from Samsung galaxy s5)
I can view images from Viewpager.
But when I start swiping. Its too slow.
And after a few seconds, the application crashed without errors in logcat.

public class Paging extends PagerAdapter {
        private ArrayList<String> IMAGES = new ArrayList<>();
        private Context context;
        private LayoutInflater layoutInflater;
        public Paging(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return MainActivity.IMAGES.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == (LinearLayout) object);
        }

        @Override
        public View instantiateItem(View container, int position) {
            IMAGES = (ArrayList<String>) MainActivity.IMAGES.clone();



            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View item_view = layoutInflater.inflate(R.layout.imagepager_layout, (ViewGroup) container, false);
            ImageView imageView = (ImageView) item_view.findViewById(R.id.img);


            Picasso.with(context)
                    .load(IMAGES.get(position))
                    .placeholder(R.drawable.plusbtn)
                    .into(imageView);

        ((ViewGroup) container).addView(item_view);

            return item_view;


        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((LinearLayout) object);
        }
    }

My adapter is adding viewPager.

 viewPager = (ViewPager)findViewById(R.id.view_pager);
 adap = new Paging(MainActivity.this);
 viewPager.setAdapter(adap);

My XML ..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center" />
</LinearLayout>

My player.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="vertical"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
</LinearLayout>
+4
source share
2 answers

, , , ( ). , Picasso , , OOM.

Picasso.with().load().resize(width, height). Picasso . fitCenter ImageView.

, Google :

+3

1. Picasso RGB565, .

2. ViewPager.

3. ViewPager setPageoffset (1);

4.try:

<application
    android:largeHeap="true"/>
-1

Source: https://habr.com/ru/post/1614984/


All Articles