android 单元布局,在Android布局中挖洞

试试这个:(中的onCreate)

class FL extends FrameLayout {

private List mViews = new ArrayList();

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);

}

}

测试代码:

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是:

GkeIx.jpg

和R.drawable .back_blur是:

be174ccdd7ce027bdf7a422ed44d6108.png

结果是:

66dbf63f88a4d93e99f69a2f1f1741bf.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值