【发布时间】:2017-01-01 17:10:00
【问题描述】:
我有不同颜色的视图。我需要模糊该视图的背景。例如,有一个线性布局,其中有一个显示一些应用程序的网格,这个线性布局(Gridview Container)有颜色(红色/绿色/黑色......等没有图像)。现在我需要模糊 LinearLayout 的背景。 这个形象是我必须实现的。
我正在通过 Android Render 脚本执行所有这些操作,因为我有许多片段并且每个片段都有彩色背景,所以我认为 Render 是最好的选择,否则它可能会在查看寻呼机滑动时卡住。
@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 我们可以模糊视图的背景有颜色但没有图像
【问题讨论】:
-
if I set color in the background of the LinearLayout then不清楚:你想模糊... 一种颜色????很明显,您只能模糊图像。并且它们的大小必须大于 0(我什至会说大于 1*1 像素)。在我看来,“模糊”一种颜色就像你想对颜色 透明度 采取行动一样。这与 blur 效果有很大不同。简而言之:can we blur the background of a view have having color but not image? 没有。 -
@Rotwang 谢谢,你说得对,但情况是,如果我将该颜色的透明度设置为低,那么我可以清楚地看到背景壁纸,这里我需要如果用户设置不透明颜色,那么肯定你是对的,但是如果用户在我上传的图片中设置了透明颜色,那么我需要在 Gridview 后面显示模糊效果(现在不在图片中)
-
再次,不清楚。但如果我能让你好起来,你希望 GridView 容器是“半透明的”。并且 GridView 完全透明,就像现在一样。
-
@Rotwang stackoverflow.com/questions/31808955/… 这是很好的解释。我需要在视图后面进行模糊处理(线性布局),线性布局是纯色还是透明色。
-
再次,这是关于模糊 图像。这就是模糊的意义所在。 “模糊”单一颜色根本没有意义。
标签: android