矩阵旋转偏移,Matrix

package com.example.xfermodesdemo;

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

public class PolyToPolyActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {
		private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);// 画笔
		private Matrix mMatrix = new Matrix();// 矩阵
		private Paint.FontMetrics mFontMetrics;// 字体矩阵

		public SampleView(Context context) {
			super(context);

			mPaint.setStrokeWidth(4);// 画笔宽度
			mPaint.setTextSize(40);// 文字尺寸
			mPaint.setTextAlign(Paint.Align.CENTER);// 设置文字对齐方式
			mFontMetrics = mPaint.getFontMetrics();

		}

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

			canvas.save();
			canvas.translate(10, 10);
			// 平移 1点
			doDraw(canvas, new float[] { 0, 0 }, new float[] { 5, 5 });// 从(0,0)平移到(5,5)
			canvas.restore();

			canvas.save();
			canvas.translate(160, 10);
			// 两点 缩放和旋转,就是两条线的缩放和旋转关系
			// rotate/uniform-scale (2 points)
			doDraw(canvas, new float[] { 32, 32, 64, 32 }, new float[] { 32,
					32, 64, 48 });
			canvas.restore();

			canvas.save();
			canvas.translate(10, 110);
			// rotate/skew (3 points) 旋转和斜视
			doDraw(canvas, new float[] { 0, 0, 64, 0, 0, 64 }, new float[] { 0,
					0, 96, 0, 24, 64 });
			canvas.restore();

			canvas.save();
			canvas.translate(160, 110);
			// perspective (4 points) 四点 透视 
			doDraw(canvas, new float[] { 0, 0, 64, 0, 64, 64, 0, 64 },
					new float[] { 0, 0, 96, 0, 64, 96, 0, 64 });
			canvas.restore();
		}

		private void doDraw(Canvas canvas, float src[], float dst[]) {
			canvas.save();
			/**
			 * boolean android.graphics.Matrix.setPolyToPoly(float[] src, int
			 * srcIndex, float[] dst, int dstIndex, int pointCount)
			 * 
			 * 
			 * Set the matrix such that the specified src points would map to
			 * the specified dst points. The "points" are represented as an
			 * array of floats, order [x0, y0, x1, y1, ...], where each "point"
			 * is 2 float values.
			 * 
			 * Parameters:
			 * 
			 * src The array of src [x,y] pairs (points)
			 * 
			 * srcIndex Index of the first pair of src values
			 * 
			 * dst The array of dst [x,y] pairs (points)
			 * 
			 * dstIndex Index of the first pair of dst values
			 * 
			 * pointCount The number of pairs/points to be used. Must be [0..4]
			 */
			mMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
			canvas.concat(mMatrix);

			mPaint.setColor(Color.GRAY);
			mPaint.setStyle(Paint.Style.STROKE);
			canvas.drawRect(0, 0, 64, 64, mPaint);
			canvas.drawLine(0, 0, 64, 64, mPaint);
			// canvas.drawLine(0, 64, 64, 0, mPaint);

			mPaint.setColor(Color.RED);
			mPaint.setStyle(Paint.Style.FILL);

			// how to draw the text center on our square
			// centering in X is easy... use alignment (and X at midpoint)
			float x = 64 / 2;
			// centering in Y, we need to measure ascent/descent first
			float y = 64 / 2 - (mFontMetrics.ascent + mFontMetrics.descent) / 2;

			canvas.drawText((src.length + 1) / 2 + "", x, y, mPaint);

			canvas.restore();

		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值