• android学习笔记43——图形图像处理3——Path


    Path类

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

    android还为路径绘制提供了PathEffect来定义绘制效果,PathEffect包含如下子类——每一个子类代表一种绘制方法:

      1.ComposePathEffect

      2.CnonerPathEffect

      3.DashPathEffect
      4.DiscretePathEffect

      5.PathDashPathEffect

      6.SumPathEffect

    实例如下:

    代码实现==》
    package com.example.mypath;
    
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    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.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity
    {
    	@Override
    	protected 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(10);
    			// 创建并初始化Path
    			path = new Path();
    			path.moveTo(0, 0);
    			for (int i = 0; i <= 15; i++)
    			{
    				// 生成15个点,随记生成他们的Y坐标,并将它们连成一条Path
    				path.lineTo(i * 30, (float) Math.random() * 100);
    			}
    			// 初始化7个颜色
    			colors = new int[] { Color.BLACK, Color.YELLOW, Color.GRAY, Color.GREEN, Color.BLUE,
    					Color.RED, Color.CYAN };
    		}
    
    		@SuppressLint("DrawAllocation")
    		@Override
    		protected void onDraw(Canvas canvas)
    		{
    			// 将背景色填充为白色
    			canvas.drawColor(Color.WHITE);
    
    			// 初始化7种路径效果
    			effects[0] = null;// 不使用路径效果
    			effects[1] = new CornerPathEffect(10);// 使用CornerPathEffect路径效果
    			effects[2] = new DiscretePathEffect(3.0f, 5.0f);// 初始化DiscretePathEffect
    			effects[3] = new DashPathEffect(new float[] { 20, 10, 5, 10 }, phase);// 初始化DashPathEffect
    
    			Path p = new Path();
    			p.addRect(0, 0, 8, 8, Path.Direction.CCW);
    			effects[4] = new PathDashPathEffect(p, 123, phase, PathDashPathEffect.Style.ROTATE);// 初始化PathDashPathEffect
    			effects[5] = new ComposePathEffect(effects[2], effects[4]);// 初始化ComposePathEffect
    			effects[6] = new SumPathEffect(effects[4], effects[3]);// 初始化SumPathEffect
    
    			// 依次使用7种不同效果路径、7种不同的颜色来绘制路径
    			for (int j = 0; j < effects.length; j++)
    			{
    				paint.setPathEffect(effects[j]);
    				paint.setColor(colors[j]);
    				canvas.drawPath(path, paint);
    				canvas.translate(0, 120);
    			}
    			// 改变phase值,形成动画效果
    			phase++;
    			invalidate();
    		}
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu)
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    

    运行效果如下:

    注意:如以上程序,当定义DashPathEffect、PathDashPathEffect时可指定一个phase参数,该参数用于指定路径效果的相位,当该参数

    改变时,绘制效果也略有变化。上面程序不停的改变phase参数,并不停的重绘该View组件——将参数如上图所示动画效果。

    除此之外,android的Canvas还提供了一个drawTextOnPath(String text,Path path,float hOffect,float vOffset,Paint paint)方法,该方法可以沿着Path绘制文本。

    其中hOffset参数指定水平偏移、vOffset指定垂直偏移。

    实例二如下:

  • 相关阅读:
    快速排序算法
    excel取值
    5.管理控制文件和日志文件
    贝叶斯决策与参数估计小结
    Kernel Methods (5) Kernel PCA
    Kernel Methods (4) Kernel SVM
    Kernel Methods (3) Kernel Linear Regression
    Kernel Methods (2) Kernel function
    Kernel Methods (1) 从简单的例子开始
    PCA算法是怎么跟协方差矩阵/特征值/特征向量勾搭起来的?
  • 原文地址:https://www.cnblogs.com/YYkun/p/5884706.html
Copyright © 2020-2023  润新知