canvas教程

如何给自定义多边形view填充颜色

字号+ 作者:H5之家 来源:H5之家 2018-01-19 14:00 我要评论( )

一下是我的多边形的代码 package com.example.bingyantest; import android.content.ContentValues; import android.content.Context; import android.graphics.

如何给自定义多边形view填充颜色

canvas android

一下是我的多边形的代码
package com.example.bingyantest;

import android.content.ContentValues;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**

  • Created by HP on 2017/3/6.
  • 算法思想:每画出一条边旋转一次画布,最终旋转N次,画出N条边
  • 边的计算方法:利用多边形对应的外接园半径 R */
  • public class MyView extends View {

    private Paint mpaint,npaint; private float radius, start_x, start_y, end_x, end_y; private float m = 30; private float UNIT = 360 / m; private float Xo,Yo; //用于保存起始点的数据 public MyView(Context context) { this(context, null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Log.d("222", "MyView: "+UNIT); mpaint = new Paint(); npaint = new Paint(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); float mW = getMeasuredWidth(); float mH = getMeasuredHeight(); start_x = mW / 2; start_y = mH / 2; Xo = start_x; Yo = start_y; radius = Math.min(mW, mH) / 3; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mpaint.setStyle(Paint.Style.STROKE); mpaint.setStrokeWidth(6); if (m > 24) { npaint.setColor(Color.RED); canvas.drawCircle(start_x,start_y,radius,npaint); canvas.drawCircle(start_x, start_y, radius, mpaint); } else { end_x = (float) (radius * Math.sin(Math.toRadians(UNIT))); end_x = start_x + end_x; end_y = (float) (radius * Math.cos(Math.toRadians(UNIT))); end_y = start_y - end_y; for (int i = 0; i < m; i++) { canvas.save(); canvas.rotate(UNIT * i, start_x, start_y); canvas.drawLine(start_x, start_y - radius, end_x, end_y, mpaint); canvas.restore(); } } } public void refresh(float m) { this.m = m; UNIT = 360 / this.m; invalidate(); }

    }

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • html5 canvas 微信海报分享

      html5 canvas 微信海报分享

      2018-01-19 15:06

    • Android画图学习总结(五)

      Android画图学习总结(五)

      2018-01-19 11:08

    • Android画图学习总结(五)

      Android画图学习总结(五)

      2018-01-19 11:08

    • canvas分类文章列表

      canvas分类文章列表

      2018-01-19 11:06

    网友点评