I want to create Hexagon shape for my project so I want to create that shape in .xml format so how can i create.
While most solutions would involve including the ShapeImageView (which is a great library, btw), you can always write your own logic to create a custom hexagon shaped layout.
All you need to do is define the properties of the Path
object and then use that in the onDraw() method of the layout using the Canvas.
This is how you would create the hexagon path.
float midx = getWidth() / 2;
float midy = getHeight() / 2;
Path p = new Path();
p.moveTo(midx, midy);
p.lineTo(midx+150, midy + 220);
p.lineTo(midx, midy + 220);
p.lineTo(midx-150, midy + 220);
p.lineTo(midx-300, midy);
p.lineTo(midx-150, midy-220);
p.lineTo(midx+150, midy-220);
p.lineTo(midx+300, midy);
p.lineTo(midx+150, midy + 220);
return p;
Now, in your custom hexagon layout, use this path in onDraw().
@Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
clipPath.addPath(p); //p is the path you created above
canvas.clipPath(clipPath);
canvas.drawColor(Color.RED); //optional
super.onDraw(canvas)
}
Once you have your custom layout ready, you can set the background of the layout to any drawable you want (just as you would do for any other layouts).