首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android布局中挖洞

在Android布局中挖洞
EN

Stack Overflow用户
提问于 2014-02-05 14:41:40
回答 1查看 2.7K关注 0票数 2

我有两种背景,一种是普通的,一种是模糊的,还有我想要创建的可以显示模糊背景的细胞。

我的XML应该是这样构造的:

Background_blurred > Background_normal >细胞。

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

-Cells无处不在,就像一个GridView,而不仅仅是在边界上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-05 16:58:50

试试这个:

代码语言:javascript
运行
复制
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);
    }
}

测试代码(在onCreate中):

代码语言:javascript
运行
复制
    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是:

R.drawable.back_blur是:

结果是:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21580299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档