Android 如何模糊视图
我有不同颜色的风景。我需要模糊那个视图的背景。例如,有一个线性布局,其中有一个网格显示一些应用程序,这个线性布局(Gridview容器)有颜色(红色/绿色/黑色…等等,没有图像)。现在我需要模糊线性布局的背景。 这个形象是我必须达到的。 我用Android渲染脚本来做这些,因为我有很多片段,每个片段都有彩色背景,所以我认为渲染是最好的选择,否则在浏览页面时可能会卡住Android 如何模糊视图,android,Android,我有不同颜色的风景。我需要模糊那个视图的背景。例如,有一个线性布局,其中有一个网格显示一些应用程序,这个线性布局(Gridview容器)有颜色(红色/绿色/黑色…等等,没有图像)。现在我需要模糊线性布局的背景。 这个形象是我必须达到的。 我用Android渲染脚本来做这些,因为我有很多片段,每个片段都有彩色背景,所以我认为渲染是最好的选择,否则在浏览页面时可能会卡住 @Override protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout mainLayout=(RelativeLayout)findViewById(R.id.activity_main);
view=(View)findViewById(R.id.view);
Bitmap blurredBitmap = blur( this, getBitmapFromView(view) );
view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
mainLayout.setBackgroundResource(R.drawable.wallp);
}
public static Bitmap getBitmapFromView(View view) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(canvas);
return bitmap;
}
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
现在的问题是,如果我在LinearLayout的背景中设置颜色,那么就会出现错误
原因:java.lang.IllegalArgumentException:宽度和高度必须大于0
我们可以模糊一个有颜色但没有图像的视图的背景吗
以下是我的观点…这可以通过以下步骤实现
LinearLayout
的背景图像
背景图像LinearLayout
Class 1中创建两个方法。
DrawBitmap
2<代码>绘图颜色调用
getBitmapFromView
方法时,视图可能未准备就绪。您可以通过以下方式包装您的方法:
view.post(new Runnable() {
public void run(){
Bitmap blurred = blur(YourActivity.this, getBitmapFromView(view));
view.setBackgroundDrawable(new BitmapDrawable(getResources(), blurred));
}
});
您可以使用blur()
方法
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
希望这有帮助
如果我在线性布局的背景中设置了颜色,则不清楚:是否要模糊。。。一种颜色????很明显,你只能模糊图像。它们的大小必须大于0(我甚至可以说大于1*1像素)。“模糊”单一颜色在我看来就像你想对颜色的透明度,而不是。这与模糊效果大不相同。简而言之:我们可以模糊一个有颜色但没有图像的视图的背景吗
?不。@Rotwang谢谢,你说得对,但情况是,如果我将该颜色的透明度设置为较低,那么我可以清楚地看到背景墙纸,这里我需要,如果用户设置为非透明颜色,那么你肯定是对的,但如果用户将透明颜色设置为我上传的图片,那么我需要在Gridview中显示模糊效果(图片中现在没有)同样,不清楚。但是如果我能让你明白,你希望GridView容器是“半透明”的。并且GridView完全透明,就像现在一样。@Rotwang这是一个很好的解释。我需要在视图后面模糊(线性布局),线性布局是纯色还是透明色。同样,这是关于模糊图像。这就是模糊的全部。模糊单一颜色毫无意义。模糊()从何而来?@c-an您可以使用所提供的相同的blur()
方法。