Android Picasso:不在片段中加载 [英] Android Picasso: Not Loading in Fragment

查看:91
本文介绍了Android Picasso:不在片段中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让毕加索尝试一些更方便的图像内存管理.我只是试图在我的片段中实现它,但是我似乎无法使其工作.

I am giving Picasso a try for some easier memory management with images. I have just been trying to implement it within my fragment however I can't seem to get it to work.

mainLayout.setBackground(new BitmapDrawable(getResources(), Picasso.with(mainLayout.getContext()).load(R.drawable.background2).get()));

其中mainLayout是LinearLayout.我也尝试过这个:

Where mainLayout is a LinearLayout. I also tried this:

Picasso.with(getActivity().getApplicationContext()).load(R.drawable.background2).into(imageView1);

我已经尝试过Picasso.with(this)...,但这根本不起作用.

I have tried Picasso.with(this)... but this doesn't work at all.

我不断收到以下异常:

java.lang.IllegalStateException: Method call should not happen from the main thread.
at com.squareup.picasso.Utils.checkNotMain(Utils.java:71)
at com.squareup.picasso.RequestCreator.get(RequestCreator.java:206)
at ... 

我叫它的地方.

任何人都经历过或知道如何正确使用碎片吗?

Anyone experience this or know how to use this properly with fragments?

推荐答案

异常消息清楚地表明了这一点. .get()消息不是异步的,因此将在主线程上完成工作(网络,读取文件等),您应尽可能避免这种情况.

The exception message states it pretty clear. The .get() message is not async, hence will do the work (network, read file,... whatever) on the main thread, which you should avoid whenever possible.

不过,您的将图像设置为imageView的代码对我来说似乎是正确的.

Your code for setting the image to an imageView seems correct to me, though.

我认为也可以对mainLayout进行同样的操作(Picasso随后会自动将drawable/Bitmap设置为背景). 如果我错了,请查看Picasso随附的Target.class.您可以将映像加载到目标中,该目标必须提供成功和错误的处理程序.在成功处理程序中,加载位图后,您将获得该位图并将其设置为背景.

I think it is also possible to do the same with the mainLayout (Picasso will then set the drawable/Bitmap to the background automatically). If I'm wrong here, have a look at the Target.class which comes with Picasso. You could load the image into a target, which must provide handlers for success and error. Within the success handler you will get the bitmap once it is loaded and can set it to your bakground.

有一些解决方案,可能适合您的情况.

There are a few solutions, which might work in your case.

使用Picasso提供的get()方法时,加载将在您当前正在使用的线程中进行,这在源代码中有明确说明: https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/RequestCreator.java

When using the get() Method provided by Picasso the loading will take place in the thread you are currently working in, which is clearly stated in the sources: https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/RequestCreator.java

/**同步完成此请求.一定不能从主线程中调用. */

/** Synchronously fulfill this request. Must not be called from the main thread. */

公共位图get()引发IOException {[...]}

public Bitmap get() throws IOException {[...]}

在您的情况下,我将使用Target接口,该接口在加载图像时提供回调.

In your case I would use the Target interface, which provides callbacks when the image is loaded.

Picasso.with(getActivity()).load(R.drawable.background2).into(new Target(){

  @Override
  public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
     mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
  }

  @Override
  public void onBitmapFailed(final Drawable errorDrawable) {
      Log.d("TAG", "FAILED");
  }

  @Override
  public void onPrepareLoad(final Drawable placeHolderDrawable) {
      Log.d("TAG", "Prepare Load");
  }      
});

在这里使用内部资源时,为什么不只是执行以下操作?

As you are using an internal resource here, why don't you just do the following?

mainLayout.setBackgroundResource(R.drawable.background2);

希望这会有所帮助.

致谢

这篇关于Android Picasso:不在片段中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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