项目中经常会用到绘图方面的知识,之前一直对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.
为程序添加水印的效果,就是通过onDraw()然后根据Path画出来的
public class WaterMark extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(new WaterMarkView(this)); } private class WaterMarkView extends View{ private Bitmap mBitmap; private Context mContext; private Paint mPaint; private static final String WATER_MARK_STRING= "HYF_AN_E V2.6.3 Demo"; public WaterMarkView(Context context) { super(context); mContext = context; mPaint = new Paint(); mPaint.setAntiAlias(true); mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.my_image); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawBitmap(mBitmap, 0, 0, mPaint); drawWaterMark(canvas,getWidth(),getHeight()); } private void drawWaterMark(Canvas canvas, int width, int height) { int fontSize = 35; Path path = new Path(); path.moveTo(0, height); path.lineTo(width, 0); path.close(); Paint paint = new Paint(); paint.setColor(0x88ff0000); paint.setTextSize(fontSize); paint.setAntiAlias(true); paint.setDither(true); Rect bounds = new Rect(); paint.getTextBounds(WATER_MARK_STRING, 0, WATER_MARK_STRING.length(), bounds); int length = (int)Math.sqrt(width*width + height*height); int hOffset = (length - (bounds.right - bounds.left)) / 2; canvas.drawTextOnPath(WATER_MARK_STRING, path, hOffset, fontSize/2, paint); } } } 

后一种效果是先画水印然后画图片,所以画的时候是有顺序的:
drawWaterMark(canvas,getWidth(),getHeight()); canvas.drawBitmap(mBitmap, 0, 0, mPaint);
本文详细介绍了如何在Android应用中利用Path类实现添加水印的效果,包括创建Path对象、设置画笔属性、绘制位图以及在特定路径上绘制文本字符串。通过逐步演示,展示了从创建水印视图到在界面上实际应用的过程,适用于Android开发者学习路径绘制的基本技巧。

524

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



