Android:使用两种背景颜色制作圆形布局

发布于 12-07 21:16 字数 898 浏览 1 评论 0原文

我想制作一个圆形形状并将其应用为布局上的背景。我已经控制住了,但我想让背景像进度条一样。

这就是我希望它看起来像这样的图像:

image with 2 backgrounds

我希望能够控制 bg 的位置改变所以我想它应该在代码中完成。 它必须以某种方式完成,这样我就可以在它上面放置文本,

此时我有用于创建圆角形状的代码:

  int w = this.getWidth() - this.getPaddingLeft() - this.getPaddingRight();
  int h = this.getHeight() - this.getPaddingTop() - this.getPaddingBottom();
  int progressWidth = Math.round((w * percent) / 100);

  ShapeDrawable shapeDrawable = new ShapeDrawable();
  float[] outerR = new float[]{5, 5, 5, 5, 5, 5, 5, 5};
  RectF inset = new RectF(0, 0, 0, 0);
  float[] innerR = new float[]{0, 0, 0, 0, 0, 0, 0, 0};
  RoundRectShape roundRectShape = new RoundRectShape(outerR, inset, innerR);
  shapeDrawable.setShape(roundRectShape);
  shapeDrawable.getPaint().setColor(progressColor);
  shapeDrawable.setBounds(0,0,w,h);

I wanted to make a rounded shape and apply it as a background on a layout. This i have under control, but i wanted to make the background like a progressbar.

that is i want it to look something like this image:

image with 2 backgrounds

I want to be able to control where the bg change so i guess it should be done in code.
It must be done in a way so i can place text over it

at the moment i have this code for creating a rounded corner shape:

  int w = this.getWidth() - this.getPaddingLeft() - this.getPaddingRight();
  int h = this.getHeight() - this.getPaddingTop() - this.getPaddingBottom();
  int progressWidth = Math.round((w * percent) / 100);

  ShapeDrawable shapeDrawable = new ShapeDrawable();
  float[] outerR = new float[]{5, 5, 5, 5, 5, 5, 5, 5};
  RectF inset = new RectF(0, 0, 0, 0);
  float[] innerR = new float[]{0, 0, 0, 0, 0, 0, 0, 0};
  RoundRectShape roundRectShape = new RoundRectShape(outerR, inset, innerR);
  shapeDrawable.setShape(roundRectShape);
  shapeDrawable.getPaint().setColor(progressColor);
  shapeDrawable.setBounds(0,0,w,h);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文