如何用android 画出三角形
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用android 画出三角形相关的知识,希望对你有一定的参考价值。
1、设置View,重写protected void onDraw(Canvas canvas)方法2、使用onDraw中的canvas,通过Path设置三角形的三个顶点,通过drawPath方式绘制三角形
示例:
@Override
protected void onDraw(Canvas canvas)
super.onDraw(canvas);
canvas.drawText("画三角形:", 10, 200, p);
// 绘制这个三角形,你可以绘制任意多边形
Path path = new Path();
path.moveTo(80, 200);// 此点为多边形的起点
path.lineTo(120, 250);
path.lineTo(80, 250);
path.close(); // 使这些点构成封闭的多边形
canvas.drawPath(path, p);
参考技术A 。支支吾吾也没说出来,哎,回家一想太可气 了,就找到方法。原来是android.graphics.Path这个类,实现的。Api里的描述如下:The Path class encapsulates compound (multiple contour) geometric paths consisting of straight line segments, quadratic curves, and cubic curves. It can be drawn with canvas.drawPath(path, paint), either filled or stroked (based on the paint's Style), or it can be used for clipping or to draw text on a path. 它大概的意思是说path类包含了多种组件,比如直线段,二次曲线,三次曲线,它可以在画布上绘制,无论是填充或笔画。下面举例说明:package cn.lgl.draw; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Shader; import android.view.View; public class MyView extends View public MyView(Context context) super(context); // TODO Auto-generated constructor stub @Override protected void onDraw(Canvas canvas) // TODO Auto-generated method stub super.onDraw(canvas); /*设置背景为白色*/ canvas.drawColor(Color.WHITE); Paint paint=new Paint(); /*去锯齿*/ paint.setAntiAlias(true); /*设置paint的颜色*/ paint.setColor(Color.RED); /*设置paint的 style 为STROKE:空心*/ paint.setStyle(Paint.Style.STROKE); /*设置paint的外框宽度*/ paint.setStrokeWidth(3); /*画一个空心三角形*/ Path path=new Path(); path.moveTo(10,330); path.lineTo(70,330); path.lineTo(40,100); path.close(); canvas.drawPath(path, paint); /*设置paint 的style为 FILL:实心*/ paint.setStyle(Paint.Style.FILL); /*设置paint的颜色*/ paint.setColor(Color.BLUE); /*画一个实心三角形*/ Path path2=new Path(); path2.moveTo(90,330); path2.lineTo(150,330); path2.lineTo(120,270); path2.close(); canvas.drawPath(path2, paint); Shader mShader=new LinearGradient(0,0,100,100, new int[]Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW, null,Shader.TileMode.REPEAT); paint.setShader(mShader); /*画一个渐变色三角形*/ Path path4=new Path(); path4.moveTo(170,330); path4.lineTo(230,330); path4.lineTo(200,270); path4.close(); canvas.drawPath(path4,paint); 本回答被提问者采纳
以上是关于如何用android 画出三角形的主要内容,如果未能解决你的问题,请参考以下文章