如何在Android中通过Glide显示背景模糊的图像?
目标是通过如何在Android中通过Glide显示背景模糊的图像?,android,android-glide,Android,Android Glide,目标是通过GLide在ImageView中有效地在中心显示背景模糊的图像。 图像的最大高度为200dp 临时工。解决方案: 我有两个重叠的ImageView(第一个底部ImageView有:android:scaleType=“fitXY”): 显示中心图像: Glide.with(mCtx) .load(Uri.fromFile(file)) .transition(DrawableTransitionOptions.withCrossFade()) .placeho
GLide
在ImageView
中有效地在中心显示背景模糊的图像。
图像的最大高度为200dp
临时工。解决方案:
我有两个重叠的ImageView
(第一个底部ImageView
有:android:scaleType=“fitXY”
):
显示中心图像:
Glide.with(mCtx)
.load(Uri.fromFile(file))
.transition(DrawableTransitionOptions.withCrossFade())
.placeholder(new ColorDrawable(Color.TRANSPARENT))
.into(preview);
格拉德尔:
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
implementation 'jp.wasabeef:glide-transformations:4.1.0' //to use BlurTransformation
问题:背景和中心图像在不同的时间加载,因为使用了两次滑动。
可能效果不好 我用一个
Glide
和一个ImageView
:.into(new CustomTarget(){…}
,但GLide不知道ImageView
大小,并且占位符和转换不起作用
下一步尝试ImageViewTarget
:
.into(new ImageViewTarget<Drawable>(preview) { //Have to be ImageViewTarget for placeholder and transition working
@Override
protected void setResource(@Nullable Drawable resource) {
//preview.setScaleType(ImageView.ScaleType.FIT_XY);
preview.setBackground(resource);
}
})
.into(新的ImageViewTarget(预览){//必须是ImageViewTarget,以便占位符和转换工作
@凌驾
受保护的void setResource(@Nullable Drawable resource){
//preview.setScaleType(ImageView.ScaleType.FIT_XY);
预览。挫折背景(资源);
}
})
占位符
和转换
工作正常,但模糊背景不会显示,因为模糊背景宽度与中心图像相同。当我使用预览.setScaleType(ImageView.ScaleType.FIT_XY)时;
中心图像填充所有可能的宽度。我需要使用宽度和最大高度200dp将图像显示到中心,并将模糊背景拉伸到最大宽度和最大高度200dp。此代码适用于我使用picaso 2.5.2尝试此操作
公共类转换实现转换{
渲染脚本
public BlurTransformation(Context context) {
super();
rs = RenderScript.create(context);
}
@Override
public Bitmap transform(Bitmap bitmap) {
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(10);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred bitmap
output.copyTo(blurredBitmap);
bitmap.recycle();
return blurredBitmap;
}
@Override
public String key() {
return "blur";
}
}这段代码对我有用,请在picaso 2.5.2上试用
公共类转换实现转换{
渲染脚本
public BlurTransformation(Context context) {
super();
rs = RenderScript.create(context);
}
@Override
public Bitmap transform(Bitmap bitmap) {
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(10);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred bitmap
output.copyTo(blurredBitmap);
bitmap.recycle();
return blurredBitmap;
}
@Override
public String key() {
return "blur";
}
}您找到解决方案了吗?您找到解决方案了吗?
public BlurTransformation(Context context) {
super();
rs = RenderScript.create(context);
}
@Override
public Bitmap transform(Bitmap bitmap) {
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(10);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred bitmap
output.copyTo(blurredBitmap);
bitmap.recycle();
return blurredBitmap;
}
@Override
public String key() {
return "blur";
}