简体   繁体   中英

how to make hexagon shaped button in android?

我需要在android中制作具有六边形图像的按钮。从一些教程中我知道我们需要创建一个类,并且需要提供该类的名称来代替在xml文件中编写按钮。开始?

EDIT: Using an image of a hexagon with transparent background will solve the problem. The image will appear as hexagon, although it is a rectangle.

You can call the method setOnClickListener() on any view, since it belongs to the view class. Therefore you can create an ImageView that fits your needs and then call setOnClickListener to use it as a button.

Try this code:

public ShapeDrawable myFunction(){
        Path path = new Path();
        float stdW = 100;
        float stdH = 100;
        float w3 = stdW / 3;
        float h2 = stdH / 2;
        path.moveTo(0, h2);
        h2 -= 6 / 2;
        path.rLineTo(w3, -h2);         path.rLineTo(w3, 0); path.rLineTo(w3, h2);
        path.rLineTo(-w3, h2); path.rLineTo(-w3, 0); path.rLineTo(-w3, -h2);
        Shape s = new PathShape(path, stdW, stdH);
        ShapeDrawable d = new ShapeDrawable(s);
        Paint p = d.getPaint();
        p.setColor(0xffeeeeee);
        p.setStyle(Style.STROKE);
        p.setStrokeWidth(6);

        return d;

    }

In your onClick:

 someView.setBackgroundDrawable(myFunction());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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