2016-06-28 13 views
-1

Ich brauche Bilder anzuzeigen, die in der Datenbank gespeichert sind, und ich habe Probleme mit Bild (Bitmap) Breite/Höhe und Image ...Bitmap und Image Ausgabe - Probleme mit Bild Breite/Höhe

Just for Test

Works: - wenn ich gleiche Bild in Projekt Drawables hinzufügen kann ich diese verwenden

Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_image); 
BitmapDrawable bd = new BitmapDrawable(context.getResources(), b); 
imageView.setImageDrawable(bd); 

gleiche wie

imageView.setImageResource(R.drawable.menu_image); 

Das folgende nicht zu arbeiten, weil Bild nicht geändert wird:

imageView.setImageBitmap(image); 

gleiche wie

imageView.setImageDrawable(new BitmapDrawable(context.getResources(), image)); 

Wo Bild wird dieses konstruiert mit:

public static Bitmap base64ToBitmap(String b64) { 
    byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT); 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inDensity =  context.getResources().getDisplayMetrics().densityDpi; 
    options.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi; 
    Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length, options); 
    return bitmap; 
} 

Bild Originalgröße ist 338x94.

676x188, dies ist die Bildgröße, wenn ich ein Bild aus dem Drawables-Verzeichnis des Projekts verwende. Das ist die Größe, nach der ich suche, in diesem Fall. Ich nehme an, eine schnelle Lösung wäre die Verwendung von Bitmap.createScaledBitmap(), aber ich habe ein paar verschiedene Bildformate und ich würde gerne imageView.setImageBitmap oder imageView.setImageDrawable verwenden, um sich so zu verhalten, wie ich Bitmap aus dem Drawables-Verzeichnis des Projekts geladen habe.

Antwort

-1

Verwenden Sie die folgende Hilfsklasse von github

public class BitmapScaler 
{ 
// scale and keep aspect ratio 
public static Bitmap scaleToFitWidth(Bitmap b, int width) 
{ 
    float factor = width/(float) b.getWidth(); 
    return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true); 
} 


// scale and keep aspect ratio 
public static Bitmap scaleToFitHeight(Bitmap b, int height) 
{ 
    float factor = height/(float) b.getHeight(); 
    return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true); 
} 


// scale and keep aspect ratio 
public static Bitmap scaleToFill(Bitmap b, int width, int height) 
{ 
    float factorH = height/(float) b.getWidth(); 
    float factorW = width/(float) b.getWidth(); 
    float factorToUse = (factorH > factorW) ? factorW : factorH; 
    return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorToUse), 
      (int) (b.getHeight() * factorToUse), true); 
} 


    // scale and don't keep aspect ratio 
    public static Bitmap strechToFill(Bitmap b, int width, int height) 
    { 
    float factorH = height/(float) b.getHeight(); 
    float factorW = width/(float) b.getWidth(); 
    return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorW), 
      (int) (b.getHeight() * factorH), true); 
    } 
    }