1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.graphics; 18 19 // Need the following import to get access to the app resources, since this 20 // class is in a sub-package. 21 //import com.example.android.apis.R; 22 23 import android.content.Context; 24 import android.graphics.*; 25 import android.os.Bundle; 26 import android.view.View; 27 28 public class PolyToPoly extends GraphicsActivity { 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(new SampleView(this)); 34 } 35 36 private static class SampleView extends View { 37 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 38 private Matrix mMatrix = new Matrix(); 39 private Paint.FontMetrics mFontMetrics; 40 41 private void doDraw(Canvas canvas, float src[], float dst[]) { 42 canvas.save(); 43 mMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1); 44 canvas.concat(mMatrix); 45 46 mPaint.setColor(Color.GRAY); 47 mPaint.setStyle(Paint.Style.STROKE); 48 canvas.drawRect(0, 0, 64, 64, mPaint); 49 canvas.drawLine(0, 0, 64, 64, mPaint); 50 canvas.drawLine(0, 64, 64, 0, mPaint); 51 52 mPaint.setColor(Color.RED); 53 mPaint.setStyle(Paint.Style.FILL); 54 // how to draw the text center on our square 55 // centering in X is easy... use alignment (and X at midpoint) 56 float x = 64/2; 57 // centering in Y, we need to measure ascent/descent first 58 float y = 64/2 - (mFontMetrics.ascent + mFontMetrics.descent)/2; 59 canvas.drawText(src.length/2 + "", x, y, mPaint); 60 61 canvas.restore(); 62 } 63 64 public SampleView(Context context) { 65 super(context); 66 67 // for when the style is STROKE 68 mPaint.setStrokeWidth(4); 69 // for when we draw text 70 mPaint.setTextSize(40); 71 mPaint.setTextAlign(Paint.Align.CENTER); 72 mFontMetrics = mPaint.getFontMetrics(); 73 } 74 75 @Override 76 protected void onDraw(Canvas canvas) { 77 canvas.drawColor(Color.WHITE); 78 79 canvas.save(); 80 canvas.translate(10, 10); 81 // translate (1 point) 82 doDraw(canvas, new float[] { 0, 0 }, new float[] { 5, 5 }); 83 canvas.restore(); 84 85 canvas.save(); 86 canvas.translate(160, 10); 87 // rotate/uniform-scale (2 points) 88 doDraw(canvas, new float[] { 32, 32, 64, 32 }, 89 new float[] { 32, 32, 64, 48 }); 90 canvas.restore(); 91 92 canvas.save(); 93 canvas.translate(10, 110); 94 // rotate/skew (3 points) 95 doDraw(canvas, new float[] { 0, 0, 64, 0, 0, 64 }, 96 new float[] { 0, 0, 96, 0, 24, 64 }); 97 canvas.restore(); 98 99 canvas.save(); 100 canvas.translate(160, 110); 101 // perspective (4 points) 102 doDraw(canvas, new float[] { 0, 0, 64, 0, 64, 64, 0, 64 }, 103 new float[] { 0, 0, 96, 0, 64, 96, 0, 64 }); 104 canvas.restore(); 105 } 106 } 107 } 108