初学Android,图形图像之使用Path类(二十六)

news/2024/7/3 13:36:12 标签: path, android, 图形, colors, float, class
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

Path类可以预先在View上将N个点连成一条"路径",然后调用Canvas的drawPath(class="tags" href="/tags/PATH.html" title=path>path,paint)即可沿着路径绘制class="tags" href="/tags/TuXing.html" title=图形>图形

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

ComposePathEffect

CornerPathEffect

DashPathEffect

DiscretePathEffect

PathDashPathEffect

SumPathEffect

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

class="language-java">package WangLi.Graphics.PathTest;

import class="tags" href="/tags/ANDROID.html" title=android>android.app.Activity;
import class="tags" href="/tags/ANDROID.html" title=android>android.content.Context;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.Canvas;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.Color;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.ComposePathEffect;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.CornerPathEffect;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.DashPathEffect;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.DiscretePathEffect;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.Paint;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.Path;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.PathDashPathEffect;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.PathEffect;
import class="tags" href="/tags/ANDROID.html" title=android>android.graphics.SumPathEffect;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.Bundle;
import class="tags" href="/tags/ANDROID.html" title=android>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
    {
		class="tags" href="/tags/FLOAT.html" title=float>float phase;
    	PathEffect[] effects = new PathEffect[7];
    	int[] class="tags" href="/tags/COLORS.html" title=colors>colors;
    	private Paint paint;
    	Path class="tags" href="/tags/PATH.html" title=path>path;
    	public MyView(Context context) {
			super(context);
			paint = new Paint();
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(4);
			//创建,并初始化Path
			class="tags" href="/tags/PATH.html" title=path>path = new Path();
			class="tags" href="/tags/PATH.html" title=path>path.moveTo(0, 0);
			for(int i = 1; i<= 15; i++)
			{
				//生成15个点,随机生成它们的坐标,并将它们连成一条Path
				class="tags" href="/tags/PATH.html" title=path>path.lineTo(i*20, (class="tags" href="/tags/FLOAT.html" title=float>float)Math.random()*60);
			}
			//初始化七个颜色
			class="tags" href="/tags/COLORS.html" title=colors>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 class="tags" href="/tags/FLOAT.html" title=float>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(class="tags" href="/tags/COLORS.html" title=colors>colors[i]);
    			canvas.drawPath(class="tags" href="/tags/PATH.html" title=path>path, paint);
    			canvas.translate(0, 60);
    		}
    		//改变phase值,形成动画效果
    		phase += 1;
    		invalidate();
    	}
    }
}
效果如下





http://www.niftyadmin.cn/n/1411382.html

相关文章

react-router-dom示例讲解(二)——url路径参数

react-router-dom的官方教程中&#xff0c;第二个示例是路径参数&#xff0c;是一个比较简单的示例。这个示例给了我们一个如何匹配路径参数的示例或者方法。 本示例实现的效果如下&#xff1a; 在路由设计中&#xff0c;我是这样实现的&#xff1a; import React, { Comp…

【windows下配置host的作用】

Winxp下的&#xff1a;\WINDOWS\system32\drivers\etc\hosts文件用记事本打开hosts文件&#xff0c;它的作用是包含IP地址和Host name(主机名)的映射关系&#xff0c;是一个映射IP地址和Hostname(主机名)的规定&#xff0c;规定要求每段只能包括一个映射关系&#xff0c;IP地址…

初学Android,图形图像之使用双缓冲画图(二十七)

当数据量很大时&#xff0c;绘图可能需要几秒钟甚至更长的时间&#xff0c;而且有时还会出现闪烁现象&#xff0c;为了解决这些问题&#xff0c;可采用双缓冲技术来绘图。双缓冲即在内存中创建一个与屏幕绘图区域一致的对象&#xff0c;先将图形绘制到内存中的这个对象上&#…

react-router-dom示例讲解(三)——认证功能

在官方的示例中&#xff0c;认证是属于比较难的一个示例&#xff0c;主要是它涉及到数据的管理问题。 实现的效果图如下所示&#xff1a; 登录成功后&#xff1a; 实现的整体思路&#xff1a; 在整个页面中&#xff0c;定义一个状态&#xff0c;叫 isLogin。由这个状态…

初学Android,图形图像之打砖块游戏(二十八)

这个弹球游戏是没有砖块的打砖块游戏&#xff0c;简版&#xff0c;效果如下package WangLi.Graphics.PinBall;import java.util.Random; import java.util.Timer; import java.util.TimerTask;import android.app.Activity; import android.content.Context; import android.gr…

Windows API教程文件系统

本篇文章主要介绍了"Windows API教程文件系统"&#xff0c;主要涉及到Windows API教程文件系统方面的内容&#xff0c;对于Windows API教程文件系统感兴趣的同学可以参考一下。 索引 概念简介 文件对象文件流文件句柄文件指针文件系统操作 常见 API 高级文件操作本讲…

react-router-dom示例讲解(四)——自定义链接

在react-router-dom的官方示例中&#xff0c;自定义链接算是一个比较简单的功能了。其核心就是Route以及其children属性的使用。 效果示例的展示&#xff1a; 相关代码&#xff1a; //在App.js中 import React, {Component} from react; import {BrowserRouter as Router,…

初学Android,图形图像之Matrix(二十九)

Matrix类可以控制图形的平移,旋转,缩放,倾斜&#xff0c;也可以控制View组件的平移&#xff0c;旋转,倾斜等下面的例子postInvalidate方法会重绘&#xff0c;随之调用onDraw方法&#xff0c;在onDraw方法里再对位图进行缩放和倾斜操作package WangLi.Graphics.Matrix; import a…