我想使用滑动转换从顶部裁剪位图,我有以下代码:
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";
}
}
我使用这样的转换:
Glide.with(context)
.load(user.getImageUrl())
.error(R.drawable.missingprofile)
.transform(new TopCropTransformation(context))
.into(holder.image);
滑行显示错误图像。任何人都可以帮助我吗?
编辑:我刚发现此异常" x +宽度必须为< = bitmap.width()"
答案 0 :(得分:0)
Finnaly使用
解决了@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);
}