我需要从图像底部切掉 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
)
}
Run Code Online (Sandbox Code Playgroud)
我该如何使用cropOffLogowith 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;
}
}
Run Code Online (Sandbox Code Playgroud)
并得到这些错误:
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)'
Run Code Online (Sandbox Code Playgroud)
要从图像中剪切一些像素,您可以创建新的(自定义)转换。在科特林中:
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) {}
}
Run Code Online (Sandbox Code Playgroud)
或者用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) {
}
}
Run Code Online (Sandbox Code Playgroud)
在科特林中
.transform(CutOffLogo())
Run Code Online (Sandbox Code Playgroud)
或者在Java中
.transform(new CutOffLogo())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2337 次 |
| 最近记录: |