Android 室内定位系列:<1>地图构建

来源:互联网 发布:守望先锋n卡优化 编辑:程序博客网 时间:2024/04/30 02:07

Path类可以预先在View上将N个点连成一条"路径",然后调用Canvas的drawPath(path,paint)即可沿着路径绘制图形

Android还为路径绘制提供了PathEffect来定义绘制效果,PathEffect包含如下子类

ComposePathEffect

CornerPathEffect

DashPathEffect

DiscretePathEffect

PathDashPathEffect

SumPathEffect

下面的示例将会逐一使用上面的绘制效果

[java] view plaincopyprint?
  1. package WangLi.Graphics.PathTest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.ComposePathEffect;  
  8. import android.graphics.CornerPathEffect;  
  9. import android.graphics.DashPathEffect;  
  10. import android.graphics.DiscretePathEffect;  
  11. import android.graphics.Paint;  
  12. import android.graphics.Path;  
  13. import android.graphics.PathDashPathEffect;  
  14. import android.graphics.PathEffect;  
  15. import android.graphics.SumPathEffect;  
  16. import android.os.Bundle;  
  17. import android.view.View;  
  18.   
  19. public class PathTest extends Activity {  
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(new MyView(this));  
  25.     }  
  26.     class MyView extends View  
  27.     {  
  28.         float phase;  
  29.         PathEffect[] effects = new PathEffect[7];  
  30.         int[] colors;  
  31.         private Paint paint;  
  32.         Path path;  
  33.         public MyView(Context context) {  
  34.             super(context);  
  35.             paint = new Paint();  
  36.             paint.setStyle(Paint.Style.STROKE);  
  37.             paint.setStrokeWidth(4);  
  38.             //创建,并初始化Path  
  39.             path = new Path();  
  40.             path.moveTo(00);  
  41.             for(int i = 1; i<= 15; i++)  
  42.             {  
  43.                 //生成15个点,随机生成它们的坐标,并将它们连成一条Path  
  44.                 path.lineTo(i*20, (float)Math.random()*60);  
  45.             }  
  46.             //初始化七个颜色  
  47.             colors = new int[] {  
  48.                     Color.BLACK,Color.BLUE,Color.CYAN,  
  49.                     Color.GREEN,Color.MAGENTA,Color.RED,Color.YELLOW  
  50.             };  
  51.         }  
  52.         protected void onDraw(Canvas canvas)  
  53.         {  
  54.             //将背景填充成白色  
  55.             canvas.drawColor(Color.WHITE);  
  56.             //-------下面开始初始化7中路径的效果  
  57.             //使用路径效果  
  58.             effects[0] = null;  
  59.             //使用CornerPathEffect路径效果  
  60.             effects[1] = new CornerPathEffect(10);  
  61.             //初始化DiscretePathEffect  
  62.             effects[2] = new DiscretePathEffect(3.0f,5.0f);  
  63.             //初始化DashPathEffect  
  64.             effects[3] = new DashPathEffect(new float[]{20,10,5,10},phase);  
  65.             //初始化PathDashPathEffect  
  66.             Path p = new Path();  
  67.             p.addRect(0088, Path.Direction.CCW);  
  68.             effects[4] = new PathDashPathEffect(p,12,phase,PathDashPathEffect.Style.ROTATE);  
  69.             //初始化PathDashPathEffect  
  70.             effects[5] = new ComposePathEffect(effects[2],effects[4]);  
  71.             effects[6] = new SumPathEffect(effects[4],effects[3]);  
  72.             //将画布移到8,8处开始绘制  
  73.             canvas.translate(88);  
  74.             //依次使用7中不同路径效果,7种不同的颜色来绘制路径  
  75.             for(int i = 0; i < effects.length; i++)  
  76.             {  
  77.                 paint.setPathEffect(effects[i]);  
  78.                 paint.setColor(colors[i]);  
  79.                 canvas.drawPath(path, paint);  
  80.                 canvas.translate(060);  
  81.             }  
  82.             //改变phase值,形成动画效果  
  83.             phase += 1;  
  84.             invalidate();  
  85.         }  
  86.     }  
  87. }  
效果如下

0 0
原创粉丝点击