使用通用图像加载器下载的图像设置RelativeLayout的背景

时间:2014-01-06 14:54:07

标签: android background relativelayout android-drawable universal-image-loader

我想将图像(从互联网下载)设置为具有透明属性的相对布局背景。

以下是我在layout.xml文件中的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/mainLayout"
    android:alpha="0.75" >
....
</RelativeLayout>

这是我想从活动中做的事情:

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeImageLoader();
        String imageUri = "http://www.laederach.com/fileadmin/img/filialen/zermatt/zermattBg.jpg";

        Bitmap bitmap = imageLoader.loadImageSync(imageUri1);


        relativeLayout = (RelativeLayout)findViewById(R.id.mainLayout);
        Drawable d =new BitmapDrawable(getResources(),bitmap);
        relativeLayout.setBackground(new BitmapDrawable(bitmap));
    }

    private void initializeImageLoader() {
        // Create global configuration and initialize ImageLoader with this configuration
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .build();
        ImageLoader.getInstance().init(config);

    }

但它不起作用。知道为什么这不起作用或我错过了什么?感谢。

2 个答案:

答案 0 :(得分:0)

使用此库: -

https://github.com/nostra13/Android-Universal-Image-Loader/

其中有一个样本,它是非常好的库。 以下是使用它的基本思路: - 有关更多信息,请参阅库样本: -

ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .showImageOnLoading(R.drawable.ic_stub)
                        .showImageForEmptyUri(R.drawable.ic_empty)
                        .showImageOnFail(R.drawable.ic_error)
                        .cacheInMemory(true)
                        .cacheOnDisc(true)
                        .considerExifParams(true)
                        .displayer(new RoundedBitmapDisplayer(20))
                        .build();

imageview = (ImageView)findViewById(R.id.imageView1);
imageLoader.displayImage(imageUri[position], imageView, options, animateFirstListener);

//Inner class to animate pics to display
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        if (loadedImage != null) {
                                ImageView imageView = (ImageView) view;
                                boolean firstDisplay = !displayedImages.contains(imageUri);
                                if (firstDisplay) {
                                        FadeInBitmapDisplayer.animate(imageView, 500);
                                        displayedImages.add(imageUri);
                                }
                        }
                }
        }

答案 1 :(得分:0)

发生这种情况可能是因为您尝试在主线程上使用网络连接。尝试使用这样的东西:

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     initializeImageLoader();
     String imageUri = "http://www.laederach.com/fileadmin/img/filialen/zermatt/zermattBg.jpg";

     new AsyncTask <Void, Void, Void> () {

         @Override
         protected Void doInBackground(Void...params) {
             Bitmap bitmap = imageLoader.loadImageSync(imageUri1);

             return null;
         }

         protected void onPostExecute(Void result) {

             relativeLayout = (RelativeLayout) findViewById(R.id.mainLayout);
             Drawable d = new BitmapDrawable(getResources(), bitmap);
             relativeLayout.setBackground(new BitmapDrawable(bitmap));

         };
     }.execute();
 }

 private void initializeImageLoader() {
     // Create global configuration and initialize ImageLoader with this configuration
     ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
         .build();
     ImageLoader.getInstance().init(config);

 }