• 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.KeyEvent;
23 import android.view.View;
24 import android.view.Window;
25 
26 public class UnicodeChart extends GraphicsActivity {
27 
28     @Override
onCreate(Bundle savedInstanceState)29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31 
32         requestWindowFeature(Window.FEATURE_NO_TITLE);
33 
34         setContentView(new SampleView(this));
35     }
36 
37     private static class SampleView extends View {
38         private Paint mBigCharPaint;
39         private Paint mLabelPaint;
40         private final char[] mChars = new char[256];
41         private final float[] mPos = new float[512];
42 
43         private int mBase;
44 
45         private static final int XMUL = 20;
46         private static final int YMUL = 28;
47         private static final int YBASE = 18;
48 
SampleView(Context context)49         public SampleView(Context context) {
50             super(context);
51             setFocusable(true);
52             setFocusableInTouchMode(true);
53 
54             mBigCharPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
55             mBigCharPaint.setTextSize(15);
56             mBigCharPaint.setTextAlign(Paint.Align.CENTER);
57 
58             mLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
59             mLabelPaint.setTextSize(8);
60             mLabelPaint.setTextAlign(Paint.Align.CENTER);
61 
62             // the position array is the same for all charts
63             float[] pos = mPos;
64             int index = 0;
65             for (int col = 0; col < 16; col++) {
66                 final float x = col * XMUL + 10;
67                 for (int row = 0; row < 16; row++) {
68                     pos[index++] = x;
69                     pos[index++] = row * YMUL + YBASE;
70                 }
71             }
72         }
73 
computeX(int index)74         private float computeX(int index) {
75             return (index >> 4) * XMUL + 10;
76         }
77 
computeY(int index)78         private float computeY(int index) {
79             return (index & 0xF) * YMUL + YMUL;
80         }
81 
drawChart(Canvas canvas, int base)82         private void drawChart(Canvas canvas, int base) {
83             char[] chars = mChars;
84             for (int i = 0; i < 256; i++) {
85                 int unichar = base + i;
86                 chars[i] = (char)unichar;
87 
88                 canvas.drawText(Integer.toHexString(unichar),
89                                 computeX(i), computeY(i), mLabelPaint);
90             }
91             canvas.drawPosText(chars, 0, 256, mPos, mBigCharPaint);
92         }
93 
onDraw(Canvas canvas)94         @Override protected void onDraw(Canvas canvas) {
95             canvas.drawColor(Color.WHITE);
96 
97             canvas.translate(0, 1);
98             drawChart(canvas, mBase * 256);
99         }
100 
onKeyDown(int keyCode, KeyEvent event)101         @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
102             switch (keyCode) {
103                 case KeyEvent.KEYCODE_DPAD_LEFT:
104                     if (mBase > 0) {
105                         mBase -= 1;
106                         invalidate();
107                     }
108                     return true;
109                 case KeyEvent.KEYCODE_DPAD_RIGHT:
110                     mBase += 1;
111                     invalidate();
112                     return true;
113                 default:
114                     break;
115             }
116             return super.onKeyDown(keyCode, event);
117         }
118     }
119 }
120