【问题标题】:Parsing online images into ImageView将在线图片解析成ImageView
【发布时间】:2015-07-28 13:56:52
【问题描述】:

我正在尝试使用 JSON 将在线图像解析为 ImageView,为此我正在使用 Picasso 库

但由于图像尺寸大,宽度:4608 像素,高度:2592 像素,我无法将在线图像导入 ImageView

    Picasso.with(context)
    .load(imageURL)
    .noFade()
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.ic_launcher)
    .into(viewHolder.imageView);

注意:-我正在成功地将小尺寸图像放入 ImageView

【问题讨论】:

标签: android json android-imageview


【解决方案1】:

您可以应用自定义转换。

我使用下面的方法来缩放图像并保持纵横比

Transformation transformation = new Transformation() {
@Override 
public Bitmap transform(Bitmap source) {

            int targetWidth = width;
            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            int targetHeight = (int) (targetWidth  * aspectRatio);

            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);

            if (result != source) {
                // Same bitmap is returned if sizes are the same
                source.recycle();
            }

            return result;

        }

然后

Picasso.with(context).
          load("your url").transform(transformation)
          .into(holder.iv)

看看图片变换@

http://square.github.io/picasso/

根据您的要求添加自定义转换

你也可以看看@

https://futurestud.io/blog/picasso-image-resizing-scaling-and-fit/

【讨论】:

  • 我已经尝试过您的代码,请在此处查看:paste.ofcode.org/nXW6SLQUufKjJD2iewhLXz 但得到:此行有多个标记 - 语法错误,插入“;”完成 FieldDeclaration - 语法错误,插入“}”以完成 ClassBody
  • String imageURL = filesArrayList.get(position).getfilename();; ?为什么要加倍;而且您还没有使用在 picaso 加载程序中创建的转换
  • @Sophie 看看大括号的闭合和打开。我在 getView 本身进行了改造
  • @DhinakaranThennarasu 哦,对不起,这是拼写错误,我也在毕加索加载器中使用了转换,检查:paste.ofcode.org/rKpU9RndKuBRYdgfAYVjdr
  • @Raghunandan ok 在 getView() 中移动了转换代码,但仍然得到:宽度无法解析为变量
【解决方案2】:

使用这个

Picasso.with(context)
    .load(imageURL)
    .noFade()
    .fit()
    .centerCrop()
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.ic_launcher)
    .into(viewHolder.imageView);

【讨论】:

  • 得到:无法在原始类型 void 上调用 fit()
  • .fit().centerCrop() 将出现在 into() 方法之前
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多