2012-01-12 29 views
8

Làm cách nào để tải có thể vẽ từ InputStream (nội dung, hệ thống tệp) và tự động định kích thước dựa trên độ phân giải màn hình hdpi, mdpi hoặc ldpi?Tải Android có thể vẽ theo chương trình và định lại kích thước

Hình ảnh gốc ở định dạng hdpi, tôi chỉ cần thay đổi kích thước cho mdpi và ldpi.

Có ai biết Android thay đổi kích thước động của các biểu đồ trong/res không?

+0

Ra khỏi tò mò, vì lý do nào để không pre-sizing nó (ví 'mdpi' và' ldpi') và liên kết với nó trong thư mục 'res'? –

+0

Các hình ảnh được tải xuống từ mạng. Tôi có thể thực hiện việc preprocesing trên máy chủ nhưng việc tải xuống sẽ chậm hơn. – peceps

Trả lời

4

Tìm thấy nó:

/** 
    * Loads image from file system. 
    * 
    * @param context the application context 
    * @param filename the filename of the image 
    * @param originalDensity the density of the image, it will be automatically 
    * resized to the device density 
    * @return image drawable or null if the image is not found or IO error occurs 
    */ 
    public static Drawable loadImageFromFilesystem(Context context, String filename, int originalDensity) { 
    Drawable drawable = null; 
    InputStream is = null; 

    // set options to resize the image 
    Options opts = new BitmapFactory.Options(); 
    opts.inDensity = originalDensity; 

    try { 
     is = context.openFileInput(filename); 
     drawable = Drawable.createFromResourceStream(context.getResources(), null, is, filename, opts);   
    } catch (Throwable e) { 
     // handle 
    } finally { 
     if (is != null) { 
     try { 
      is.close(); 
     } catch (Throwable e1) { 
      // ingore 
     } 
     } 
    } 
    return drawable; 
    } 

Sử dụng như thế này:

loadImageFromFilesystem(context, filename, DisplayMetrics.DENSITY_MEDIUM); 
+1

Unfortunatley mã này không hoạt động trên HTC Desire HD và HTC Evo. Xem giải pháp tại đây: http://stackoverflow.com/questions/7747089/exception-in-drawable-createfromresourcestream-htc-only/9195531#9195531 – peceps

1

Nếu bạn muốn hiển thị một hình ảnh nhưng tiếc là hình ảnh này có kích thước lớn, cho phép Ví dụ, bạn muốn hiển thị một hình ảnh ở định dạng 30 x 30, sau đó kiểm tra kích thước của nó nếu kích thước lớn hơn kích thước yêu cầu của bạn, sau đó chia cho số tiền của bạn (30 * 30 ở đây trong trường hợp này) và những gì bạn đã nhận lại và sử dụng để chia lại khu vực hình ảnh .

drawable = this.getResources().getDrawable(R.drawable.pirImg); 
int width = drawable.getIntrinsicWidth(); 
int height = drawable.getIntrinsicHeight(); 
if (width > 30)//means if the size of an image is greater than 30*30 
{ 
    width = drawable.getIntrinsicWidth()/30; 
    height = drawable.getIntrinsicWidth()/30; 
} 

drawable.setBounds(
    0, 0, 
    drawable.getIntrinsicWidth()/width, 
    drawable.getIntrinsicHeight()/height); 

//and now add the modified image in your overlay 
overlayitem[i].setMarker(drawable) 
8

Đây là thoải mái và dễ dàng (các câu trả lời khác không có tác dụng đối với tôi), tìm thấy here:

ImageView iv = (ImageView) findViewById(R.id.imageView); 
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.picture); 
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, newWidth, newHeight, true); 
    iv.setImageBitmap(bMapScaled); 

tài liệu Android có sẵn here.

0

sau khi bạn tải hình ảnh của bạn và đặt nó vào ImageView bạn có thể sử dụng kích thước layoutparamsto hình ảnh của bạn để match_parent

như thế này

android.view.ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); 
layoutParams.width =MATCH_PARENT; 
layoutParams.height =MATCH_PARENT; 
imageView.setLayoutParams(layoutParams); 
Các vấn đề liên quan