2012-03-06 20 views
5

Il codice seguente ridimensiona una bitmap e mantiene le proporzioni. Mi chiedevo se esiste un modo più efficace di ridimensionamento, perché ho avuto l'idea che sto scrivendo il codice che è già disponibile nell'API di Android.Ridimensionamento di una bitmap

private Bitmap resizeImage(Bitmap bitmap, int newSize){ 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 

    int newWidth = 0; 
    int newHeight = 0; 

    if(width > height){ 
     newWidth = newSize; 
     newHeight = (newSize * height)/width; 
    } else if(width < height){ 
     newHeight = newSize; 
     newWidth = (newSize * width)/height; 
    } else if (width == height){ 
     newHeight = newSize; 
     newWidth = newSize; 
    } 

    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
      width, height, matrix, true); 

    return resizedBitmap; 
} 

risposta

0

io davvero non la penso così, ho fatto quasi allo stesso modo, come voi, ed ha ottenuto l'idea dalle pagine degli sviluppatori Android ...

0

dipende da come si sta utilizzando l'immagine . Se tutto ciò che si vuole fare è visualizzare la bitmap in una vista, basta chiamare myImageView.setImageBitmap(bitmap) e lasciare che il framework lo ridimensiona. Ma se sei preoccupato per la memoria, scalare prima potrebbe essere un buon approccio.

Problemi correlati