如何在Android中绘制六边形?

如何在Android中绘制六边形?,第1张

如何在Android中绘制六边形?

试试这个:

import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Path;import android.graphics.Region;import android.util.AttributeSet;import android.view.View;public class HexagonMaskView extends View {    private Path hexagonPath;    private Path hexagonBorderPath;    private float radius;    private float width, height;    private int maskColor;public HexagonMaskView(Context context) {    super(context);    init();}public HexagonMaskView(Context context, AttributeSet attrs) {    super(context, attrs);    init();}public HexagonMaskView(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    init();}private void init() {    hexagonPath = new Path();    hexagonBorderPath = new Path();    maskColor = 0xFF01FF77;}public void setRadius(float r) {    this.radius = r;    calculatePath();}public void setMaskColor(int color) {    this.maskColor = color;    invalidate();}private void calculatePath() {    float triangleHeight = (float) (Math.sqrt(3) * radius / 2);    float centerX = width/2;    float centerY = height/2;    hexagonPath.moveTo(centerX, centerY + radius);    hexagonPath.lineTo(centerX - triangleHeight, centerY + radius/2);    hexagonPath.lineTo(centerX - triangleHeight, centerY - radius/2);    hexagonPath.lineTo(centerX, centerY - radius);    hexagonPath.lineTo(centerX + triangleHeight, centerY - radius/2);    hexagonPath.lineTo(centerX + triangleHeight, centerY + radius/2);    hexagonPath.moveTo(centerX, centerY + radius);    float radiusBorder = radius - 5;        float triangleBorderHeight = (float) (Math.sqrt(3) * radiusBorder / 2);    hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);    hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY + radiusBorder/2);    hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY - radiusBorder/2);    hexagonBorderPath.lineTo(centerX, centerY - radiusBorder);    hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY - radiusBorder/2);    hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY + radiusBorder/2);    hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);    invalidate();}@Overridepublic void onDraw(Canvas c){    super.onDraw(c);    c.clipPath(hexagonBorderPath, Region.Op.DIFFERENCE);    c.drawColor(Color.WHITE);    c.save();    c.clipPath(hexagonPath, Region.Op.DIFFERENCE);    c.drawColor(maskColor);    c.save();}// getting the view size and default radius@Overridepublic void onMeasure(int widthMeasureSpec, int heightMeasureSpec){    super.onMeasure(widthMeasureSpec, heightMeasureSpec);    width = MeasureSpec.getSize(widthMeasureSpec);    height =  MeasureSpec.getSize(heightMeasureSpec);    radius = height / 2 - 10;    calculatePath();}}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5439642.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存