使用Picasso时ViewPager在滑动时太慢

4

我正在从我的图库获取所有图片(来自三星Galaxy S5手机)
我可以在ViewPager中查看这些图片。
但是当我开始滑动时,它的速度太慢了。
几秒钟后,应用程序崩溃,日志中没有错误信息。

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);
        }
    }

我的ViewPager添加了适配器。

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

我的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>

我的视图翻页器。

<?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>

您的图像太大了,因此应用程序会出现ANR(应用程序无响应)。在加载之前,请尝试减小图像大小。 - Neo
如何缩小图像的大小,但仍然保持与在手机相册中查看时相同的清晰度? - Charles Galvez
Google上有很多关于“在Android中减小图像大小”的答案。这里有一个在加载之前缩放图像的示例:https://dev59.com/puo6XIcBkEYKwwoYTzNK#823966 - Neo
或者使用Picasso库中的resize函数:http://www.101apps.co.za/index.php/articles/gridview-tutorial-using-the-picasso-library.html - Neo
我使用了Picasso的resize函数,但一些竖向的图片变得像横向的一样小了。 - Charles Galvez
只想补充一下,使用Fresco图像加载库时,有一个setDownsampleEnabled()方法,可以自动对图像进行正确大小的采样。 - Breavyn
2个回答

3
这很可能是由于加载的图像像素数据过多而超出了视图大小。如果您的图像非常大,Picasso默认会将完整尺寸的图像加载到内存中,导致帧率变慢以及OOM异常。
一个快速的解决方法是使用Picasso.with().load().resize(width, height)。它将使Picasso加载图像的缩小版本到内存中。在UI方面,您可以为您的ImageView使用fitCenter缩放类型。
为了更好地理解这个概念,我建议在这个主题上阅读更多有关Google开发者指南的内容: 高效加载大型位图

尝试使用resize,但一些全屏的肖像照片变得非常小了。 - Charles Galvez
尝试在xml中将此属性添加到您的<ImageView />: android:scaleType="fitCenter", 这将缩放您调整大小的图像以适应视图大小。同时,在调整大小时考虑尝试不同的目标宽度/高度。 - bosphere

-1

1. Picasso 将模式设置为 RGB565,设置内存缓存和磁盘缓存。

2. 在 ViewPager 中重复使用视图。

3. ViewPager setPageoffset(1);

4.try:

<application
    android:largeHeap="true"/>

你试过所有三个了吗? - tiny sunlight
我更新了它。尝试观察内存使用情况,你可以在其中获得一些信息。 - tiny sunlight

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接