博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
绘制多边形
阅读量:5806 次
发布时间:2019-06-18

本文共 4791 字,大约阅读时间需要 15 分钟。

hot3.png

import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.drawable.ShapeDrawable;import android.graphics.drawable.shapes.OvalShape;import android.graphics.drawable.shapes.PathShape;import android.graphics.drawable.shapes.RectShape;import android.view.View;/** * @version 2012-8-10 上午10:25:09 **/public class GameView extends View implements Runnable {    Paint mPaint = null;    GameView2 mGameView2 = null;    public GameView(Context context) {        super(context);        mPaint = new Paint();        mGameView2 = new GameView2(context);        new Thread(this).start();    }    @Override    public void run() {        // 判读该线程是否中断        while(!Thread.currentThread().isInterrupted()) {            try {                Thread.sleep(100);            }            catch(Exception e) {                Thread.currentThread().interrupt();            }            // 使用postInvalidate可以直接在线程中更新界面            postInvalidate();        }    }    @Override    protected void onDraw(Canvas canvas) {        // 设置画布颜色        canvas.drawColor(Color.BLACK);        // 取消锯齿        mPaint.setAntiAlias(true);        // 画空心        mPaint.setStyle(Paint.Style.STROKE);        {            // 定义矩形            Rect rect1 = new Rect();            // 设置矩形大小            rect1.left = 5;            rect1.top = 5;            rect1.bottom = 25;            rect1.right = 45;            mPaint.setColor(Color.BLUE);            // 绘制矩形            canvas.drawRect(rect1, mPaint);            mPaint.setColor(Color.YELLOW);            // 绘制圆形(圆心x坐标,圆心y坐标,半径r,p)            canvas.drawCircle(40, 70, 30, mPaint);            // 椭圆形            RectF rectf1 = new RectF();            rectf1.left = 80;            rectf1.top = 30;            rectf1.right = 150;            rectf1.bottom = 70;            mPaint.setColor(Color.LTGRAY);            canvas.drawOval(rectf1, mPaint);            // 绘制多边形            Path path1 = new Path();            // 起始点            path1.moveTo(150 + 5, 80 - 50);            path1.lineTo(150 + 45, 80 - 50);            path1.lineTo(150 + 30, 120 - 50);            path1.lineTo(150 + 20, 120 - 50);            // 是这些点构成封闭的多边形            path1.close();            mPaint.setColor(Color.GRAY);            canvas.drawPath(path1, mPaint);            mPaint.setColor(Color.RED);            // 设置线的宽度            mPaint.setStrokeWidth(3);            // 划线            canvas.drawLine(5, 110, 315, 110, mPaint);        }        mGameView2.DrawShape(canvas);    }    public class GameView2 extends View {        // 声明ShapeDrawable对象        ShapeDrawable mShapeDrawable = null;        public GameView2(Context context) {            super(context);        }        public void DrawShape(Canvas canvas) {            // 实例化ShapeDrawable对象并说明是绘制一个矩形            mShapeDrawable = new ShapeDrawable(new RectShape());            // 得到画笔Paint对象并设置其颜色            mShapeDrawable.getPaint().setColor(Color.RED);            Rect bounds = new Rect(15, 250, 55, 280);            // 设置图像显示区域            mShapeDrawable.setBounds(bounds);            // 绘制图像            mShapeDrawable.draw(canvas);            /* =========================== */            // 绘制椭圆形            mShapeDrawable = new ShapeDrawable(new OvalShape());            // 设置画笔颜色            mShapeDrawable.getPaint().setColor(Color.GREEN);            // 设置显示区域            mShapeDrawable.setBounds(70, 250, 150, 280);            // 绘制图像            mShapeDrawable.draw(canvas);            // 绘制多边形            Path path1 = new Path();            path1.moveTo(150 + 5, 80 + 80 - 50);            path1.lineTo(150 + 45, 80 + 80 - 50);            path1.lineTo(150 + 30, 80 + 120 - 50);            path1.lineTo(150 + 20, 80 + 120 - 50);            path1.close();            // 设置宽高            mShapeDrawable = new ShapeDrawable(new PathShape(path1, 150, 150));            mShapeDrawable.getPaint().setColor(Color.BLUE);            // 设置显示区域            mShapeDrawable.setBounds(100, 170, 200, 280);            // 绘制图像            mShapeDrawable.draw(canvas);        }    }}
// 矩形Rect rect = new Rect();rect.top = 5;rect.left = 5;rect.bottom = 100;rect.right = 100;canvas.drawRect(rect, mPaint);// 圆形cx,cy为圆心坐标,radius为半径canvas.drawCircle(160, 60, 50, mPaint);// 椭圆RectF rectF = new RectF();rectF.top = 5;rectF.left = 230;rectF.bottom = 100;rectF.right = 400;canvas.drawOval(rectF, mPaint);// 任意多边形Path path = new Path();path.moveTo(5, 120);path.lineTo(100, 150);path.lineTo(130, 250);path.close();canvas.drawPath(path, mPaint);// 划线canvas.drawLine(5, 300, 50, 400, mPaint);// 画点canvas.drawPoint(200, 400, mPaint);

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/u/2406628/blog/473371

你可能感兴趣的文章
shell编程笔记六:实现ll命令
查看>>
【SAP HANA】关于SAP HANA中带层次结构的计算视图Cacultation View创建、激活状况下在系统中生成对象的研究...
查看>>
[nodejs] nodejs开发个人博客(五)分配数据
查看>>
《Linux内核修炼之道》 之 高效学习Linux内核
查看>>
Java数据持久层框架 MyBatis之API学习九(SQL语句构建器详解)
查看>>
30分钟Git命令“从入门到放弃”
查看>>
nginx : TCP代理和负载均衡的stream模块
查看>>
MYSQL数据库间同步数据
查看>>
DevOps 前世今生 | mPaaS 线上直播 CodeHub #1 回顾
查看>>
iOS 解决UITabelView刷新闪动
查看>>
行为型模式:观察者模式
查看>>
让前端小姐姐愉快地开发表单
查看>>
Dubbo笔记(四)
查看>>
Web前端JQuery入门实战案例
查看>>
Android开发教程 - 使用Data Binding(一) 介绍
查看>>
java B2B2C Springboot电子商城系统- SSO单点登录之OAuth2.0 登出流程(3)
查看>>
12月26日云栖精选夜读:CDN新品发布:阿里云SCDN安全加速开放公测
查看>>
USB 通信原理
查看>>
7zZip zip RAR iOS
查看>>
date命令的详细用法!
查看>>