使用毕加索将图像分成可绘制 [英] Use Picasso to Place Image Into Drawable

查看:137
本文介绍了使用毕加索将图像分成可绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用从毕加索广场从URL拉JPG,然后附加到一个EditText。原因毕加索是,它是在执行非常轻巧。正如我使用的是占位符的ImageView,其中毕加索将导入从提供的URL的图像,然后我将其转换成ImageView的一个可绘制可见。这同样为ImageGetter。不过,我使用下面的配置时收到空指针错误。 (注意:简单地使用来自应用程序资源的可绘制到位低于的drawImage'变量的时候,这样的配置工作,但我试图把它扩大到从URL拉资源)。

I am attempting to use Picasso from Square to pull a jpg from a URL and then append to an EditText. The reason for Picasso is that it's very lightweight in the implementation. As can be seen I am using a placeholder ImageView, whereby Picasso will import the image from the URL provided, and then I convert that ImageView into a Drawable. The same goes for the ImageGetter. However I receive null pointer error when using the configuration below. (Note when simply using a drawable from the application resources in place of the 'drawImage' variable below, this configuration works, but I'm trying to extend it to pull resources from a URL).

有什么出位吗?或者更有效的方式做到这一点?

Is there something out of place here? Or a more efficient way to do this?

方法追加到的EditText:

Method to append to EditText:

public void appendToMessageHistory(String username, String message) {
        if (username != null && message != null) {

            ImageView image = new ImageView(getApplicationContext());

            Picasso.with(getBaseContext()).load("http://localhost:3000/uploads/campaign/image/2/2.jpg").into(image);
            Drawable drawImage = image.getDrawable();

            messageHistoryText.append(Html.fromHtml("<b>" + username + ":"
                    + "</b>" + "<br>"));
            messageHistoryText.append(Html.fromHtml(message + "<hr>" + "<br>")
                    + System.getProperty("line.separator") + "");

            messageHistoryText.append(Html.fromHtml("<img src = '"
            + drawImage + "'/>",
            imageGetter, null));

        }
    }

该ImageGetter:

The ImageGetter:

ImageGetter imageGetter = new ImageGetter() {

@Override
public Drawable getDrawable(String source) {
    ImageView image = new ImageView(getApplicationContext());

    Picasso.with(getBaseContext()).load("http://localhost:3000/uploads/campaign/image/2/2.jpg").into(image);
    Drawable drawImage = image.getDrawable();

    drawImage.setBounds(0, 0, drawImage.getIntrinsicHeight(), drawImage.getIntrinsicWidth());
    return drawImage;


}

};

推荐答案

有是你错过毕加索的东西。你可以在进入方法一样设置annonymous目标类,并设置位图您有任何对象:

There is something you miss about Picasso. You can set an annonymous Target class in inTo method like and set bitmap to any object you have :

Picasso.with(getBaseContext()).load("your url").into(new Target() {

                @Override
                public void onPrepareLoad(Drawable arg0) {


                }

                @Override
                public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
                    // TODO Create your drawable from bitmap and append where you like.

                }

                @Override
                public void onBitmapFailed(Drawable arg0) {


                }
            });

修改
因此,这是如何做到这一点:

EDIT so this is how you do that:

public void appendToMessageHistory(String username, String message) {
        if (username != null && message != null) {

            ImageView image = new ImageView(getApplicationContext());

            Picasso.with(getBaseContext()).load("image url").into(new Target() {

                    @Override
                    public void onPrepareLoad(Drawable arg0) {


                    }

                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
                    Drawable drawImage = new BitmapDrawable(getBaseContext().getResources(),bitmap);
                     messageHistoryText.append(Html.fromHtml("<b>" + username + ":"
                    + "</b>" + "<br>"));
                    messageHistoryText.append(Html.fromHtml(message + "<hr>" + "<br>")
                    + System.getProperty("line.separator") + "");

                    messageHistoryText.append(Html.fromHtml("<img src = '"
                    + drawImage + "'/>",
                    imageGetter, null));    
                    }

                    @Override
                    public void onBitmapFailed(Drawable arg0) {


                    }
                });




        }
    }

这篇关于使用毕加索将图像分成可绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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