调整图像大小,以完整的宽度和高度可变毕加索 [英] Resize image to full width and variable height with Picasso

查看:182
本文介绍了调整图像大小,以完整的宽度和高度可变毕加索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可变大小(宽度和高度),它包含一个适配器的ImageView 列表视图。我需要调整画面负载与毕加索到布局的最大宽度和可变高度由图像的纵横比给定

I have a listView with an adapter that contains ImageView of variable size (width and height). I need resize the pictures load with Picasso to the max width of layout and a variable height given by the aspect ratio of the picture.

我已经检查了这个问题: <一href="http://stackoverflow.com/questions/20823249/resize-image-to-full-width-and-fixed-height-with-picasso">Resize图片全宽度和高度固定与毕加索

I have checked this question: Resize image to full width and fixed height with Picasso

飞度()的作品,但我还没有发现什么,以保持画面的纵横比。

The fit() works but I haven't found nothing to keep the aspect ratio of the picture.

这code部分的作品,如果我固定的高度在适配器的布局:

This code partially works if I fixed the height in the layout of the adapter:

Picasso.with(this.context).load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.fit().centerInside()
.into(holder.message_picture);

但它生成列表视图的图片之间的空白,因为照片可能是因为没有那个高度。

But it generates blank spaces between the pictures of the listView because the pictures may be that not have that height.

在此先感谢。

推荐答案

最后,我解决了它做毕加索的改造,这里是片段:

Finally I solved it doing a transformation of Picasso, here is the snippet:

    Transformation transformation = new Transformation() {

        @Override
        public Bitmap transform(Bitmap source) {
            int targetWidth = holder.message_picture.getWidth();

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

        @Override
        public String key() {
            return "transformation" + " desiredWidth";
        }
    };

    mMessage_pic_url = message_pic_url;

    Picasso.with(this.context)
        .load(message_pic_url)
        .error(android.R.drawable.stat_notify_error)
        .transform(transformation)
        .into(holder.message_picture, new Callback() {
            @Override
            public void onSuccess() {
                holder.progressBar_picture.setVisibility(View.GONE);
            }

            @Override
            public void onError() {
                Log.e(LOGTAG, "error");
                holder.progressBar_picture.setVisibility(View.GONE);
            }
    });

这行是定制你想要的宽度:

This line is for customize with your desired width:

int targetWidth = holder.message_picture.getWidth();

此外该剪断包括回调装载隐藏和错误绘制内置的毕加索。

Additionally this snipped include Callback for loading hide and error drawable built-in Picasso.

如果您需要更多的信息来调试任何错误,则必须实现一个自定义的监听器(毕加索生成器)怎么一回事,因为在的onError 回调信息是空。你只知道,有对用户界面行为的错误。

If you need more information to debug any error, you MUST implement a custom listener (Picasso builder) beacuse the onError Callback information is "null". You only know that there is an error for UI behavior.

我希望这可以帮助别人节省很多时间。

I hope this helps someone to save many hours.

这篇关于调整图像大小,以完整的宽度和高度可变毕加索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆