2016-11-09 5 views
0

Ich möchte ein Bitmap von oben zuzuschneiden eine Glide-Transformation, ich habe den folgenden Code:Glide Transformation nicht funktioniert

public class TopCropTransformation extends BitmapTransformation { 

    public TopCropTransformation(Context context) { 
     super(context); 
    } 

    public TopCropTransformation(BitmapPool bitmapPool) { 
     super(bitmapPool); 
    } 

    @Override 
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
     return Bitmap.createBitmap(toTransform, 0,0, outWidth, outHeight); 
    } 

    @Override 
    public String getId() { 
     return "TopCropTransformation"; 
    } 
} 

Und ich Transformation wie folgt aus:

Glide.with(context) 
.load(user.getImageUrl()) 
.error(R.drawable.missingprofile) 
.transform(new TopCropTransformation(context)) 
.into(holder.image); 

und gleiten zeigt das Fehlerbild an. Kann mir jemand helfen?

EDIT: Ich habe gerade diese Ausnahme gefunden "x + Breite sein muss < = bitmap.width()"

+0

look @ diesem Link http: // Stackoverflow. com/a/13171571/5255006 –

Antwort

0

Finnaly mit gelöst

@Override 
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 

    float heightRatio = (float)toTransform.getHeight()/(float)toTransform.getWidth(); 
    int newHeight = (int) (heightRatio * outWidth); 
    Bitmap scaled = Bitmap.createScaledBitmap(toTransform, outWidth, newHeight, false); 
    return Bitmap.createBitmap(scaled, 0,0, outWidth, outHeight); 
} 
Verwandte Themen