2012-01-12 20 views
8

Come posso caricare drawable da InputStream (risorse, file system) e ridimensionarlo dinamicamente in base alla risoluzione dello schermo hdpi, mdpi o ldpi?Caricamento Android programmabile programmaticamente e ridimensionarlo

L'immagine originale è in hdpi, ho solo bisogno di ridimensionare per mdpi e ldpi.

Qualcuno sa come fa Android il ridimensionamento dinamico dei drawable in/res?

+0

Per curiosità, alcuna ragione per non pre-dimensionamento (per 'mdpi' e' ldpi') e collegandoci ad esso nella directory 'res'? –

+0

Le immagini scaricate dalla rete. Posso fare il pre-processo sul server ma il download sarà più lento. – peceps

risposta

4

trovato:

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

Usa come questo:

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

Purtroppo questo codice non funziona su HTC Desire HD e HTC Evo. Vedi la soluzione qui: http://stackoverflow.com/questions/7747089/exception-in-draw-createfromresourcestream-htc-only/9195531#9195531 – peceps

1

Se si desidera visualizzare un'immagine, ma purtroppo questa immagine è di grandi dimensioni, permette ad esempio, si desidera visualizzare un'immagine nel formato 30 per 30, quindi verificarne le dimensioni se è maggiore della dimensione richiesta, quindi dividerlo per la quantità (30 * 30 qui in questo caso), e ciò che si ottiene è di nuovo prendere e utilizzare per dividere nuovamente l'area dell'immagine .

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

Questo è bello e facile (le altre risposte non funzionavano per me), trovato 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); 

documentazione Android è disponibile here.

0

dopo aver caricato la tua foto e imposta a imageview è possibile utilizzare dimensioni layoutparamsto vostra immagine per match_parent

come questo

android.view.ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); 
layoutParams.width =MATCH_PARENT; 
layoutParams.height =MATCH_PARENT; 
imageView.setLayoutParams(layoutParams); 
Problemi correlati