2010-07-06 6 views
0

Je suis en train d'écrire une application Music et j'ai déjà eu l'album arts. Cependant, ils sont venus dans différentes tailles. Alors, comment est-ce que j'ai normalisé la taille du bitmap retourné?Redimensionnement de bitmaps

Répondre

4

Tu ferais quelque chose comme ceci:

// load the origial BitMap (500 x 500 px) 
     Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
       R.drawable.android); 

     int width = bitmapOrg.width(); 
     int height = bitmapOrg.height(); 
     int newWidth = 200; 
     int newHeight = 200; 

     // calculate the scale - in this case = 0.4f 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     // createa matrix for the manipulation 
     Matrix matrix = new Matrix(); 
     // resize the bit map 
     matrix.postScale(scaleWidth, scaleHeight); 

     // recreate the new Bitmap 
     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
          width, height, matrix, true); 
0

Ou vous pouvez redimensionner l'image bitmap à la taille requise lorsque vous dessinez sur la toile:

De la documentation Android:

drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint) Dessine le bitmap spécifié, redimensionnant/translatant automatiquement pour remplir le rectangle de destination.

Marque src nul et dst est un Rect vous voulez la taille/la position sur la toile, mis en place comme

Rect rect = new Rect(0, 0, width, height) 
canvas.drawBitmap(bitmap, null, rect) 
0

Dans mon expérience le code dans la réponse acceptée ne fonctionne pas, au moins sur certaines plateformes.

Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); 

vous donnera une image sous-échantillonnée à la pleine taille de l'original - donc juste une image floue.

Chose intéressante, le code

Bitmap resizedBitmap = Bitmap.createScaledBitmap(square, (int) targetWidth, (int) targetHeight, false); 

donne aussi l'image floue. Dans mon cas, il était nécessaire de le faire:

// RESIZE THE BIT MAP 

// According to a variety of resources, this function should give us pixels from the dp of the screen 
// From http://stackoverflow.com/questions/4605527/converting-pixels-to-dp-in-android 
float targetHeight = DWUtilities.convertDpToPixel(80, getActivity()); 
float targetWidth = DWUtilities.convertDpToPixel(80, getActivity()); 

// However, the above pixel dimension are still too small to show in my 80dp image view 
// On the Nexus 4, a factor of 4 seems to get us up to the right size 
// No idea why. 
targetHeight *= 4; 
targetWidth *= 4; 

matrix.postScale((float) targetHeight/square.getWidth(), (float) targetWidth/square.getHeight()); 


Bitmap resizedBitmap = Bitmap.createBitmap(square, 0, 0, square.getWidth(), square.getHeight(), matrix, false); 

// By the way, the below code also gives a full size, but blurry image 
// Bitmap resizedBitmap = Bitmap.createScaledBitmap(square, (int) targetWidth, (int) targetHeight, false 

Je n'ai pas une autre solution à ce moment, mais c'est nous espérons utile à quelqu'un.