当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(74):Graphics->PolyToPoly

android.graphics.Matrix 类提供二维坐标变换(平移,旋转,缩放等)使用Matrix通常的方法是直接指定所需的Matrix的值,另外一种如果已知坐标变换前后几个点的坐标,你可以根据这些点的映射关系算出对应的变换矩阵。但Matrix提供了一种更为简洁的方法:

public boolean setPolyToPoly(float[] src, int srcIndex, float[] dst, int dstIndex, int pointCount)

提供指定变换前(src)和变换后(dst)的坐标对,Matrix自动帮你计算出实现这些坐标变换对于的Matrix。每个坐标的格式为[x0,y0,x1,y1 ...]两个float值代表一个点。

本例使用1 个点 (偏移变换) 2个点(旋转/缩放) ,3个点(旋转/剪切),4个点(透视变换) 使用polyToPoly 计算出对应的Matrix,然后使用计算出的Matrix来绘制图形:

[java] 
private void doDraw(Canvas canvas, float src[], float dst[]) { 
 canvas.save(); 
 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/2 + "", x, y, mPaint); 
  
 canvas.restore(); 

  
.... 
  
canvas.save(); 
canvas.translate(10, 10); 
// translate (1 point)  
doDraw(canvas, new float[] { 0, 0 }, 
 new float[] { 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();
 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/2 + "", x, y, mPaint);
 
 canvas.restore();
}
 
....
 
canvas.save();
canvas.translate(10, 10);
// translate (1 point)
doDraw(canvas, new float[] { 0, 0 },
 new float[] { 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();  www.zzzyk.com
 
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();

 作者:mapdigit
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,