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