【问题标题】:Resize image to full width and fixed height with Picasso使用毕加索将图像大小调整为全宽和固定高度
【发布时间】:2014-01-16 09:02:58
【问题描述】:

我有一个垂直线性布局,其中一项是使用毕加索加载的ImageView。我需要将图像的宽度提升到整个设备宽度,并显示图像的中心部分,以固定高度(150dp)裁剪。我目前有以下代码:

Picasso.with(getActivity()) 
    .load(imageUrl) 
    .placeholder(R.drawable.placeholder) 
    .error(R.drawable.error) 
    .resize(screenWidth, imageHeight)
    .centerInside() 
    .into(imageView);

我应该在screenWidthimageHeight (=150dp) 中输入哪些值?

【问题讨论】:

    标签: java android imageview image-resizing picasso


    【解决方案1】:

    在某些情况下 fit() 是无用的。在您必须等待宽度和高度测量结束之前。所以你可以使用 globallayoutlistener。例如;

    imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    Picasso.with(getActivity())
                            .load(imageUrl)
                            .placeholder(R.drawable.placeholder)
                            .error(R.drawable.error)
                            .resize(screenWidth, imageHeight)
                            .fit
                            .centerInside()
                            .into(imageView);
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            });
    

    【讨论】:

      【解决方案2】:

      您正在寻找:

      .fit().centerCrop()
      

      这些是什么意思:

      • fit - 等到 ImageView 被测量并调整图像大小以完全匹配其大小。
      • centerCrop - 按照纵横比缩放图像,直到它填满大小。裁剪上下或左右,使其与尺寸完全匹配。

      【讨论】:

      • imageView 的高度应该是多少?我不希望我的 imageView 有一个固定的高度。它应该根据图像的高度而变化。
      • .fit().centerInside() 为我工作,只是使用.centerInside() 会因Center inside requires calling resize with positive width and height. 错误消息而崩溃。
      • @Rock Lee,你需要调整它的大小:' .load(url) .resize(targetWidth, targetHeight) '.......
      • .fit().centerCrop().fit().centerInside() 不起作用。图像未在 imageView 中加载。没有.fit() 图像加载正常。在这两种情况下我都没有使用.resize()
      • 如何在不从任何一侧裁剪的情况下调整其大小并保持外观
      猜你喜欢
      • 2014-03-20
      • 2014-08-17
      • 2015-05-19
      • 2012-08-14
      • 2012-01-24
      • 2013-11-03
      • 1970-01-01
      • 2012-01-13
      相关资源
      最近更新 更多