How to create a custom shape for a Button

此生再无相见时 提交于 2019-12-08 03:12:26

问题


How to create a custom shape for a Button? Can I set different colors to button background colors for many conditions.Any help will be appreciated.Thanks.


回答1:


How to create a custom shape for a Button?

Create a test.xml file in drawable and add code:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/one">       //<---- id is must for color change
    <shape android:shape="rectangle">
        <size
            android:width="150dp"
            android:height="40dp" />
        <solid android:color="@color/colorPrimary" />
        <corners android:radius="10dp"/> //<---- remove radius if not needed
    </shape>
</item>

<item
    android:top="10dp"
    android:bottom="-12dp"
    android:left="-55dp"
    android:width="75dp">
    <rotate
        android:fromDegrees="-47">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>
</layer-list>

In button:

     <Button
        .....
        android:background="@drawable/test"
        ...../>

Output

Can I set different colors to button background colors for many conditions?

Yes, you can change at run-time. For that you have to assign id to each item to know where you want to make changes. As I did on my answer code.

LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_shape_test);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.id_of_drawable-one);
gradientDrawable.setColor(your_color);



回答2:


Try this code:

public class ButtonBackGroundDrawable extends Drawable {
private Paint paint;

public ButtonBackGroundDrawable() {
    paint = new Paint();
    paint.setAntiAlias(true);
}

@Override
public void draw(Canvas canvas) {

    int height = getBounds().height();
    int width = getBounds().width();
    RectF rect = new RectF(0.0f, 0.0f, width, height);
    Point point0_draw = new Point(0, 0);
    Point point1_draw = new Point(width, 0);
    Point point2_draw = new Point(width, height);
    Point point3_draw = new Point(0, height);
    Point point4_draw = new Point(width / 10, height / 2);

    Path path = new Path();
    path.moveTo(point0_draw.x, point0_draw.y);
    path.lineTo(point1_draw.x, point1_draw.y);
    path.lineTo(point2_draw.x, point2_draw.y);
    path.lineTo(point3_draw.x, point3_draw.y);
    path.lineTo(point4_draw.x, point4_draw.y);
    path.lineTo(point0_draw.x, point0_draw.y);
    path.close();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.CYAN);
 //        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawPath(path, paint);
}

@Override
public void setAlpha(int alpha) {
    paint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter colorFilter) {
    paint.setColorFilter(colorFilter);
}

@Override
public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
}
}


来源:https://stackoverflow.com/questions/41971893/how-to-create-a-custom-shape-for-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!