Android 浏览页面时速度太慢(使用毕加索)
我正在从我的图库中获取所有图像。(来自三星galaxy s5的图片)Android 浏览页面时速度太慢(使用毕加索),android,android-studio,picasso,Android,Android Studio,Picasso,我正在从我的图库中获取所有图像。(来自三星galaxy s5的图片) 我可以从Viewpager查看图像。 但是当我开始刷卡的时候。太慢了。 几秒钟后,应用程序崩溃,logcat中没有错误 public class Paging extends PagerAdapter { private ArrayList<String> IMAGES = new ArrayList<>(); private Context context;
我可以从Viewpager查看图像。
但是当我开始刷卡的时候。太慢了。
几秒钟后,应用程序崩溃,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);
}
}
我的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>
1.
Picasso
将模式设置为RGB565
,设置内存缓存和磁盘缓存
2.重新使用ViewPager
中的视图
3.ViewPager
setPageoffset(1)
4.尝试:
<application
android:largeHeap="true"/>
这很可能是由于加载了过多的图像像素数据(以适应您的视图大小)。如果您的图像很大,
毕加索
默认情况下会将图像的全尺寸加载到内存中,从而导致帧速率降低以及OOM异常
快速修复方法是使用毕加索.with().load().resize(宽度、高度)
。它将使毕加索能够将图像的下采样版本加载到内存中。在UI端,您可以为ImageView
使用fitCenter
缩放类型
不过,为了更好地理解这个概念,我建议在这里阅读更多关于这个主题的谷歌开发者指南:你的图像太大了,所以应用程序会出现错误。在加载之前尝试减小图像大小。如何减小图像大小,但与在手机图库中查看图像的大小相同?谷歌有很多关于“在android中减小图像大小”的答案。下面是一个在加载前缩放图像的示例:或者在
毕加索
库中使用resize
函数:我使用了毕加索的resizez函数,但是一些肖像图像变小了,就像它的风景一样。你尝试了所有3个吗?我更新了它。尝试观察内存使用情况,你可以从中获得一些信息。尝试使用resize,但是一些全屏的肖像图片变得太小了。请尝试将此属性添加到xml中的
:android:scaleType=“fitCenter”,这样可以放大调整大小的图像以适应视图大小。也考虑在调整大小时尝试不同的目标宽度/高度。
<application
android:largeHeap="true"/>