Android 滑动:裁剪 - 从图像底部剪掉 X 像素
Android glide: crop - cut off X pixels from image bottom
我需要从图像底部切掉 20px 并缓存它,这样设备就不必在用户每次再次看到图像时一遍又一遍地裁剪它,否则对电池等不利。对吧?
这是我目前拥有的:
Glide
.with(context)
.load(imgUrl)
.into(holder.image)
fun cropOffLogo(originalBitmap: Bitmap) : Bitmap {
return Bitmap.createBitmap(
originalBitmap,
0,
0,
originalBitmap.width,
originalBitmap.height - 20
)
}
如何将 cropOffLogo
与 glide
一起使用?
编辑:
我尝试使用 https://github.com/bumptech/glide/wiki/Transformations#custom-transformations
private static class CutOffLogo extends BitmapTransformation {
public CutOffLogo(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
int outWidth, int outHeight) {
Bitmap myTransformedBitmap = Bitmap.createBitmap(
toTransform,
10,
10,
toTransform.getWidth(),
toTransform.getHeight() - 20);
return myTransformedBitmap;
}
}
并得到那些错误:
Modifier 'private' not allowed here
Modifier 'static' not allowed here
'BitmapTransformation()' in 'com.bumptech.glide.load.resource.bitmap.BitmapTransformation' cannot be applied to '(android.content.Context)'
查看Transformations on Glide and do your custom Transformation。
转型
要从图像中剪切一些像素,您可以创建新的(自定义)转换。在科特林中:
class CutOffLogo : BitmapTransformation() {
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap =
Bitmap.createBitmap(
toTransform,
0,
0,
toTransform.width,
toTransform.height - 20 // numer of pixels
)
override fun updateDiskCacheKey(messageDigest: MessageDigest) {}
}
或 Java:
public class CutOffLogo extends BitmapTransformation {
@Override
protected Bitmap transform(
@NotNull BitmapPool pool,
@NotNull Bitmap toTransform,
int outWidth,
int outHeight
) {
return Bitmap.createBitmap(
toTransform,
0,
0,
toTransform.getWidth(),
toTransform.getHeight() - 20 // numer of pixels
);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
调用它
在科特林中
.transform(CutOffLogo())
或 Java
.transform(new CutOffLogo())
我需要从图像底部切掉 20px 并缓存它,这样设备就不必在用户每次再次看到图像时一遍又一遍地裁剪它,否则对电池等不利。对吧?
这是我目前拥有的:
Glide
.with(context)
.load(imgUrl)
.into(holder.image)
fun cropOffLogo(originalBitmap: Bitmap) : Bitmap {
return Bitmap.createBitmap(
originalBitmap,
0,
0,
originalBitmap.width,
originalBitmap.height - 20
)
}
如何将 cropOffLogo
与 glide
一起使用?
编辑:
我尝试使用 https://github.com/bumptech/glide/wiki/Transformations#custom-transformations
private static class CutOffLogo extends BitmapTransformation {
public CutOffLogo(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
int outWidth, int outHeight) {
Bitmap myTransformedBitmap = Bitmap.createBitmap(
toTransform,
10,
10,
toTransform.getWidth(),
toTransform.getHeight() - 20);
return myTransformedBitmap;
}
}
并得到那些错误:
Modifier 'private' not allowed here
Modifier 'static' not allowed here
'BitmapTransformation()' in 'com.bumptech.glide.load.resource.bitmap.BitmapTransformation' cannot be applied to '(android.content.Context)'
查看Transformations on Glide and do your custom Transformation。
转型
要从图像中剪切一些像素,您可以创建新的(自定义)转换。在科特林中:
class CutOffLogo : BitmapTransformation() {
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap =
Bitmap.createBitmap(
toTransform,
0,
0,
toTransform.width,
toTransform.height - 20 // numer of pixels
)
override fun updateDiskCacheKey(messageDigest: MessageDigest) {}
}
或 Java:
public class CutOffLogo extends BitmapTransformation {
@Override
protected Bitmap transform(
@NotNull BitmapPool pool,
@NotNull Bitmap toTransform,
int outWidth,
int outHeight
) {
return Bitmap.createBitmap(
toTransform,
0,
0,
toTransform.getWidth(),
toTransform.getHeight() - 20 // numer of pixels
);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
调用它
在科特林中
.transform(CutOffLogo())
或 Java
.transform(new CutOffLogo())