Android Path 使用

 

Android Path 使用


项目中经常会用到绘图方面的知识,之前一直对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画出来的

[java]  view plain copy
  1. public class WaterMark extends Activity{  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(new WaterMarkView(this));  
  8.     }  
  9.       
  10.     private class WaterMarkView extends View{  
  11.         private Bitmap mBitmap;  
  12.         private Context mContext;  
  13.         private Paint mPaint;  
  14.         private static final String WATER_MARK_STRING= "HYF_AN_E V2.6.3 Demo";  
  15.   
  16.         public WaterMarkView(Context context) {  
  17.             super(context);  
  18.             mContext = context;  
  19.             mPaint = new Paint();  
  20.             mPaint.setAntiAlias(true);  
  21.             mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.my_image);  
  22.         }  
  23.           
  24.         @Override  
  25.         protected void onDraw(Canvas canvas) {  
  26.             // TODO Auto-generated method stub  
  27.             super.onDraw(canvas);  
  28.               
  29.             canvas.drawBitmap(mBitmap, 00, mPaint);  
  30.             drawWaterMark(canvas,getWidth(),getHeight());  
  31.         }  
  32.   
  33.         private void drawWaterMark(Canvas canvas, int width, int height) {  
  34.             int fontSize = 35;  
  35.             Path path = new Path();  
  36.             path.moveTo(0, height);  
  37.             path.lineTo(width, 0);  
  38.             path.close();  
  39.               
  40.             Paint paint = new Paint();  
  41.             paint.setColor(0x88ff0000);  
  42.             paint.setTextSize(fontSize);  
  43.             paint.setAntiAlias(true);  
  44.             paint.setDither(true);  
  45.             Rect bounds = new Rect();  
  46.             paint.getTextBounds(WATER_MARK_STRING, 0, WATER_MARK_STRING.length(), bounds);  
  47.               
  48.             int length = (int)Math.sqrt(width*width + height*height);  
  49.             int hOffset = (length - (bounds.right - bounds.left)) / 2;  
  50.             canvas.drawTextOnPath(WATER_MARK_STRING, path, hOffset, fontSize/2, paint);  
  51.         }  
  52.     }  
  53. }  

后一种效果是先画水印然后画图片,所以画的时候是有顺序的:

[java]  view plain copy
  1. drawWaterMark(canvas,getWidth(),getHeight());  
  2. canvas.drawBitmap(mBitmap, 00, mPaint);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值