您的位置:首页 > 其它

矩阵旋转偏移,Matrix

2015-11-19 16:33 429 查看
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();

}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: