本文介绍了使用Facebook的壁画加载位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用壁画取代我的Android应用程序中的毕加索。然而,我不知道如何使用Fresco来简单加载位图。



毕加索我只会执行以下操作。

 位图Poster = Picasso.with(getActivity())
.load(url)
.resize (Utils.convertDpToPixel(WIDTH,HEIGHT))
.centerCrop()
.get();

我一直无法弄清楚如何使用此壁画创建位图。有任何想法吗?

解决方案

如Fresco所说:



如果您对管道的请求是一个解码的图像 - 一个Android Bitmap,您可以利用我们更容易使用的BaseBitmapDataSubscriber:

  dataSource.subscribe (新的BaseBitmapDataSubscriber(){
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap){
//您可以使用有限的方式
//不需要做任何清理
}

@Override
public void onFailureImpl(DataSource dataSource){
//此处不需要清理
}
},
执行者);

您不能将位图分配给不在onNewResultImpl方法范围内的任何变量。





我的代码:

  public void setDataSubscriber(Context context,Uri uri,int width,int height){
DataSubscriber dataSubscriber = new BaseDataSubscriber< CloseableReference< CloseableBitmap>(){
@Override
public void onNewResultImpl(
DataSource< CloseableReference< CloseableBitmap>> dataSource){
if(!dataSource.isFinished()){
return;
}
CloseableReference< CloseableBitmap> imageReference = dataSource.getResult();
if(imageReference!= null){
final CloseableReference< CloseableBitmap> closeableReference = imageReference.clone();
try {
CloseableBitmap closeableBitmap = closeableReference.get();
Bitmap bitmap = closeableBitmap.getUnderlyingBitmap();
if(bitmap!= null&!bitmap.isRecycled()){
//你可以在这里使用位图
}
} finally {
imageReference 。关();
closeableReference.close();
}
}
}
@Override
public void onFailureImpl(DataSource dataSource){
Throwable throwable = dataSource.getFailureCause();
//处理失败
}
};
getBitmap(context,uri,width,height,dataSubscriber);
}

/ **
*
* @param上下文
* @param uri
* @param width
* @param height
* @param dataSubscriber
* /
public void getBitmap(Context context,Uri uri,int width,int height,DataSubscriber dataSubscriber){
ImagePipeline imagePipeline = Fresco。 getImagePipeline();
ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
if(width> 0&&& height> 0){
builder.setResizeOptions(new ResizeOptions(width,height));
}
ImageRequest request = builder.build();
DataSource< CloseableReference< CloseableImage>>
dataSource = imagePipeline.fetchDecodedImage(request,context);
dataSource.subscribe(dataSubscriber,UiThreadExecutorService.getInstance());
}


I'm trying to replace Picasso in my android app with Fresco. However I am unsure of how to simply load a bitmap using Fresco.

With Picasso I would just do the following.

Bitmap poster = Picasso.with(getActivity())
                    .load(url)
                    .resize(Utils.convertDpToPixel(WIDTH,HEIGHT))
                    .centerCrop()
                    .get();

I have been unable to figure out how to create a Bitmap with this Fresco. Any ideas?

解决方案

As Fresco said:

If your request to the pipeline is for a decoded image - an Android Bitmap, you can take advantage of our easier-to-use BaseBitmapDataSubscriber:

dataSource.subscribe(new BaseBitmapDataSubscriber() {
    @Override
    public void onNewResultImpl(@Nullable Bitmap bitmap) {
       // You can use the bitmap in only limited ways
      // No need to do any cleanup.
    }

    @Override
    public void onFailureImpl(DataSource dataSource) {
      // No cleanup required here.
    }
  },
  executor);

You can not assign the bitmap to any variable not in the scope of the onNewResultImpl method.

http://frescolib.org/docs/datasources-datasubscribers.html#_

My code :

public void setDataSubscriber(Context context, Uri uri, int width, int height){
    DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableBitmap>>() {
        @Override
        public void onNewResultImpl(
                DataSource<CloseableReference<CloseableBitmap>> dataSource) {
            if (!dataSource.isFinished()) {
                return;
            }
            CloseableReference<CloseableBitmap> imageReference = dataSource.getResult();
            if (imageReference != null) {
                final CloseableReference<CloseableBitmap> closeableReference = imageReference.clone();
                try {
                    CloseableBitmap closeableBitmap = closeableReference.get();
                    Bitmap bitmap  = closeableBitmap.getUnderlyingBitmap();
                    if(bitmap != null && !bitmap.isRecycled()) {
                        //you can use bitmap here
                    }
                } finally {
                    imageReference.close();
                    closeableReference.close();
                }
            }
        }
        @Override
        public void onFailureImpl(DataSource dataSource) {
            Throwable throwable = dataSource.getFailureCause();
            // handle failure
        }
    };
    getBitmap(context, uri, width, height, dataSubscriber);
}

/**
 *
 * @param context
 * @param uri
 * @param width
 * @param height
 * @param dataSubscriber
 */
public void getBitmap(Context context, Uri uri, int width, int height, DataSubscriber dataSubscriber){
    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
    if(width > 0 && height > 0){
        builder.setResizeOptions(new ResizeOptions(width, height));
    }
    ImageRequest request = builder.build();
    DataSource<CloseableReference<CloseableImage>>
            dataSource = imagePipeline.fetchDecodedImage(request, context);
    dataSource.subscribe(dataSubscriber, UiThreadExecutorService.getInstance());
}

这篇关于使用Facebook的壁画加载位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 15:20