2011-10-04 43 views
16

tôi muốn thiết lập nền tuyến tính Layout động theo cách sau:Set tuyến tính bố trí nền động

  1. Fetch hình ảnh từ url web thông qua phân tích cú pháp XML và sau đó lưu trữ hình ảnh đó vào thẻ sd.

  2. Bây giờ hình ảnh được lưu vào thẻ sd.

  3. Đặt hình ảnh đó làm nền bố cục tuyến tính trong ứng dụng.

Bây giờ tôi bị kẹt ở bước thứ ba. Có ai giúp được không?

+0

http://developerlife.com/tutorials/?p=312 –

Trả lời

44

Sử dụng này:

Bitmap bmImg = BitmapFactory.decodeStream(is); 
BitmapDrawable background = new BitmapDrawable(bmImg); 
linearLayout.setBackgroundDrawable(background); 

Ngoài ra kiểm tra này: How to convert a Bitmap to Drawable in android?

+0

không lưu hình ảnh của tôi vào mảng byte.am lưu nó như InputStream là = conn.getInputStream(); Bitmap bmImg = BitmapFactory.decodeStream (is); FileOutputStream fos = new FileOutputStream (outputFile); bmImg.compress (CompressFormat.JPEG, 0, fos); fos.flush(); fos.close(); – Kanika

+0

Trong trường hợp này, hãy sử dụng: 'bmImg = BitmapFactory.decodeStream (is); ' ' BitmaBitmapDrawable background = new BitmapDrawable (bmImg); ' ' linearLayout.setBackgroundDrawable (nền); ' – Deimos

+0

không có nó không hoạt động, nó vẫn hiển thị cho tôi ngoại lệ con trỏ null – Kanika

2

Một cách dễ dàng hơn:

BitmapDrawable d = new BitmapDrawable("/sdcard/data/image.jpg"); 
linearLayout.setBackgroundDrawable(d); 
+0

Ngoại lệ con trỏ không có ở đó với mã ở trên – Kanika

-2

Bạn cũng có thể thiết lập hình ảnh từ thư mục drawable.

yourView.setBackgroundResource(R.drawable.FILENAME); 

Tập hợp FILENAME làm hình nền.

4

Tôi đã làm theo cách này:

private RelativeLayout relativeLayout; 

onCreate:

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

new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg", 
      "androidfigure").execute(); 

AsyncTask để tải hình ảnh trong nền:

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); 
     relativeLayout.setBackgroundDrawable(result); 
    } 
} 

Hy vọng điều này sẽ giúp bạn.

3

API đang bị phản đối, bạn có thể sử dụng mã dưới đây

BitmapDrawable background = new BitmapDrawable(getResources(), bitmapImage); 
linearLayout.setBackground(background); 
0

Cố gắng sử dụng này:

Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.img); 
BitmapDrawable bmpBackground = new BitmapDrawable(getResources(), bmpOriginal) 
0

Sử dụng @ Deimos của câu trả lời, nhưng như thế này kể từ khi một số phương pháp không được tán thành tại

Bitmap bmImg = BitmapFactory.decodeStream(is); 
BitmapDrawable background = new BitmapDrawable(context.getResources(), bmImg); 
linearLayout.setBackground(background); 
Các vấn đề liên quan