Hal*_*iaz 5 android bitmap imageview renderscript
使用滑动android库我将图像作为位图(参见滑动文档),然后我尝试使用renderscript和ScriptIntrinsicBlur模糊位图,这是一个高斯模糊.(取自此stackoverflow帖子)
Glide.with(getApplicationContext())
.load(ImageUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(300,200) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
RenderScript rs = RenderScript.create(mContext); // context = this. this referring to the activity
final Allocation input = Allocation.createFromBitmap( rs, resource, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT );
final Allocation output = Allocation.createTyped( rs, input.getType() );
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create( rs, Element.U8_4( rs ) );
script.setRadius(8f);
script.setInput(input);
script.forEach(output);
output.copyTo(resource);
mImageView.setImageBitmap(resource);
}
});
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感谢谢谢.:)
因为它只支持U8_4和U8格式.在通过此示例将位图发送到RenderScript之前,您必须将位图转换为ARGB_8888.
Bitmap U8_4Bitmap;
if(sentBitmap.getConfig() == Bitmap.Config.ARGB_8888) {
U8_4Bitmap = sentBitmap;
} else {
U8_4Bitmap = sentBitmap.copy(Bitmap.Config.ARGB_8888, true);
}
//==============================
Bitmap bitmap = Bitmap.createBitmap(U8_4Bitmap.getWidth(), U8_4Bitmap.getHeight(), U8_4Bitmap.getConfig());
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs,
U8_4Bitmap,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, output.getElement());
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
rs.destroy();
return bitmap;
Run Code Online (Sandbox Code Playgroud)
有没有可能输入图像不是U8_4(即RGBA8888)?您可以从使用“Element.U8_4(rs)”切换为使用“output.getElement()”吗?这可能会做正确的事。如果结果表明图像不是 RGBA8888,您至少可能会得到一个 Java 异常,描述底层格式是什么(如果我们的 Blur 不支持它)。
| 归档时间: |
|
| 查看次数: |
1011 次 |
| 最近记录: |