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 MeasureText 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 final int WIDTH = 50; 37 private static final int HEIGHT = 50; 38 private static final int STRIDE = 64; // must be >= WIDTH 39 createColors()40 private static int[] createColors() { 41 int[] colors = new int[STRIDE * HEIGHT]; 42 for (int y = 0; y < HEIGHT; y++) { 43 for (int x = 0; x < WIDTH; x++) { 44 int r = x * 255 / (WIDTH - 1); 45 int g = y * 255 / (HEIGHT - 1); 46 int b = 255 - Math.min(r, g); 47 int a = Math.max(r, g); 48 colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b; 49 } 50 } 51 return colors; 52 } 53 54 private static class SampleView extends View { 55 private Paint mPaint; 56 private float mOriginX = 10; 57 private float mOriginY = 80; 58 SampleView(Context context)59 public SampleView(Context context) { 60 super(context); 61 setFocusable(true); 62 63 mPaint = new Paint(); 64 mPaint.setAntiAlias(true); 65 mPaint.setStrokeWidth(5); 66 mPaint.setStrokeCap(Paint.Cap.ROUND); 67 mPaint.setTextSize(64); 68 mPaint.setTypeface(Typeface.create(Typeface.SERIF, 69 Typeface.ITALIC)); 70 } 71 showText(Canvas canvas, String text, Paint.Align align)72 private void showText(Canvas canvas, String text, Paint.Align align) { 73 // mPaint.setTextAlign(align); 74 75 Rect bounds = new Rect(); 76 float[] widths = new float[text.length()]; 77 78 int count = mPaint.getTextWidths(text, 0, text.length(), widths); 79 float w = mPaint.measureText(text, 0, text.length()); 80 mPaint.getTextBounds(text, 0, text.length(), bounds); 81 82 mPaint.setColor(0xFF88FF88); 83 canvas.drawRect(bounds, mPaint); 84 mPaint.setColor(Color.BLACK); 85 canvas.drawText(text, 0, 0, mPaint); 86 87 float[] pts = new float[2 + count*2]; 88 float x = 0; 89 float y = 0; 90 pts[0] = x; 91 pts[1] = y; 92 for (int i = 0; i < count; i++) { 93 x += widths[i]; 94 pts[2 + i*2] = x; 95 pts[2 + i*2 + 1] = y; 96 } 97 mPaint.setColor(Color.RED); 98 mPaint.setStrokeWidth(0); 99 canvas.drawLine(0, 0, w, 0, mPaint); 100 mPaint.setStrokeWidth(5); 101 canvas.drawPoints(pts, 0, (count + 1) << 1, mPaint); 102 } 103 onDraw(Canvas canvas)104 @Override protected void onDraw(Canvas canvas) { 105 canvas.drawColor(Color.WHITE); 106 107 canvas.translate(mOriginX, mOriginY); 108 109 showText(canvas, "Measure", Paint.Align.LEFT); 110 canvas.translate(0, 80); 111 showText(canvas, "wiggy!", Paint.Align.CENTER); 112 canvas.translate(0, 80); 113 showText(canvas, "Text", Paint.Align.RIGHT); 114 } 115 } 116 } 117 118