切割XML形状的一部分 [英] Cut part of XML shape

查看:84
本文介绍了切割XML形状的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML矩形视图:

I have a rectangle shape view in XML:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"> 

        <solid android:color="@color/lightblue"/> 
    </shape>

我想要的东西被切成两半,所以结果看起来像:

What I want is cut it in half, so result will looks like:

有可能吗?如果是的话,我该如何实现?

Is it possible? And if yes, how do I achieve it?

注意:

1)添加旋转的白色矩形不是解决方案.我需要保持蓝色形状的切割区域透明(下面有更多的View层).

1) Adding rotated white rectangle isn't solution. I need to keep cutted area of the blue shape transparent (there are more View layers under it).

2)rect的左下角有点圆(我忘了在上图中绘制它).

2) Bottom left corner of rect is a bit rounded (I forgot to draw it in image above).

推荐答案

使用ShapeDrawable像这样:

Drawable d = new ShapeDrawable(new S(Color.BLUE, 32));

其中类S是自定义Shape:

class S extends Shape {
    final int color;
    final float radius;
    Path path = new Path();

    public S(int color, float radius) {
        this.color = color;
        this.radius = radius;
    }

    @Override
    protected void onResize(float width, float height) {
        path.reset();
        path.moveTo(0, 0);
        path.lineTo(width, height);
        path.lineTo(radius, height);
        RectF oval = new RectF(0, height - 2 * radius, 2 * radius, height);
        path.arcTo(oval, 90, 90);
        path.close();
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        paint.setColor(color);
        canvas.drawPath(path, paint);
    }
}

现在您可以在任何对View#setBackground()TextView#setCompoundDrawables()等的调用中使用Drawable d

now you can use Drawable d in any call to View#setBackground(), TextView#setCompoundDrawables() etc

这篇关于切割XML形状的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆