PathEffectsDemo

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;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值