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