• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.content.Context;
23 import android.graphics.*;
24 import android.os.Bundle;
25 import android.view.KeyEvent;
26 import android.view.*;
27 
28 public class TextAlign extends GraphicsActivity {
29 
30     @Override
onCreate(Bundle savedInstanceState)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;
38         private float   mX;
39         private float[] mPos;
40 
41         private Path    mPath;
42         private Paint   mPathPaint;
43 
44         private static final int DY = 30;
45         private static final String TEXT_L = "Left";
46         private static final String TEXT_C = "Center";
47         private static final String TEXT_R = "Right";
48         private static final String POSTEXT = "Positioned";
49         private static final String TEXTONPATH = "Along a path";
50 
makePath(Path p)51         private static void makePath(Path p) {
52             p.moveTo(10, 0);
53             p.cubicTo(100, -50, 200, 50, 300, 0);
54         }
55 
buildTextPositions(String text, float y, Paint paint)56         private float[] buildTextPositions(String text, float y, Paint paint) {
57             float[] widths = new float[text.length()];
58             // initially get the widths for each char
59             int n = paint.getTextWidths(text, widths);
60             // now popuplate the array, interleaving spaces for the Y values
61             float[] pos = new float[n * 2];
62             float accumulatedX = 0;
63             for (int i = 0; i < n; i++) {
64                 pos[i*2 + 0] = accumulatedX;
65                 pos[i*2 + 1] = y;
66                 accumulatedX += widths[i];
67             }
68             return pos;
69         }
70 
SampleView(Context context)71         public SampleView(Context context) {
72             super(context);
73             setFocusable(true);
74 
75             mPaint = new Paint();
76             mPaint.setAntiAlias(true);
77             mPaint.setTextSize(30);
78             mPaint.setTypeface(Typeface.SERIF);
79 
80             mPos = buildTextPositions(POSTEXT, 0, mPaint);
81 
82             mPath = new Path();
83             makePath(mPath);
84 
85             mPathPaint = new Paint();
86             mPathPaint.setAntiAlias(true);
87             mPathPaint.setColor(0x800000FF);
88             mPathPaint.setStyle(Paint.Style.STROKE);
89         }
90 
onDraw(Canvas canvas)91         @Override protected void onDraw(Canvas canvas) {
92             canvas.drawColor(Color.WHITE);
93 
94             Paint p = mPaint;
95             float x = mX;
96             float y = 0;
97             float[] pos = mPos;
98 
99             // draw the normal strings
100 
101             p.setColor(0x80FF0000);
102             canvas.drawLine(x, y, x, y+DY*3, p);
103             p.setColor(Color.BLACK);
104 
105             canvas.translate(0, DY);
106             p.setTextAlign(Paint.Align.LEFT);
107             canvas.drawText(TEXT_L, x, y, p);
108 
109             canvas.translate(0, DY);
110             p.setTextAlign(Paint.Align.CENTER);
111             canvas.drawText(TEXT_C, x, y, p);
112 
113             canvas.translate(0, DY);
114             p.setTextAlign(Paint.Align.RIGHT);
115             canvas.drawText(TEXT_R, x, y, p);
116 
117             canvas.translate(100, DY*2);
118 
119             // now draw the positioned strings
120 
121             p.setColor(0xBB00FF00);
122             for (int i = 0; i < pos.length/2; i++) {
123                 canvas.drawLine(pos[i*2+0], pos[i*2+1]-DY,
124                                 pos[i*2+0], pos[i*2+1]+DY*2, p);
125             }
126             p.setColor(Color.BLACK);
127 
128             p.setTextAlign(Paint.Align.LEFT);
129             canvas.drawPosText(POSTEXT, pos, p);
130 
131             canvas.translate(0, DY);
132             p.setTextAlign(Paint.Align.CENTER);
133             canvas.drawPosText(POSTEXT, pos, p);
134 
135             canvas.translate(0, DY);
136             p.setTextAlign(Paint.Align.RIGHT);
137             canvas.drawPosText(POSTEXT, pos, p);
138 
139             // now draw the text on path
140 
141             canvas.translate(-100, DY*2);
142 
143             canvas.drawPath(mPath, mPathPaint);
144             p.setTextAlign(Paint.Align.LEFT);
145             canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
146 
147             canvas.translate(0, DY*1.5f);
148             canvas.drawPath(mPath, mPathPaint);
149             p.setTextAlign(Paint.Align.CENTER);
150             canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
151 
152             canvas.translate(0, DY*1.5f);
153             canvas.drawPath(mPath, mPathPaint);
154             p.setTextAlign(Paint.Align.RIGHT);
155             canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
156         }
157 
158         @Override
onSizeChanged(int w, int h, int ow, int oh)159         protected void onSizeChanged(int w, int h, int ow, int oh) {
160             super.onSizeChanged(w, h, ow, oh);
161             mX = w * 0.5f;  // remember the center of the screen
162         }
163     }
164 }
165 
166