Android有效地将图像加载到imageview中

Android load image into imageview efficiently(Android有效地将图像加载到imageview中)

本文介绍了Android有效地将图像加载到imageview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个问题:为了避免 OutOfMemoryError,我使用 Picasso 将我的大图像加载到 ImageViews 中:

I have next problem: To avoid OutOfMemoryError i'm loading my big images into ImageViews using Picasso in such way:

public static RequestCreator picasso(final Context context, final int resourceId) {
    return Picasso.with(context)
            .load(resourceId)
            .fit()
            .centerCrop()
            .noFade();
}

在活动或片段中,我正在将图像加载到 ImageView 中

And in activity or fragment, i'm loading image into ImageView

final ImageView backgroundImage = (ImageView) root.findViewById(R.id.background_image);
Util.picasso(getActivity(),R.drawable.background_soulmap)
            .into(backgroundImage);

但是,我有两个问题:

  1. 图片加载有些延迟(但我需要立即加载)
  2. centerCrop() 在某些情况下不是我需要的.

  1. Image loads with some delay (but i need to load it immideatly)
  2. centerCrop() is not what i need in some cases.

如何实现 topCrop() 或 bottomCrop() 之类的东西?我试过 https://github.com/cesards/CropImageView 库,但我在 setFrame() 方法中得到 NullPointerException(getDrawable() 返回 null),因为图像尚未加载.提前致谢!

How can i implement something like topCrop() or bottomCrop()? I've tried https://github.com/cesards/CropImageView library, but i'm getting NullPointerException in setFrame() method (getDrawable() returns null), because image did not load yet. Thanks in advance!

推荐答案

图像加载有一些延迟(但我需要立即加载)

Image loads with some delay (but i need to load it immideatly)

您必须选择:(a) 内存有效延迟或 (b) 即时但可能的 OOM.我说根据经验,我在 Play Store 上的应用程序在 Picasso 处理它时会有一点延迟.这就是它的工作原理.

you have to choose: (a) memory efficient delay or (b) instant but probable OOM. And I said that by experience, the app I have on the Play Store have a little delay while Picasso is processing it. It's just how it works.

centerCrop() 在某些情况下不是我需要的.

centerCrop() is not what i need in some cases.

那么您应该将图像 into() 加载到 目标.目标将在 UI 线程中接收 Drawable,从那里您可以在 ImageView 中设置它(使用 setScaleType)或作为您想要的任何背景.甚至可能使用 InsetDrawable 来调整位置.

then you should load the image into() a Target. The target will receive the Drawable in the UI thread, from there you can set it up inside the ImageView (using the setScaleType) or as a background anyway you want. Maybe even using an InsetDrawable to adjust position.

这篇关于Android有效地将图像加载到imageview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android有效地将图像加载到imageview中

基础教程推荐