1 /*
2  * Copyright (C) 2010 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.android.test.hwui;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.graphics.Canvas;
22 import android.graphics.Paint;
23 import android.graphics.Path;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.widget.ScrollView;
27 
28 @SuppressWarnings({"UnusedDeclaration"})
29 public class TextPathActivity extends Activity {
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33 
34         ScrollView scroller = new ScrollView(this);
35         scroller.addView(new CustomTextView(this));
36         setContentView(scroller);
37     }
38 
39     static class CustomTextView extends View {
40         private final Paint mHugePaint;
41 
CustomTextView(Context c)42         CustomTextView(Context c) {
43             super(c);
44 
45             mHugePaint = new Paint();
46             mHugePaint.setAntiAlias(true);
47             mHugePaint.setColor(0xff000000);
48             mHugePaint.setTextSize(300f);
49         }
50 
51         @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)52         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53             setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), 3000);
54         }
55 
56         @Override
onDraw(Canvas canvas)57         protected void onDraw(Canvas canvas) {
58             super.onDraw(canvas);
59             canvas.drawRGB(255, 255, 255);
60 
61             Path path = new Path();
62 
63             canvas.translate(100.0f, 300.0f);
64             drawTextAsPath(canvas, "Hello", path);
65 
66             canvas.translate(0.0f, 400.0f);
67             drawTextAsPath(canvas, "OpenGL", path);
68         }
69 
drawTextAsPath(Canvas canvas, String text, Path path)70         private void drawTextAsPath(Canvas canvas, String text, Path path) {
71             int count = text.length();
72             mHugePaint.getTextPath(text, 0, count, 0, 0, path);
73             path.close();
74             canvas.drawPath(path, mHugePaint);
75         }
76     }
77 }
78