我有一个不同颜色的视图.我需要模糊该视图的背景.例如,有一个LinearLayout,其中有一个Grid显示一些应用程序,这个线性布局(Gridview容器)有颜色(红色/绿色/黑色……等没有图像).现在我需要模糊LinearLayout的背景.
这个图像是我必须实现的.
我正在通过Android 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的背景中设置颜色,那么就会出现错误
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
can we blur the background of a view have having color but not image
最佳答案 这可以通过以下步骤实现
>通过裁剪提取LinearLayout的背景图像
背景图片.
>现在扩展LinearLayout类.
>覆盖OnDraw(Canvas mCanvas)方法.
>在Custom LinearLayout Class 1中创建两个方法.
DrawBitmap 2. DrawColor.
>首先通过给出你得到的偏移来调用DrawBitmap函数
ViewPager为背景图像,以便在使用时滑动图像
屏幕.
>最后用透明度级别绘制颜色
我希望这能解决你的问题.
示例代码