1 /* 2 * Copyright (C) 2008 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 import android.app.Activity; 20 import android.content.Context; 21 import android.graphics.*; 22 import android.os.Bundle; 23 import android.view.View; 24 25 public class ScaleToFit extends GraphicsActivity { 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(new SampleView(this)); 31 } 32 33 private static class SampleView extends View { 34 private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 35 private final Paint mHairPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 36 private final Paint mLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 37 private final Matrix mMatrix = new Matrix(); 38 private final RectF mSrcR = new RectF(); 39 40 private static final Matrix.ScaleToFit[] sFits = 41 new Matrix.ScaleToFit[] { 42 Matrix.ScaleToFit.FILL, 43 Matrix.ScaleToFit.START, 44 Matrix.ScaleToFit.CENTER, 45 Matrix.ScaleToFit.END 46 }; 47 48 private static final String[] sFitLabels = new String[] { 49 "FILL", "START", "CENTER", "END" 50 }; 51 52 private static final int[] sSrcData = new int[] { 53 80, 40, Color.RED, 54 40, 80, Color.GREEN, 55 30, 30, Color.BLUE, 56 80, 80, Color.BLACK 57 }; 58 private static final int N = 4; 59 60 private static final int WIDTH = 52; 61 private static final int HEIGHT = 52; 62 private final RectF mDstR = new RectF(0, 0, WIDTH, HEIGHT); 63 64 public SampleView(Context context) { 65 super(context); 66 67 mHairPaint.setStyle(Paint.Style.STROKE); 68 mLabelPaint.setTextSize(16); 69 } 70 71 private void setSrcR(int index) { 72 int w = sSrcData[index*3 + 0]; 73 int h = sSrcData[index*3 + 1]; 74 mSrcR.set(0, 0, w, h); 75 } 76 77 private void drawSrcR(Canvas canvas, int index) { 78 mPaint.setColor(sSrcData[index*3 + 2]); 79 canvas.drawOval(mSrcR, mPaint); 80 } 81 82 private void drawFit(Canvas canvas, int index, Matrix.ScaleToFit stf) { 83 canvas.save(); 84 85 setSrcR(index); 86 mMatrix.setRectToRect(mSrcR, mDstR, stf); 87 canvas.concat(mMatrix); 88 drawSrcR(canvas, index); 89 90 canvas.restore(); 91 92 canvas.drawRect(mDstR, mHairPaint); 93 } 94 95 @Override protected void onDraw(Canvas canvas) { 96 Paint paint = mPaint; 97 98 canvas.drawColor(Color.WHITE); 99 100 canvas.translate(10, 10); 101 102 canvas.save(); 103 for (int i = 0; i < N; i++) { 104 setSrcR(i); 105 drawSrcR(canvas, i); 106 canvas.translate(mSrcR.width() + 15, 0); 107 } 108 canvas.restore(); 109 110 canvas.translate(0, 100); 111 for (int j = 0; j < sFits.length; j++) { 112 canvas.save(); 113 for (int i = 0; i < N; i++) { 114 drawFit(canvas, i, sFits[j]); 115 canvas.translate(mDstR.width() + 8, 0); 116 } 117 canvas.drawText(sFitLabels[j], 0, HEIGHT*2/3, mLabelPaint); 118 canvas.restore(); 119 canvas.translate(0, 80); 120 } 121 } 122 } 123 } 124 125