Android Path 类运用实践

理论学习了 Android Path 类之后,对 Path 类的属性方法都有所了解,写两个例子实践一下。理论与实践结合嘛。

实例1,五环 + 文字:

useesee

源码:

package com.example.pathstudy;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;

public class PathStudyActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new PathStudyView(getApplicationContext()));
	}

	private class PathStudyView extends View {

		public PathStudyView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub
			// super.onDraw(canvas);
			canvas.drawColor(Color.WHITE);
			Paint mPaint = new Paint();
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setStrokeWidth(20);

			int originX = 0;
			int originY = 0;
			int offsetX = 0;
			int offsetY = 0;
			int offsetLengthX = 240;
			int offsetLengthY = 110;

			float circleX = 150;
			float circleY = 200;
			float radius = 100;

			// first circle
			mPaint.setColor(Color.BLUE);
			Path mPath = new Path();
			mPath.addPath(getDrawCirclePath(circleX, circleY, radius));
			canvas.drawPath(mPath, mPaint);

			// second circle
			mPaint.setColor(Color.BLACK);
			canvas.save();
			offsetX = originX + offsetLengthX;
			canvas.translate(offsetX, offsetY);
			mPath.reset();
			mPath.addPath(getDrawCirclePath(circleX, circleY, radius));
			canvas.drawPath(mPath, mPaint);
			canvas.restore();

			// thid circle
			mPaint.setColor(Color.RED);
			canvas.save();
			offsetX += offsetLengthX;
			canvas.translate(offsetX, offsetY);
			mPath.reset();
			mPath.addPath(getDrawCirclePath(circleX, circleY, radius));
			canvas.drawPath(mPath, mPaint);
			canvas.restore();

			// forth circle
			mPaint.setColor(Color.YELLOW);
			canvas.save();
			offsetX = originX + offsetLengthX / 2;
			offsetY = originY + offsetLengthY;
			canvas.translate(offsetX, offsetY);
			mPath.reset();
			mPath.addPath(getDrawCirclePath(circleX, circleY, radius));
			canvas.drawPath(mPath, mPaint);
			canvas.restore();

			// fifth circle
			mPaint.setColor(Color.GREEN);
			canvas.save();
			offsetX += offsetLengthX;
			canvas.translate(offsetX, offsetY);
			mPath.reset();
			mPath.addPath(getDrawCirclePath(circleX, circleY, radius));
			canvas.drawPath(mPath, mPaint);
			canvas.restore();

			// draw text kevinems.com
			// fifth circle
			mPaint.setColor(Color.BLACK);
			mPaint.setStyle(Paint.Style.FILL);
			mPaint.setTextSize(100);
			mPaint.setStrokeWidth(5);
			String websiteStr = "kevinems.com";
			canvas.drawText(websiteStr, 110, 500, mPaint);
			canvas.restore();

		}

		private Path getDrawCirclePath(float x, float y, float radius) {
			Path mPath = new Path();
			mPath.setFillType(Path.FillType.EVEN_ODD);
			mPath.addCircle(x, y, radius, Path.Direction.CCW);
			return mPath;
		}
	}
}

实例2,可以随意绘画 path 的手写板:

paint-pad

源码:

package com.example.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class PathView extends View {
	private static final String LOG_TAG = "PathView";
	private Path mPath;
	private Paint mPaint;
	private float mPosX;
	private float mPosY;

	public PathView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		mPath = new Path();
		mPaint = new Paint();

		mPaint.setColor(Color.GREEN);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(5);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		// super.onDraw(canvas);
		Log.i(LOG_TAG, "onDraw");
		canvas.drawColor(Color.WHITE);
		canvas.drawPath(mPath, mPaint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		// return super.onTouchEvent(event);
		int action = event.getAction();
		float x = event.getX();
		float y = event.getY();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			mPath.moveTo(x, y);
			break;

		case MotionEvent.ACTION_MOVE:
			mPath.quadTo(mPosX, mPosY, x, y);

			invalidate();
			break;

		default:
			break;
		}

		mPosX = x;
		mPosY = y;

		return true;
	}
}

Android Path 类学习

这是 Kevin 的一篇 Google Android API 的翻译习作,原文请移步:

Android Deveolopers Path 类 api


public class
公共类

Path

extends Object
继承 Object

java.lang.Object
   ↳ android.graphics.Path

Class Overview
类 概貌


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.

Path类封装了由直线段,二次曲线,三次曲线混合(多轮廓)组成的几何路径。它可以通过 canvas.drawPath(path, paint) 进行绘制,包括填充和描边(取决于 paint’s Style 这个参数),或者它可以用于裁剪或绘制路径上的文本。

Summary
概要


Nested Classes
嵌套类
enum Path.Direction Specifies how closed shapes (e.g. rects, ovals) are oriented when they are added to a path.
指定封闭的形状(如矩形的,椭圆形),当它们被添加到路径时的方向。有顺时针和逆时针两个数值。
enum Path.FillType Enum for the ways a path may be filled
填充方式的枚举类型:Path.FillType EVEN_ODD,用奇偶规则填充
Path.FillType INVERSE_EVEN_ODD,顾名思义,和EVEN_ODD规则恰好相反。
Path.FillType INVERSE_WINDING,同样,WINDING的反效果。
Path.FillType WINDING,用非零环绕数规则填充。
Public Constructors
公共构造函数
Path()

Create an empty path
创建一个空的 path
Path(Path src)

Create a new path, copying the contents from the src path.
复制 Path src 到一个新的 path
Public Methods
公共类
void addArc(RectF oval, float startAngle, float sweepAngle)

Add the specified arc to the path as a new contour.
将一个指定的弧形作为新的轮廓添加到 path
void addCircle(float x, float y, float radius, Path.Direction dir)

Add a closed circle contour to the path
添加一个闭合圆形轮廓到 path
void addOval(RectF oval, Path.Direction dir)

Add a closed oval contour to the path
添加一个闭合的椭圆形轮廓到 path
void addPath(Path src, float dx, float dy)

Add a copy of src to the path, offset by (dx,dy)
将源 path 拷贝一份并添加到  path(当前),dx, dy 为偏移
void addPath(Path src)

Add a copy of src to the path
将源 path 拷贝一份并添加到  path(当前)
void addPath(Path src, Matrix matrix)

Add a copy of src to the path, transformed by matrix
将源 path 拷贝一份并添加到  path(当前),由 matrix 进行转换
void addRect(float left, float top, float right, float bottom, Path.Direction dir)

Add a closed rectangle contour to the path
添加一个闭合的矩形轮廓到 path
void addRect(RectF rect, Path.Direction dir)

Add a closed rectangle contour to the path
添加一个闭合的矩形轮廓到 path
void addRoundRect(RectF rect, float[] radii, Path.Direction dir)

Add a closed round-rectangle contour to the path.
添加一个闭合的圆角矩形轮廓到 path
void addRoundRect(RectF rect, float rx, float ry, Path.Direction dir)

Add a closed round-rectangle contour to the path
添加一个闭合的圆角矩形轮廓到 path
void arcTo(RectF oval, float startAngle, float sweepAngle)

Append the specified arc to the path as a new contour.
追加指定的弧形到 path 作为一个新的轮廓
void arcTo(RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo)

Append the specified arc to the path as a new contour.
追加指定的弧形到 path 作为一个新的轮廓
void close()

Close the current contour.
闭合当前轮廓
void computeBounds(RectF bounds, boolean exact)

Compute the bounds of the control points of the path, and write the answer into bounds.
计算路径的控制点的边界,并把答案写进边界
void cubicTo(float x1, float y1, float x2, float y2, float x3, float y3)

Add a cubic bezier from the last point, approaching control points (x1,y1) and (x2,y2), and ending at (x3,y3).
从上一个点添加一个三次贝塞尔曲线,经过控制点(x1, y1), (x2, y2),在(x3, y3) 结束
Path.FillType getFillType()

Return the path’s fill type.
返回 path 的填充类型
void incReserve(int extraPtCount)

Hint to the path to prepare for adding more points.
对 path 暗示将会添加更多的点
boolean isEmpty()

Returns true if the path is empty (contains no lines or curves)
当 path 为空时返回 true (包括没有线和曲线)
boolean isInverseFillType()

Returns true if the filltype is one of the INVERSE variants
如果填充类型为 INVERSE 变量的其中一种,返回 true
boolean isRect(RectF rect)

Returns true if the path specifies a rectangle.
如果 path 指定一个矩形,返回 true
void lineTo(float x, float y)

Add a line from the last point to the specified point (x,y).
从上一点到 (x, y) 画线
void moveTo(float x, float y)

Set the beginning of the next contour to the point (x,y).
将下一个轮廓的起点设置为 (x, y)
void offset(float dx, float dy, Path dst)

Offset the path by (dx,dy), returning true on success
将 path 偏移 (x, y),成功则返回 true
void offset(float dx, float dy)

Offset the path by (dx,dy), returning true on success
将 path 偏移 (x, y),成功则返回 true
void quadTo(float x1, float y1, float x2, float y2)

Add a quadratic bezier from the last point, approaching control point (x1,y1), and ending at (x2,y2).
从上一个点添加一个二次贝塞尔曲线,经过控制点(x1, y1),在(x2, y2) 结束
void rCubicTo(float x1, float y1, float x2, float y2, float x3, float y3)

Same as cubicTo, but the coordinates are considered relative to the current point on this contour.
同 cublicTo,但坐标被认为是相对于当前轮廓上的当前点。
void rLineTo(float dx, float dy)

Same as lineTo, but the coordinates are considered relative to the last point on this contour.
同 lineTo,但坐标被认为是相对于当前轮廓上的上一点。
void rMoveTo(float dx, float dy)

Set the beginning of the next contour relative to the last point on the previous contour.
设置下一个轮廓的开始点相当于上一个轮廓的最后一点
void rQuadTo(float dx1, float dy1, float dx2, float dy2)

Same as quadTo, but the coordinates are considered relative to the last point on this contour.
同 quadTo,但坐标被认为是相对于当前轮廓上的上一点。
void reset()

Clear any lines and curves from the path, making it empty.
清空 path 里面的所有线和曲线
void rewind()

Rewinds the path: clears any lines and curves from the path but keeps the internal data structure for faster reuse.
清空 path 中的所有线和曲线,但是保留其内部数据以便快速重用
void set(Path src)

Replace the contents of this with the contents of src.
用源 path 代替当前 path 的内容
void setFillType(Path.FillType ft)

Set the path’s fill type.
设置 path 填充类型
void setLastPoint(float dx, float dy)

Sets the last point of the path.
设置 path 的最后一点
void toggleInverseFillType()

Toggles the INVERSE state of the filltype
切换 filltype 的逆状态
void transform(Matrix matrix, Path dst)

Transform the points in this path by matrix, and write the answer into dst.
应用矩阵变换当前 path, 并把结果写到目的 path
void transform(Matrix matrix)

Transform the points in this path by matrix.
应用矩阵变换当前 path
Protected Methods
私有方法
void finalize()

Invoked when the garbage collector has detected that this instance is no longer reachable.
当垃圾收集器检测到该实例不再可达时调用

Google Android 高管 Hugo Barra 声明将加入小米

Hugo-Barra

Hugo Barra 已经在 G+ 上声明他将加入小米,作为小米的副总裁,负责小米国际业务拓展,以及与谷歌 Android 的战略合作。

新的 Android 篇章

在 Google 工作了差不多5年半,其中差不多3年是作为 Android 团体的一员(我毕生所遇到最优秀的团体),我决定开始一个新的职业角色。

在将来的几周内,我将加入中国的小米团体,作为副总裁,帮助他们扩大其令人难以置信的产品组合和全球业务。我非常期待这个新的挑战,同时特别兴奋能有机会继续帮助推动Android生态系统。

然后是致谢……(省略)

而 Google 方面也确认了这个消息。

而小道消息是各种与 Google 老大争情人的狗血剧情,这个要闹那样?小米就偷笑吧。

 

了解更多,来自 ifanr 的报道:

官方剧情

狗血剧情

我的开源项目 Device Checker 设备检测器

一、项目介绍:

描述:

Device Checker 设备检测器是一款 android 应用,用于检测手机的各个硬件模块是否正常工作。

立项:

本人主业是嵌入式驱动开发,经常要跟设备的各个模块打交道,经常要写一些代码测试各个模块是否正常工作,所以有想法做这么一个项目,一方面是能增强自己的开发实力,一方面可以帮助到其他朋友验机(如果他们看得上眼的话~~~)。

开源:

开源的想法主要是学习一下开源项目的流程,当然有朋友一起学习开发就更好。源码托管选择了google code,

应用发布:

地址:http://www.mumayi.com/android-648974.html

继续阅读我的开源项目 Device Checker 设备检测器

百度1T云盘与金山快盘

百度云昨天花1块钱买了1T白菜,哦,不,是百度的1T云盘,超划算,现在1块钱在深圳可买不到白菜了。

百度360争推1TB永久网盘, 心血来潮,就购买了百度云的1TB云盘,360的东西相信不过,百度勉强信得过,所以选择了百度云。

其实我现在在用金山快盘,而且也算是早期的用户吧,但是今天通过使用对比,我选择了百度云作为我以后的备份空间。

网盘是一个网络产品,其价值就体现在速度、稳定性、主要功能、增值功能。

继续阅读百度1T云盘与金山快盘

我心永恒–恒大2:0莱赫维亚

我就是一个不折不扣的爱凑热闹的伪球迷~~~这场亚冠的直播,上半场很揪心,下半场很爽。

北京时间8月21日20:00,2013赛季亚冠联赛展开1/4决赛首回合的争夺,中超冠军广州恒大[微博]坐镇天河体育中心迎战卡塔尔莱赫维亚队。第25分钟,穆里奇禁区内推射高出。第60分钟,孔卡门前停球后打门被封堵。72分钟,孔卡罚进自己制造的点球。第76分钟,孔卡脚后跟妙传,埃尔克森包抄劲射破门。90分钟激战,广州恒大2-0击败莱赫维亚,双方次回合的比赛是9月19日01:15。

第一粒进球:

继续阅读我心永恒–恒大2:0莱赫维亚

Android adb 网络连接

工作中偶尔会用到adb网络连接,在这里收集整理了相关的教程。

原理:

adb (Android Debug Brigde) 是一个多功能命令行工具。使用 adb 可以方便地与 Android 模拟器或者硬件设备进行通信。这是一个 client-server 程序,包含三个组件:

  • client 在计算机上运行。你可以通过 adb 命令,从 shell 中调用一个client。其他工具例如 ADT 插件和 DDMS 也可以创建 adb 客户端。
  • daemon,模拟器或者设备上的后台进程。
  • server,计算机上的一个后台进程。server 管理 client 与 daemon 之间的通信。

方法:

网络连接的关键是如何在设备上启动网络 adb 调试的功能,这里介绍3种方法:

继续阅读Android adb 网络连接

DNSPod解析name.com的域名IP错误,指向sedo域名停放界面

这是一个无厘头的问题~~~

现象描述:

本人的kevinems.com的域名是在name.com购买的,在name.com后台修改dns服务器指向DNSPod的服务器后,DNSPod解析的域名指向了错误的IP。有时指向正确的IP,有时指向sedo的域名停放页面。

原因分析:

可能修改了name.com域名解析服务器的缘故,同时DNS Records清空了的话,name.com就认为这个域名你没有使用,就把它指向了域名停放界面(很可恨是吧!)。

解决方法:

解决方法,修改DNS Records,把域名和子域名都指向网站服务器的IP.

这样问题暂时就解决了。这也可能是我的个例,因为我在网上搜索,都是说子域名被指向sedo域名停放界面的,而不是我这样的随机出现的指向错误IP的问题。

博客再次正式发布了

更新:
由于bluehost偶尔抽风的原因,我申请了退款。bluehost服务还是不错的,错就错在我们之间的距离实在太远了。

现在换了西门的主机格调,感觉不错,希望稳定性也可以有好的表现。

—————————分割线————————–

大学好友突然问我哪些虚拟空间比较好,我也3、4年没玩网站了,不了解行情了。当初被国内的网站备案郁闷到了,干脆放弃了。而现在这次把网站建设起来,算是心血来潮吧。希望自己能坚持下去,能和自己的儿女子孙们一起更新就更好了,哈哈。

因为不想再用国内的空间,所以空间选用了国外的bluehost,一年的套餐用了优惠链接5.95美元每月,口碑还不错,速度也可以,偶尔会慢一下,不知道是不是最近刮风的问题。。。

域名kevinems.com是bluehost送的,一年后要续费,字面意思是“你看看”,创意来自中式英语”give you some color to see see”,囧。

博客的内容主要定位为情感大爆炸、电子创作、嵌入式软件开发、Android相关等等等等~~~

欢迎相关主题网站交换链接。谢谢。