Unable to set image view background image using universal image loader?

我与影子孤独终老i 提交于 2019-12-11 08:24:47

问题


I am trying to set background for the image view. But not got luck to set background using universal image loader.

I am using following code.

DisplayImageOptions options = new DisplayImageOptions.Builder()
                                    .showStubImage(R.drawable.mo_no_image)
                                    .showImageForEmptyUri(R.drawable.mo_no_image)
                                    .cacheInMemory().cacheOnDisc()
                                    .displayer(new RoundedBitmapDisplayer(0))
                                    .imageScaleType(ImageScaleType.EXACTLY).build();
ImageLoader.getInstance().displayImage("url_to_load_image", imgView, options);

By using above code I am able to set image resource which check aspect ratio and using that modifying image size. I want to set downloaded image to the background of image view.


回答1:


I can recommend a different way that works like a charm: Android Query.

You can download that jar file from http://code.google.com/p/android-query/downloads/list

AQuery androidAQuery=new AQuery(this);

As an example:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

It's very fast and accurate, and using this you can find many more features like Animation when loading; getting a bitmap, if needed; etc.

In your case you need to fetch Bitmap, so for it, use below code

androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
                        @Override
                        public void callback(String url, Bitmap object, AjaxStatus status) {
                            super.callback(url, object, status);

                            //You will get Bitmap from object.
                        }

});

Use this bitmap to set your background resource of ImageView using method.




回答2:


Universal Image Loader also provide to use the Background functionality.Please check the below coed for it:-

Here Uri is the path of the folder image OR the url of the image.

imageLoader.loadImage(YOUR_URL, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
   super.onLoadingComplete(imageUri, view, loadedImage);
   layout.setBackgroundDrawable(new BitmapDrawable(loadedImage));
  }
});


来源:https://stackoverflow.com/questions/22322428/unable-to-set-image-view-background-image-using-universal-image-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!