Skip to content Skip to sidebar Skip to footer

Cut Part Of Xml Shape

I have a rectangle shape view in XML:

Solution 1:

use a ShapeDrawable like this:

Drawabled=newShapeDrawable(newS(Color.BLUE, 32));

where class S is a custom Shape:

classSextendsShape {
    finalint color;
    finalfloat radius;
    Pathpath=newPath();

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

    @OverrideprotectedvoidonResize(float width, float height) {
        path.reset();
        path.moveTo(0, 0);
        path.lineTo(width, height);
        path.lineTo(radius, height);
        RectFoval=newRectF(0, height - 2 * radius, 2 * radius, height);
        path.arcTo(oval, 90, 90);
        path.close();
    }

    @Overridepublicvoiddraw(Canvas canvas, Paint paint) {
        paint.setColor(color);
        canvas.drawPath(path, paint);
    }
}

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

Solution 2:

Play around bit ,As of API 21 you can use vector drawables

Here is what i did, change numbers as you need . created a drawable my_custom_shape.xml as resource file

<?xml version="1.0" encoding="utf-8"?><vectorxmlns:android="http://schemas.android.com/apk/res/android"android:height="100dp"android:width="100dp"android:viewportHeight="100"android:viewportWidth="45" ><groupandroid:name="triableGroup"><pathandroid:name="triangle"android:fillColor="#000"android:pathData="m 0,0 l 50,100 -50,0 z" /></group></vector>

out put:

enter image description here

and in your View set this as backround android:background="@drawable/my_custom_shape"

Note : forgot to try use px instead of dp and see will help to keep the same size for every device , try and see

Post a Comment for "Cut Part Of Xml Shape"