关闭

PathEffectsDemo

标签: AndroidPathEffectspath
205人阅读 评论(0) 收藏 举报
分类:
package com.example.xfermodesdemo;

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.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

public class PathEffectsActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {
		/** 画笔 */
		private Paint mPaint;
		/** 路径 */
		private Path mPath;
		/** 有效路径 */
		private PathEffect[] mEffects;
		private int[] mColors;
		private float mPhase;

		public SampleView(Context context) {
			super(context);
			setFocusable(true);
			setFocusableInTouchMode(true);

			mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setStrokeWidth(6);

			mPath = makeFollowPath();

			mEffects = new PathEffect[6];

			mColors = new int[] { Color.BLACK, Color.RED, Color.BLUE,
					Color.GREEN, Color.MAGENTA, Color.BLACK };
		}

		private static Path makeFollowPath() {

			Path p = new Path();
			p.moveTo(0, 0);// Set the beginning of the next contour to the point
							// (x,y).
			for (int i = 1; i <= 15; i++) {
				p.lineTo(i * 20, (float) Math.random() * 35);
				// Add a line from the last point to the specified point (x,y).
				// If no moveTo() call has been made for this contour, the first
				// point is automatically set to (0,0).
			}
			return p;
		}

		@SuppressLint("DrawAllocation")
		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.WHITE);

			RectF bounds = new RectF();// 圆矩形边界
			/**
			 * Compute the bounds of the control points of the path, and write
			 * the answer into bounds. If the path contains 0 or 1 points, the
			 * bounds is set to (0,0,0,0)
			 * 
			 * Parameters:
			 * 
			 * bounds Returns the computed bounds of the path's control points.
			 * 
			 * exact This parameter is no longer used.
			 * */
			mPath.computeBounds(bounds, false);// 根据点来计算边界,保存边界在bounds中,路径只有0或者1个点,边界就是(0,0,0,0)
			canvas.translate(10 - bounds.left, 10 - bounds.top);

			makeEffects(mEffects, mPhase);
			mPhase += 1;
			invalidate();

			for (int i = 0; i < mEffects.length; i++) {
				mPaint.setPathEffect(mEffects[i]);
				mPaint.setColor(mColors[i]);
				canvas.drawPath(mPath, mPaint);
				canvas.translate(0, 28);
			}
		}

		private static void makeEffects(PathEffect[] e, float phase) {
			e[0] = null;// 没影响
			e[1] = new CornerPathEffect(10);// 红色的线有圆滑的边角效果
			/**
			 * The intervals array must contain an even number of entries (>=2),
			 * with the even indices specifying the "on" intervals, and the odd
			 * indices specifying the "off" intervals. phase is an offset into
			 * the intervals array (mod the sum of all of the intervals). The
			 * intervals array controls the length of the dashes. The paint's
			 * strokeWidth controls the thickness of the dashes. Note: this
			 * patheffect only affects drawing with the paint's style is set to
			 * STROKE or STROKE_AND_FILL. It is ignored if the drawing is done
			 * with style == FILL.
			 */
			e[2] = new DashPathEffect(new float[] { 10, 5, 5, 5 }, phase);// 10
																			// 5
																			// 5
																			// 5
																			// 来划分,第一个填充色
																			// 第二个空白第三个填充色第四个空白
			/**
			 * Dash the drawn path by stamping it with the specified shape. This
			 * only applies to drawings when the paint's style is STROKE or
			 * STROKE_AND_FILL. If the paint's style is FILL, then this effect
			 * is ignored. The paint's strokeWidth does not affect the results.
			 * 
			 * 
			 */
			e[3] = new PathDashPathEffect(makePathDash(), 12, phase,
					PathDashPathEffect.Style.ROTATE);// 自定义绘制路径

			e[4] = new ComposePathEffect(e[2], e[1]);// 组合路径
			e[5] = new ComposePathEffect(e[3], e[1]);
		}

		private static Path makePathDash() {
			Path p = new Path();
			p.moveTo(4, 0);
			p.lineTo(0, -4);
			p.lineTo(12, 0);
			p.lineTo(0, 4);
			return p;
		}
	}
}

0
1

  相关文章推荐
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44776次
    • 积分:1564
    • 等级:
    • 排名:千里之外
    • 原创:112篇
    • 转载:36篇
    • 译文:0篇
    • 评论:0条