2017-02-17 6 views
3

J'ai une confusion entre la mise à l'échelle et l'échantillonnage bitmap android ici peut avoir deux codes un pour la mise à l'échelle et un autre pour l'échantillonnage quelqu'un peut m'aider à identifier le fonctionnement de ce code et quelles sont les différences principales entre eux.
Quelle est la différence entre la mise à l'échelle et l'échantillonnage de bitmap?

Mise à l'échelle:

public static Bitmap getScaleBitmap(Bitmap bitmap, int newWidth, int newHeight) { 
    int width = bitmap.getWidth(); 
    int height = bitmap.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 
    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); 
} 

échantillonnage:

mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.id.myimage, 100, 100)); 

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) { 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

ici à la fois le code effectuer le redimensionnement d'image, mais de manière différente alors comment je peux identifier ce qui est bon et simple.

Répondre

0

Mise à l'échelle: Tout d'abord, vous décodez la totalité de l'image en mémoire, puis vous la redimensionnez.

Échantillonnage: Vous obtenez le bitmap mis à l'échelle requis sans charger le bitmap entier en mémoire.

0

Premier code, prend un bitmap et crée un nouveau bitmap plus petit - vous utiliseriez memmory pour le plus grand bitmap.

Le deuxième code prend la ressource. inJustDecodeBounds Le fait de ne pas charger l'intégralité de l'image en memmory juste des informations pour cela. Puis calculer comment il doit être dimensionné, puis à nouveau lorsqu'il est réglé inJustDecodeBounds pour charger fausse dans la version memmory réduite de l'image. Vous utiliserez donc memmory uniquement pour l'image décodée

Oficial docs