矩阵旋转偏移,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(); } } }
相关文章推荐
- 数据库中间件druid更换c3p0以及druid数据源监控配置
- Android Fragment 真正的完全解析(上)
- Tomcat Web服务器的配置及详解
- Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验
- 通信行业并非那么”腹黑“
- 面试题47:不用加减乘除做加法
- 微信、qq时间格式模板
- JavaWeb开发基础:Servlet登录小实例
- du命令
- 数据绑定(data binding)2
- 关于Eclipse(MyEclipse)中一次性批量导入多个项目Project.
- 正则表达式 整数&正整数
- [11.20] 实验要求
- web入门第四天
- ThinkPHP 分页实现
- Eclipse用法和技巧二十:一个快速打印技巧
- mac下安装swoole-----XAMPP
- php生成一个可选位数的随机码
- 监控工具,Spotlight 的使用
- Cloudera 介绍