前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android N 边形 View

Android N 边形 View

作者头像
萬物並作吾以觀復
发布2021-06-09 22:18:21
4150
发布2021-06-09 22:18:21
举报
文章被收录于专栏:指尖下的Android指尖下的Android

需求千奇百怪,对于登录后账户的头像,要求圆形、多边形、带描边、带 VIP 标示等等,所以实现一个 N 边形玩玩,效果和代码如下。

多边形各顶点计算公式如下:
代码语言:javascript
复制
(1)正多边形的中心点为(0,0)
for (i = 0; i < n; i++) {
  printf("%f %f\n", r * Math.cos(2 * Math.PI * i / n), r * Math.sin(2 * Math.PI * i / n));
}
x轴坐标为 r * Math.cos(2 * Math.PI * i / n)
y轴坐标为 r * Math.sin(2 * Math.PI * i / n)

(2)正多边形的中心点为(a,b)
for (i = 0; i < n; i++) {
  printf("%f %f\n",a + r * Math.cos(2 * Math.PI * i / n), b+ r * Math.sin(2 * Math.PI * i / n));
}

x轴坐标为 a + r * Math.cos(2 * Math.PI * i / n)
y轴坐标为 b + r * Math.sin(2 * Math.PI * i / n)

算法原文链接:https://blog.csdn.net/wuprogrammer/article/details/108903244

12 边形

代码语言:javascript
复制
public class TestView extends View {

    private static final int DEF_SIDE_LENGTH = 6;
    private static final int DEF_SIZE = 66;
    private int sideLength;
    private Paint paint;
    private Context context;
    private Path path;
    private int bgRes;
    private int radius;

    public TestView(Context context) {
        super(context);
        init(context, null);
    }

    public TestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TestView);
        sideLength = typedArray.getInt(R.styleable.TestView_side_length, DEF_SIDE_LENGTH);
        bgRes = typedArray.getResourceId(R.styleable.TestView_bg_res, 0);
        typedArray.recycle();
        if(bgRes != 0){
            initPaint();
        }
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, android.R.color.black));
        paint.setStrokeWidth(10f);
        paint.setAntiAlias(true);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), bgRes);
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(shader) ;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(bgRes == 0){
            return;
        }
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if(widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY){
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            radius = Math.min(width, height) / 2;
        }else {
            float scale = context.getResources().getDisplayMetrics().density;
            int size = (int) (DEF_SIZE * scale + 0.5f);
            radius = size / 2;
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
            setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        }
        initPoint();
    }


    public void setSideLength(int sideLength) {
        this.sideLength = sideLength;
        initPoint();
        invalidate();
    }

    private void initPoint() {
        if(null == path){
            path = new Path();
        }
        path.reset();
        for (int i = 1; i <= sideLength; i++) {
           float x = (float)(radius * Math.cos(2 * Math.PI * i / sideLength)) + radius;
            float y = (float)(radius * Math.sin(2 * Math.PI * i / sideLength)) + radius;
            if(i == 1){
                path.moveTo(x, y);
            }else {
                path.lineTo(x, y);
            }
        }
        path.close();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(bgRes != 0){
            canvas.drawPath(path, paint);
        }
    }

}

自定义属性如下

代码语言:javascript
复制
 <declare-styleable name="TestView">
        <attr name="side_length" format="integer" />
        <attr name="bg_res" format="reference" />
    </declare-styleable>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 多边形各顶点计算公式如下:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档