The method panel and brush for drawing tables and chairs in Android (2)

Vertex drawing via Canvas and paint

The principle is basically the same, here is based on the coordinate points, and then use the brush to draw circles and lines, draw text, about the text, just at the center of the big circle, get the width and height of the text, and then change the coordinates when the text is drawn.

/**
 * Created by adminZPH on 2016/12/22.
 */
public class MyTableView extends View{
    private int mRadius;
    private int mJianju;
    private int seatnumber=10;//默认是10个防止出错

    private int seatnumbernumber=0;

    private int xx;
    private int yy;

    private Paint p1,p2,p3;
    private int number;
    private Context context;
    public MyTableView(Context context,int seatnumber,int seatnumbernumber,int xx,int yy,int number) {
        this(context,null, 0,seatnumber,seatnumbernumber,xx,yy,number);
    }
    public MyTableView(Context context, AttributeSet attrs, int defStyle,int seatnumber,int seatnumbernumber,int xx,int yy,int number)
    {
        super(context, attrs, defStyle);
        this.context=context;
        this.seatnumber=seatnumber;
        this.seatnumbernumber=seatnumbernumber;
        this.xx= (int) SizeUtils.pxTodp(context,xx);
        this.yy= (int) SizeUtils.pxTodp(context,yy);
        this.mRadius=20;
        this.mJianju=30;
        this.number=number;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 创建画笔
        //首先画大圆
         Paint p = new Paint();
        //设置实心
        p.setStyle(Paint.Style.FILL);
        // 设置红色
        p.setColor(Color.parseColor("#FAE4E7"));
        // 设置画笔的锯齿效果
        p.setAntiAlias(true);
        p.setStrokeWidth(0.5f);
        //绘制
        canvas.drawCircle(xx, yy,mRadius, p);
        //然后画小圆
        for(int i=0;i<seatnumber;++i){
            Paint p1 = new Paint();
            //设置实心
            p1.setStyle(Paint.Style.FILL);
            // 设置红色
            p1.setColor(Color.parseColor("#6e6e6e"));
            // 设置画笔的锯齿效果
            p1.setAntiAlias(true);
            p1.setStrokeWidth(0.5f);
            //绘制
            int cl = (int) (xx+mJianju*Math.sin((360/seatnumber*i)*Math.PI/180));
            int ct = (int) (yy-mJianju*Math.cos((360/seatnumber*i)*Math.PI/180));
            canvas.drawCircle(cl,ct,5, p1);
        }
        //画文字  桌子序列号

        // 绘文字   
        // 设置颜色  
        Paint paint = new Paint();


        paint.setColor(Color.WHITE);
        int sp=(int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,
                12, //多少sp
                context.getResources().getDisplayMetrics());//屏幕的密度
        paint.setTextSize(sp);
        //获得输入字的宽高,保证文字在大圆的中心
        Rect bounds=new Rect();
        paint.getTextBounds("01", 0,1, bounds);
        float x,y;
        x=xx-bounds.width()+2;
        y=yy+bounds.height()/2;
        // 绘文字 
        if(number<10) {
            canvas.drawText("0"+(number+1), x, y,paint);
        }
        else
            canvas.drawText(String.valueOf(number+1), x, y,paint);
    }

Then call in Activity:

 //2代表序号,15个椅子,1个桌子,宽高个100
        MyTableView mMyTableView=new MyTableView(this,15,1,100,100,2);
        mMyTableView.invalidate();//通知重绘
        //然后和以通过addView(mMyTableView)即可

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325400767&siteId=291194637