path填充Demo(PathFillTypes)

package com.example.xfermodesdemo;

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 PathFillTypesActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {
		/** 抗锯齿画笔 */
		private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		/** 绘制路径 */
		private Path mPath;

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

			mPath = new Path();
			/**
			 * CW : clockwise 顺时针方向
			 * 
			 * CCW : counter-clockwise 逆时针方向
			 */
			mPath.addCircle(40, 40, 45, Path.Direction.CCW);
			mPath.addCircle(80, 80, 45, Path.Direction.CCW);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);

			Paint paint = mPaint;
			canvas.drawColor(0xFFCCCCCC);// 绘制背景颜色
			canvas.translate(20, 20);// 画板平移(20,20)
			paint.setAntiAlias(true);// 设置抗锯齿

			showPath(canvas, 0, 0, Path.FillType.WINDING, paint, 0);
			showPath(canvas, 160, 0, Path.FillType.EVEN_ODD, paint, 1);
			showPath(canvas, 0, 160, Path.FillType.INVERSE_WINDING, paint, 2);
			showPath(canvas, 160, 160, Path.FillType.INVERSE_EVEN_ODD, paint, 3);

		}

		private void showPath(Canvas canvas, int x, int y, Path.FillType ft,
				Paint paint, int count) {
			canvas.save();// 保存
			canvas.translate(x, y);// 平移
			canvas.clipRect(0, 0, 120, 120);// 裁剪矩形
			canvas.drawColor(Color.WHITE);// 在裁剪的矩形里面绘制背景
			mPath.setFillType(ft);
			mPaint.setColor(0xFF109093 + count * 20);
			canvas.drawPath(mPath, paint);
			canvas.restore();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值