Home | History | Annotate | Download | only in graphics
      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.app.Activity;
     24 import android.content.Context;
     25 import android.graphics.*;
     26 import android.os.Bundle;
     27 import android.view.View;
     28 
     29 public class PolyToPoly extends GraphicsActivity {
     30 
     31     @Override
     32     protected void onCreate(Bundle savedInstanceState) {
     33         super.onCreate(savedInstanceState);
     34         setContentView(new SampleView(this));
     35     }
     36 
     37     private static class SampleView extends View {
     38         private Paint   mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
     39         private Matrix  mMatrix = new Matrix();
     40         private Paint.FontMetrics mFontMetrics;
     41 
     42         private void doDraw(Canvas canvas, float src[], float dst[]) {
     43             canvas.save();
     44             mMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
     45             canvas.concat(mMatrix);
     46 
     47             mPaint.setColor(Color.GRAY);
     48             mPaint.setStyle(Paint.Style.STROKE);
     49             canvas.drawRect(0, 0, 64, 64, mPaint);
     50             canvas.drawLine(0, 0, 64, 64, mPaint);
     51             canvas.drawLine(0, 64, 64, 0, mPaint);
     52 
     53             mPaint.setColor(Color.RED);
     54             mPaint.setStyle(Paint.Style.FILL);
     55             // how to draw the text center on our square
     56             // centering in X is easy... use alignment (and X at midpoint)
     57             float x = 64/2;
     58             // centering in Y, we need to measure ascent/descent first
     59             float y = 64/2 - (mFontMetrics.ascent + mFontMetrics.descent)/2;
     60             canvas.drawText(src.length/2 + "", x, y, mPaint);
     61 
     62             canvas.restore();
     63         }
     64 
     65         public SampleView(Context context) {
     66             super(context);
     67 
     68             // for when the style is STROKE
     69             mPaint.setStrokeWidth(4);
     70             // for when we draw text
     71             mPaint.setTextSize(40);
     72             mPaint.setTextAlign(Paint.Align.CENTER);
     73             mFontMetrics = mPaint.getFontMetrics();
     74         }
     75 
     76         @Override protected void onDraw(Canvas canvas) {
     77             Paint paint = mPaint;
     78 
     79             canvas.drawColor(Color.WHITE);
     80 
     81             canvas.save();
     82             canvas.translate(10, 10);
     83             // translate (1 point)
     84             doDraw(canvas, new float[] { 0, 0 }, new float[] { 5, 5 });
     85             canvas.restore();
     86 
     87             canvas.save();
     88             canvas.translate(160, 10);
     89             // rotate/uniform-scale (2 points)
     90             doDraw(canvas, new float[] { 32, 32, 64, 32 },
     91                            new float[] { 32, 32, 64, 48 });
     92             canvas.restore();
     93 
     94             canvas.save();
     95             canvas.translate(10, 110);
     96             // rotate/skew (3 points)
     97             doDraw(canvas, new float[] { 0, 0, 64, 0, 0, 64 },
     98                            new float[] { 0, 0, 96, 0, 24, 64 });
     99             canvas.restore();
    100 
    101             canvas.save();
    102             canvas.translate(160, 110);
    103             // perspective (4 points)
    104             doDraw(canvas, new float[] { 0, 0, 64, 0, 64, 64, 0, 64 },
    105                            new float[] { 0, 0, 96, 0, 64, 96, 0, 64 });
    106             canvas.restore();
    107         }
    108     }
    109 }
    110 
    111