永发信息网

如何用android 画出三角形

答案:2  悬赏:40  手机版
解决时间 2021-04-27 19:48
  • 提问者网友:戎马万世
  • 2021-04-27 06:19
如何用android 画出三角形
最佳答案
  • 五星知识达人网友:北方的南先生
  • 2021-04-27 07:44
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);
}
全部回答
  • 1楼网友:雾月
  • 2021-04-27 09:10
。支支吾吾也没说出来,哎,回家一想太可气 了,就找到方法。原来是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.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); 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.setStyle(Paint.Style.FILL); 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); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯