。支支吾吾也没说出来,哎,回家一想太可气 了,就找到方法。原来是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画渐变色三角形
最新推荐文章于 2024-04-28 20:37:52 发布
本文介绍了如何使用 Android 的 Path 类来绘制不同类型的三角形,包括空心、实心及带有渐变色填充的三角形,并展示了如何设置颜色、样式等属性。
621

被折叠的 条评论
为什么被折叠?



