2011-12-21 3 views
0

가 어떻게 소스가 데이터를 동적으로 설정할 수 있습니다 이미지 뷰에 정적 이미지동적으로 고해상도 폴더에없는 그

<ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.67" 
     android:src="@drawable/static_image" /> 

을 설정할 수 있습니다 외부 자원 안드로이드 이미지 뷰를 채우는?

즉, 내 응용 프로그램에는 화면에 아이콘이 있지만 아이콘의 실제 이미지는 외부 서버에서 다운로드되어 동적으로 변경할 수 있습니다. 다운로드시 원하는 이미지로 ImageView를 어떻게 업데이트합니까?

Image selectedImage = //get from server 

myImageView.setImage(selectedImage); 
+0

사용 AsyncTask를 다운로드를 한 후 새로운 이미지 UR 이미지 뷰를 업데이트 – ingsaurabh

답변

2

우르의 문제는 명확하지 않다. u는 단지 싶어 이미지보기로 설정 한 이미지 (즉, 어떤 URL에),

Bitmap bmp=getBitmapFromURL(ur url here); 
imgview.setImageBitmap(bmp); 

을 쓰는이 기능하는 경우 :

public static Bitmap getBitmapFromURL(String src) { 
     try { 

      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url 
        .openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap mybitmap = BitmapFactory.decodeStream(input); 

      return mybitmap; 

     } catch (Exception ex) { 

      return null; 
     } 
1
yourImageView.setImageBitmap(bitmap); 

서버에서 비트 맵을 얻을 :

내가 좋아하는 귀하의 질문에 뭔가를 이해 당으로
public static Bitmap loadBitmap(String url) { 
    Bitmap bitmap = null; 
    InputStream in = null; 
    BufferedOutputStream out = null; 

    try { 
     in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); 

     final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); 
     out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE); 
     copy(in, out); 
     out.flush(); 

     final byte[] data = dataStream.toByteArray(); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     //options.inSampleSize = 1; 

     bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options); 
    } catch (IOException e) { 
     Log.e(TAG, "Could not load Bitmap from: " + url); 
    } finally { 
     closeStream(in); 
     closeStream(out); 
    } 

    return bitmap; 
} 
1

,

ImageView selectedImage; 
selectedImage = (ImageView)findViewById(R.id.imageView1); 
Bitmap bmImg; 

downloadFile(imageUrl); 

그리고이 downloadFile() 방법이다 나는 기능적으로 뭔가를 원하는 ...

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); 
       selectedImage.setImageBitmap(bmImg); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 
0

사용자 정의보기를 만들고 onDraw()를 오버라이드해야합니다. 거기에서 드로잉 캔버스가 제공되며 원하는대로 할 수 있습니다. 따라서 동적 이미지를 비트 맵으로 변환 할 수 있다고 가정하면 Canvas에서 사용할 수있는 수많은 drawBitmap() 메서드가 있습니다.

onMeasure()를 오버라이드해야합니다. 그러면 뷰에서 레이아웃 관리자에게 필요한 공간을 알릴 수 있습니다. 여기에 좋은 정보가 많이있다

: http://developer.android.com/guide/topics/ui/custom-components.html

관련 문제