Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 导航抽屉背景模糊_Android_Blur_Navigation Drawer - Fatal编程技术网

Android 导航抽屉背景模糊

Android 导航抽屉背景模糊,android,blur,navigation-drawer,Android,Blur,Navigation Drawer,我该如何在导航抽屉上创建一个实时模糊,这样当你拉出抽屉时,列表的背景就是它后面显示的片段的模糊背景 我已经看过了,但主要发现位图模糊,并且没有任何活动内容。该过程包括以下几个阶段: 获取整个抽屉布局的快照 将其裁剪到菜单的精确大小 将其缩小一点(系数8相当不错) 模糊图像 有一个自定义视图,在菜单显示位置的正后方显示部分图像 当你滑动抽屉时,你可以看到图片的大部分 下面是一个代码示例(都是在BlurActionBarDrawerToggle和BlurAndDimView中完成的): “BlurA

我该如何在导航抽屉上创建一个实时模糊,这样当你拉出抽屉时,列表的背景就是它后面显示的片段的模糊背景


我已经看过了,但主要发现位图模糊,并且没有任何活动内容。

该过程包括以下几个阶段:

  • 获取整个抽屉布局的快照
  • 将其裁剪到菜单的精确大小
  • 将其缩小一点(系数8相当不错)
  • 模糊图像
  • 有一个自定义视图,在菜单显示位置的正后方显示部分图像
  • 当你滑动抽屉时,你可以看到图片的大部分
  • 下面是一个代码示例(都是在BlurActionBarDrawerToggle和BlurAndDimView中完成的):

    “BlurAndDim”视图使菜单下方的部分模糊,并使其余部分变暗(您可以调整数字以获得所需的确切效果):

    对于良好的模糊算法(在可能的情况下使用Renderscript),可以使用

    布局如下所示:

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.v4.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.bla.bla.BlurAndDimView
            android:id="@+id/blurrer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:visibility="invisible" />
    
        <ListView
            android:id="@+id/menu_list"
            android:layout_width="@dimen/browse_menu_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice" />
    </android.support.v4.widget.DrawerLayout>
    
    
    
    你真的想模糊背景,而不仅仅是使其半透明吗?模糊是一个昂贵的操作,在没有技巧的情况下进行动态操作可能代价太高!你能创建一个透明的.png图像,上面有一些模糊吗?然后将抽屉背景设置为该值?不能使用blur.png,因为创建模糊需要对图像进行采样,然后进行模糊记住,根据缩小/模糊的程度,在某些设备上可能需要40-50毫秒以上的时间。但是,如果你坚持我给出的数字,你应该能够在大多数情况下毫无问题地使用它。我试过这个。但无法显示抽屉。如何从MainActivity使用它(打开抽屉)?您从哪里获得“Blur.Blur”的?您可以指定您使用的库或类吗?谢谢,我已经在答案中提供了链接:如何在导航抽屉从右侧打开时使用此链接?
    public class BlurAndDimView extends View {
    
        private static final int BLUR_RADIUS = 4;
        private static final int BLUE_ALPHA = 178;
        private static final int MAX_DIM_ALPHA = 127;
        private Paint bitmapPaint;
        private Paint dimPaint;
        private Paint blueSemiTransparentPaint;
        private int menuWidth;
        private int titleHeight;
    
        private Bitmap image;
        private Rect rectDst;
        private Rect rectSrc;
        private int downSampling;
        private Rect rectRest;
    
        public BlurAndDimView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            postConstruct();
        }
    
        public BlurAndDimView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
            postConstruct();
        }
    
        public BlurAndDimView(Context context) {
            this(context, null);
            postConstruct();
        }
    
        private void postConstruct() {
            bitmapPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
            dimPaint = new Paint();
            blueSemiTransparentPaint = new Paint();
            //You might want to also have a semitransparent overlay over your blurred image, since you can't control what's behind your menu.
            blueSemiTransparentPaint.setColor(getResources().getColor(R.color.dark_blue));
            blueSemiTransparentPaint.setAlpha(BLUE_ALPHA);
            menuWidth = getResources().getDimensionPixelSize(R.dimen.browse_menu_width);
        }
    }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (image != null) {
                canvas.drawBitmap(image, rectSrc, rectDst, bitmapPaint);
                canvas.drawRect(rectDst, blueSemiTransparentPaint);
                canvas.drawRect(rectRest, dimPaint);
            }
        }
    
        public void handleScroll(float xOffset) {
            if(image != null) {
                rectSrc.right = (int) (image.getWidth() * xOffset);
                rectDst.right = rectSrc.right * downSampling;
                rectRest.left = rectDst.right;
                dimPaint.setAlpha((int) (xOffset * MAX_DIM_ALPHA));
                invalidate();
            }
        }
    
        public void setImage(Bitmap bmp, int downSampling) {
            Bitmap cropped = Bitmap.createBitmap(bmp, 0, 0, menuWidth / downSampling, getHeight() / downSampling);
            this.image = Blur.blur(getContext(), cropped, BLUR_RADIUS);
            rectSrc = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
            rectDst = new Rect(0, 0, menuWidth, getHeight());
            rectRest = new Rect(menuWidth, 0, getWidth(), getHeight());
            this.downSampling = downSampling;
            invalidate();
        }
    
        public boolean hasImage() {
            return image != null;
        }
    
        public void clearImage() {
            image = null;
        }
    }
    
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.v4.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.bla.bla.BlurAndDimView
            android:id="@+id/blurrer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:visibility="invisible" />
    
        <ListView
            android:id="@+id/menu_list"
            android:layout_width="@dimen/browse_menu_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice" />
    </android.support.v4.widget.DrawerLayout>