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