Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ApiDemos示例解析(74):Graphics->PolyToPoly

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

編輯:關於Android編程

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.2cto.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
 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved