栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Android中绘制六边形?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464146.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号