java - 如何在 android studio 上构建六边形作为按钮?

标签 java android android-studio button

我正在开发一个“卡坦岛定居者”应用程序,但我遇到了一个很大的图形问题:游戏板包含一个必须是按钮的六边形。我在 youtube 上搜索,但没有找到任何绘制六角形按钮的方法。 有谁知道如何制作一个六边形按钮?

最佳答案

相关于:How to draw hexagons in android?试试这个类(class):

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();
}

@Override
public 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
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width = MeasureSpec.getSize(widthMeasureSpec);
    height =  MeasureSpec.getSize(heightMeasureSpec);
    radius = height / 2 - 10;
    calculatePath();
}
}

关于java - 如何在 android studio 上构建六边形作为按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47183835/

相关文章:

android - 谷歌播放服务更新

android - Gradle:构建 flavor 时从 src/main 中排除包

android - 不同 api 级别的 Gradle 依赖项

Java程序通过socket接口(interface)与邮件服务器建立TCP连接并发送邮件

java - 是否可以确定Selenium WebElements的html代码顺序

android - "ConvertResourcesCases"任务意外失败。 Xamarin Android 构建

android-studio - Android studio 1.0 不显示 drawable-hdpi , drawable-xhdpi, drawable-mdpi,drawable-xxhdpi

java - 检查 2 个数字是否在彼此的 1% 以内

java - 用于操作数据库的类,可在 Android 和 Java 桌面上运行

android - Android 开发人员是否通常在 iPhone 上使用 Three20 等第 3 方 UI/网络库?