Android图形图像之使用Path类

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

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

ComposePathEffect

CornerPathEffect

DashPathEffect

DiscretePathEffect

PathDashPathEffect

SumPathEffect

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

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

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.SumPathEffect;
import android.os.Bundle;
import android.view.View;

public class PathTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }
    class MyView extends View
    {
		float phase;
    	PathEffect[] effects = new PathEffect[7];
    	int[] colors;
    	private Paint paint;
    	Path path;
    	public MyView(Context context) {
			super(context);
			paint = new Paint();
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(4);
			//创建,并初始化Path
			path = new Path();
			path.moveTo(0, 0);
			for(int i = 1; i<= 15; i++)
			{
				//生成15个点,随机生成它们的坐标,并将它们连成一条Path
				path.lineTo(i*20, (float)Math.random()*60);
			}
			//初始化七个颜色
			colors = new int[] {
					Color.BLACK,Color.BLUE,Color.CYAN,
					Color.GREEN,Color.MAGENTA,Color.RED,Color.YELLOW
			};
		}
    	protected void onDraw(Canvas canvas)
    	{
    		//将背景填充成白色
    		canvas.drawColor(Color.WHITE);
    		//-------下面开始初始化7中路径的效果
    		//使用路径效果
    		effects[0] = null;
    		//使用CornerPathEffect路径效果
    		effects[1] = new CornerPathEffect(10);
    		//初始化DiscretePathEffect
    		effects[2] = new DiscretePathEffect(3.0f,5.0f);
    		//初始化DashPathEffect
    		effects[3] = new DashPathEffect(new float[]{20,10,5,10},phase);
    		//初始化PathDashPathEffect
    		Path p = new Path();
    		p.addRect(0, 0, 8, 8, Path.Direction.CCW);
    		effects[4] = new PathDashPathEffect(p,12,phase,PathDashPathEffect.Style.ROTATE);
    		//初始化PathDashPathEffect
    		effects[5] = new ComposePathEffect(effects[2],effects[4]);
    		effects[6] = new SumPathEffect(effects[4],effects[3]);
    		//将画布移到8,8处开始绘制
    		canvas.translate(8, 8);
    		//依次使用7中不同路径效果,7种不同的颜色来绘制路径
    		for(int i = 0; i < effects.length; i++)
    		{
    			paint.setPathEffect(effects[i]);
    			paint.setColor(colors[i]);
    			canvas.drawPath(path, paint);
    			canvas.translate(0, 60);
    		}
    		//改变phase值,形成动画效果
    		phase += 1;
    		invalidate();
    	}
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值