简体   繁体   English

Android使用XML创建形状的弧形矩形

[英]android create arc rectangle in shape using xml

I have tried to create rectangle like this, in xml with shape but no luck, is this possible or how to transparent this image, I mean it's color want to transparent like we define color for any shape with alpha then we get transparency, is there any way for 1 of this? 我试图在具有形状但没有运气的xml中创建这样的矩形,这是否可能或如何透明化此图像,我的意思是它的颜色想要透明,就像我们为任何带有alpha的形状定义颜色然后我们获得透明性,是否存在任何一种方法?

Thanks 谢谢

在此处输入图片说明

I was able to do this by making the color of the clipped region (a circle in my case) same to the background color. 通过将剪切区域(在我的情况下为圆形)的颜色与背景颜色相同,可以做到这一点。

float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
float[] circleR = new float[] { 50, 50, 50, 50, 50, 50, 50, 50 };

 mDrawables = new ShapeDrawable[2]; mDrawables[0] = new ShapeDrawable(new RoundRectShape(outerR, null, null)); mDrawables[1] = new ShapeDrawable(new RoundRectShape(circleR, null, null)); mDrawables[0].getPaint().setColor(0xFF0000FF); mDrawables[1].getPaint().setColor(Color.GRAY); 



And in onDraw: 在onDraw中:

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.GRAY);
    int x = 10;<br>
    int y = 10;<br>
    int width = 100;<br>
    int height = 100;

    for (int i = 0; i < mDrawables.length; i++) {
    Drawable dr = mDrawables[i];
    if (i == 0) {
            dr.setBounds(x, y, x + width, y + height);
            dr.draw(canvas);
        } else {
            x = 10 + 75;
            y = 10 + 75;
            dr.setBounds(x, y, x + 50, y + 50);
            dr.draw(canvas);
        }
    }
    canvas.save();
}

在我的乳化机上看起来像这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM