Android 模糊只会模糊图像,而不会模糊文本或其他内容
当我打开导航抽屉时,我有一个Android 模糊只会模糊图像,而不会模糊文本或其他内容,android,Android,当我打开导航抽屉时,我有一个BlurActionBarDrawerToggle来模糊我的背景: public class BlurActionBarDrawerToggle extends ActionBarDrawerToggle { public static int DEFAULT_BLUR_RADIUS = 25; public static float DEFAULT_DOWNSCALEFACTOR = 8.0f; private Context context
BlurActionBarDrawerToggle
来模糊我的背景:
public class BlurActionBarDrawerToggle extends ActionBarDrawerToggle {
public static int DEFAULT_BLUR_RADIUS = 25;
public static float DEFAULT_DOWNSCALEFACTOR = 8.0f;
private Context context = null;
private DrawerLayout drawerLayout = null;
private ImageView blurredImageView = null;
private int blurRadius = DEFAULT_BLUR_RADIUS;
private float downScaleFactor = DEFAULT_DOWNSCALEFACTOR;
private boolean prepareToRender = true;
private boolean isOpening = false;
public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, openDrawerContentDescRes, closeDrawerContentDescRes);
this.context = activity.getBaseContext();
this.drawerLayout = drawerLayout;
this.init();
}
public BlurActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);
this.context = activity.getBaseContext();
this.drawerLayout = drawerLayout;
this.init();
}
private void init() {
this.blurredImageView = new ImageView(context);
this.blurredImageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
this.blurredImageView.setClickable(false);
this.blurredImageView.setVisibility(View.GONE);
this.blurredImageView.setScaleType(ImageView.ScaleType.FIT_XY);
this.drawerLayout.setScrimColor(this.context.getResources().getColor(android.R.color.transparent));
this.drawerLayout.post(new Runnable() {
@Override
public void run() {
drawerLayout.addView(blurredImageView, 1);
}
});
}
@Override
public void onDrawerSlide(final View drawerView, final float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (slideOffset == 0.f) {
this.isOpening = false;
} else {
this.isOpening = true;
}
this.render();
this.setAlpha(this.blurredImageView, slideOffset, 100);
}
@Override
public void onDrawerClosed(View view) {
this.prepareToRender = true;
this.blurredImageView.setVisibility(View.GONE);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
if (newState == DrawerLayout.STATE_IDLE && !this.isOpening) {
this.handleRecycle();
}
}
private void render() {
if (this.prepareToRender) {
this.prepareToRender = false;
Bitmap bitmap = loadBitmapFromView(this.drawerLayout);
bitmap = scaleBitmap(bitmap);
bitmap = Blur.fastblur(this.context, bitmap, this.blurRadius, false);
this.blurredImageView.setVisibility(View.VISIBLE);
this.blurredImageView.setImageBitmap(bitmap);
}
}
public void setRadius(int radius) {
this.blurRadius = radius < 1 ? 1 : radius;
}
public void setDownScaleFactor(float downScaleFactor) {
this.downScaleFactor = downScaleFactor < 1 ? 1 : downScaleFactor;
}
private void setAlpha(View view, float alpha, long durationMillis) {
final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
animation.setDuration(durationMillis);
animation.setFillAfter(true);
view.startAnimation(animation);
}
private Bitmap loadBitmapFromView(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
private Bitmap scaleBitmap(Bitmap myBitmap) {
int width = (int) (myBitmap.getWidth() / this.downScaleFactor);
int height = (int) (myBitmap.getHeight() / this.downScaleFactor);
return Bitmap.createScaledBitmap(myBitmap, width, height, false);
}
private void handleRecycle() {
Drawable drawable = this.blurredImageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap != null) {
bitmap.recycle();
}
this.blurredImageView.setImageBitmap(null);
}
this.prepareToRender = true;
}
}
不幸的是,这只是模糊了像背景图像这样的图像。
下图显示了带有图像背景的视图的模糊:
第二张图片显示了相同的导航抽屉和actionbardrawertoggle
在没有任何图像的视图上:
正如你所看到的,这里没有任何东西是模糊的。有人能告诉我这里出了什么问题吗?这实际上是三个因素的组合:
blurredImageView
设置背景色。如果没有其他更改,这意味着其他UI元素(文本、行和c)将被隐藏。例如,在render()
中:
如果要使其模糊但仍然模糊可见,请减小缩小比例因子和半径,例如:
mDrawerToggle.setDownScaleFactor(2);
mDrawerToggle.setRadius(10);
但是,请注意,这也会减少图像的模糊(以及为位图消耗更多内存)。模糊会影响画布的内容。您可以尝试在位图的画布内渲染文本,然后应用蓝色效果。这将导致文本模糊。
this.blurredImageView.setVisibility(View.VISIBLE);
this.blurredImageView.setImageBitmap(bitmap);
this.blurredImageView.setBackgroundColor(Color.RED); // added
mDrawerToggle.setDownScaleFactor(2);
mDrawerToggle.setRadius(10);