如何在Android中动态更改布局背景?

时间:2014-08-06 03:46:48

标签: android xml json layout imageurl

我在启动画面时解析了JSON,其中图像网址被解析为登录屏幕的背景图像。以下是登录屏幕的示例XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_bg"  <!-- I want to change this background dynamically. --> 
android:focusableInTouchMode="true"
android:gravity="center"
tools:context=".activity.LoginActivity" >

<ScrollView
    android:id="@+id/mainScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

 <!-- .... Here is edit text for login inputs and buttuns for singnup and login. -->

    </LinearLayout>
</ScrollView>   
</RelativeLayout>

在上面我已经在RelativeLayout的背景中放置了静态图像,但我想根据图像网址将背景变为可变。

提前致谢。

3 个答案:

答案 0 :(得分:1)

您需要将url图像转换为位图,然后将位图图像转换为Drawable并将其设置为RelativeLayout。

首先将网址图片转换为位图,请参阅示例代码。

Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");

采取RelativeLayout参考

RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);

BitmapDrawable(obj)将Bitmap对象转换为可绘制对象。

Drawable dr = new BitmapDrawable(myImage);
rLayout.setBackgroundDrawable(dr);

Url图片到位图转换方法

 public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

  1. here下载最新的AndroidQuery jar:

  2. 将此jar放到libs文件夹中,右键单击jar和Build Path - &gt;添加到bulid路径。

  3. 如何使用请参阅此示例:

    AQuery androidQuery = new AQuery(this);

    androidQuery.ajax(url.trim(), Bitmap.class, 0, new AjaxCallback<Bitmap>() {
         @Override
         public void callback(String url, Bitmap object, AjaxStatus status) {
             super.callback(url, object, status);
             yourRelativeLayout.setBackground(new BitmapDrawable(object));
         }
    });
    

答案 2 :(得分:0)

我就是这样做的

像这样调用AsyncTask

 new GetImageFromServer().execute(strUrl);  // strUrl is your URL

这是AsyncTask类

public class GetImageFromServer extends AsyncTask<String, Void, Bitmap>
    {


        private Bitmap image;

        protected void onPreExecute(){
            super.onPreExecute();

        }

        @Override
        protected Bitmap doInBackground(String... params){
            try{

                URL urli = new URL(params[0].trim());
                URLConnection ucon = urli.openConnection();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;


                image = BitmapFactory.decodeStream(ucon.getInputStream(),null,options);

            } catch (MalformedURLException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }
            return image;  //<<< return Bitmap
        }
        @Override
        protected void onPostExecute(Bitmap result){ 

             RelativeLayout relative = (RelativeLayout) findViewById(R.id.loginLayout);
             Drawable dr = new BitmapDrawable(result);
             relative.setBackgroundDrawable(dr);

            }


    }