挖孔的Andr​​oid布局布局、挖孔、Andr、oid

2023-09-06 00:56:21 作者:青春是场偶遇i

是否有可能在Android layout.I创造洞中有两个背景,一是正常的,人们模糊,我想创建细胞,它可以显示模糊的背景。

Is it possible to create holes in an Android layout.I have two backgrounds, one normal, one blurred, and what I want to create cells that can display the blurred background.

我的XML将这样的构造:

My XML would be constructed like this:

Background_blurred> Background_normal>细胞。

Background_blurred > Background_normal > Cells.

- 我已经知道如何模糊背景。

-I already know how to blur a background.

- 细胞遍地都是,就像一个GridView,而不仅仅是边界。

-Cells are everywhere, like a GridView, and not just on the borders.

推荐答案

试试这个:

class FL extends FrameLayout {
    private List<View> mViews = new ArrayList<View>();
    private Bitmap mBack;
    private Bitmap mBackBlur;
    private int[] mLocation = new int[2];
    private Matrix mMatrix = new Matrix();

    public FL(Context context) {
        super(context);
        Resources res = getResources();
        mBack = BitmapFactory.decodeResource(res, R.drawable.back);
        mBackBlur = BitmapFactory.decodeResource(res, R.drawable.back_blur);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        RectF src = new RectF(0, 0, mBack.getWidth(), mBack.getHeight());
        RectF dst = new RectF(0, 0, w, h);
        mMatrix.setRectToRect(src, dst, ScaleToFit.FILL);
    }

    public void add(View v) {
        mViews.add(v);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.drawBitmap(mBack, mMatrix, null);

        canvas.save(Canvas.CLIP_SAVE_FLAG);
        getLocationOnScreen(mLocation);
        int x = mLocation[0];
        int y = mLocation[1];
        Op op = Op.REPLACE;
        for (View v : mViews) {
            v.getLocationOnScreen(mLocation);
            mLocation[0] -= x;
            mLocation[1] -= y;

            int left = mLocation[0];
            int top = mLocation[1];
            int right = left + v.getWidth();
            int bottom = top + v.getHeight();
            canvas.clipRect(left, top, right, bottom, op);
            op = Op.UNION;
        }
        canvas.drawBitmap(mBackBlur, mMatrix, null);
        canvas.restore();
        super.dispatchDraw(canvas);
    }
}

测试code(中的onCreate):

testing code (in onCreate):

    FL fl = new FL(this);
    ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    float[] hsv = {
            0, 1, 0.75f
    };
    float[] h = {
            0, 39, 60, 120, 300
    };
    for (int i = 0; i < 5; i++) {
        TextView tv = new TextView(this);
        hsv[0] = h[i];
        tv.setTextColor(0xffeeeeee);
        tv.setBackgroundColor(Color.HSVToColor(128, hsv));
        tv.setPadding(10, 10, 10, 10);
        tv.setTextSize(64);
        tv.setText("#" + i);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = 10;
        params.bottomMargin = 10;
        params.leftMargin = 20;
        params.rightMargin = 20;
        ll.addView(tv, params);
        fl.add(tv);
    }
    sv.addView(ll);
    fl.addView(sv);
    setContentView(fl);

在这里R.drawable.back是:

where R.drawable.back is:

和R.drawable.back_blur是:

and R.drawable.back_blur is:

结果是: