2012-01-12 16 views
8

nasıl InputStream dan çekilebilir (varlık, dosya sistemi) yüklemek ve ekran çözünürlüğü hdpi, MDPI veya LDPI göre dinamik olarak yeniden boyutlandırmak olabilir? Android yük çekilebilir programlama ve yeniden boyutlandırmak o

orijinal resim

Ben sadece MDPI ve LDPI için boyutlandırma ihtiyaç hdpi içindedir.

herkes Android/res drawables dinamik boyutlandırma yapar biliyor mu?

+0

gibi

match_parent resminizi yüklemek ve layoutparamsto boyutunu kullanabilirsiniz ImageView ayarlayın sonra, herhangi bir nedenle değil için (bunu önceden boyutlandırma 'mdpi' ve' ldpi'' ve 'res' dizininde ona bağlanıyor? –

+0

Ağdan indirilen görüntüler. Ön provalamayı sunucuda yapabilirim ancak indirme daha yavaş olacak. Bu kodun Unfortunatley – peceps

cevap

4

Buldum:

/** 
    * 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; 
    } 

Kullanım böyle:

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

HTC Desire HD ve HTC Evo çalışmaz. Çözüm burada bakın: http://stackoverflow.com/questions/7747089/exception-in-drawable-createfromresourcestream-htc-only/9195531#9195531 – peceps

1

Bir görüntüyü göstermek istiyorum ama ne yazık ki bu görüntü büyük boyutta ise, örnek sağlayan, bir resim göstermek istiyorum alıp tekrar görüntü alanını bölen için kullanmak sizin gerektiren boyutundan daha büyükse 30 biçiminde 30'da, ardından onun boyutunu kontrol gereken tutarı (bu durumda burada 30 * 30) bölerek, ve ne var yine .

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

Bu güzel ve (diğer cevaplar benim için çalıştığınız değil) kolaydır, here bulundu:

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); 

Android belgeler mevcuttur here olduğunu.

0

resminizin meraktan bu

android.view.ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); 
layoutParams.width =MATCH_PARENT; 
layoutParams.height =MATCH_PARENT; 
imageView.setLayoutParams(layoutParams); 
İlgili konular