2011-12-21 49 views
3

是否可以使用url來設置backgroundframelayout通過URL更改Framelayout的背景圖像

例如: 我想使用像this這樣的URL作爲我的framelayout背景。

我真正想要實現的是,當我的活動加載它將從我的域中獲取圖像並將其用作我的framelayout背景圖像。我能夠做到這一點,但我用了一個imageview和一個按鈕。

下面是我使用的代碼:

public class MyAppActivity extends Activity { 

ImageView imView; 
    String imageUrl= "http://www.mydomain.com/resource/images/"; 

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.main); 
Button bt3= (Button)findViewById(R.id.postbutton); 
bt3.setOnClickListener(getImgListener); 

} 
View.OnClickListener getImgListener = new View.OnClickListener() 
{ 

     public void onClick(View view) { 

      downloadFile(imageUrl+"image"+".png"); 
      Log.i("im url",imageUrl+"image"+".png"); 
     } 

}; 


Bitmap bmImg; 
void downloadFile(String fileUrl){ 
     URL myFileUrl =null;   
     try { 
      myFileUrl= new URL(fileUrl); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 

      bmImg = BitmapFactory.decodeStream(is); 
      imView.setImageBitmap(bmImg); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
} 

} 

,這裏是我的XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/rakistaradiobg" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="98dp" 
    android:layout_gravity="bottom|center" 
    android:scaleType="fitXY" 
    android:src="@drawable/btnbg" /> 

<Button 
    android:id="@+id/postbutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="50dp" 
    android:text="Button" /> 

回答

4

東西有興趣的還精彩的教程,(我從來沒有嘗試這一點,但它應該工作)

FrameLayout fm = (FrameLayout)findViewById(R.id.FrameLayout01);  
Drawable drw = ImageOperations(this,url,filename) 
fm.setBackgroundDrawable(drw) 

轉換你的圖片網址繪製對象使用

private Drawable ImageOperations(Context ctx, String url, String saveFilename) { 
     try { 
      InputStream is = (InputStream) this.fetch(url); 
      Drawable d = Drawable.createFromStream(is, saveFilename); 
      return d; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      return null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public Object fetch(String address) throws MalformedURLException,IOException { 
     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    } 

試試這個,讓我知道發生了什麼..

+0

好了,測試它,做工精細,你可以使用此代碼.. – user370305 2011-12-21 07:26:10

+0

它給了我一個錯誤它說:SKImageDecoder :: Factory返回null這是什麼意思? – HeartlessArchangel 2011-12-22 01:45:25

+0

是說沒有找到頁面 – HeartlessArchangel 2011-12-22 05:58:50

0

這裏是你的代碼,你可以從位圖獲得圖像從URL

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); 

和期運用This你可以轉換位圖繪製...

後獲得可繪製在佈局應用像

framelyt.setBackgroundDrawable(d); 
+0

我不能讓這段代碼工作我得到錯誤.. – HeartlessArchangel 2011-12-22 00:08:53

0
private RelativeLayout myRelativeLauout; 

myRelativeLauout = (RelativeLayout)findViewById(R.id.rlBg); 
new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg", 
      "androidfigure").execute(); 


private class LoadBackground extends AsyncTask<String, Void, Drawable> { 

    private String imageUrl , imageName; 

    public LoadBackground(String url, String file_name) { 
     this.imageUrl = url; 
     this.imageName = file_name; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected Drawable doInBackground(String... urls) { 

     try { 
      InputStream is = (InputStream) this.fetch(this.imageUrl); 
      Drawable d = Drawable.createFromStream(is, this.imageName); 
      return d; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      return null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
    private Object fetch(String address) throws MalformedURLException,IOException { 
     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    } 


    @Override 
    protected void onPostExecute(Drawable result) { 
     super.onPostExecute(result); 
     myRelativeLauout.setBackgroundDrawable(result); 
    } 
}