gpt4 book ai didi

java - 在 Android 布局中挖洞

转载 作者:行者123 更新时间:2023-11-30 11:22:41 29 4
gpt4 key购买 nike

是否可以在 Android 布局中创建孔。我有两种背景,一种是正常的,一种是模糊的,我想创建可以显示模糊背景的单元格。

我的 XML 将像这样构造:

Background_blurred > Background_normal > 单元格。

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

- 单元格无处不在,就像 GridView,而不仅仅是在边框上。 enter image description here

最佳答案

试试这个:

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 中):

    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 在哪里:

enter image description here

R.drawable.back_blur 是:

enter image description here

结果是:

enter image description here

关于java - 在 Android 布局中挖洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21580299/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com