TextOnPathActivity.java revision 325740fb444af8fc7fb0119b2e30ce322c2ae134
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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.Path;
24import android.os.Bundle;
25import android.view.View;
26
27@SuppressWarnings({"UnusedDeclaration"})
28public class TextOnPathActivity extends Activity {
29    private Path mPath;
30
31    @Override
32    protected void onCreate(Bundle savedInstanceState) {
33        super.onCreate(savedInstanceState);
34
35        mPath = makePath();
36
37        final TextOnPathView view = new TextOnPathView(this);
38        setContentView(view);
39    }
40
41    private Path makePath() {
42        Path path = new Path();
43        buildPath(path);
44        return path;
45    }
46
47    private void buildPath(Path path) {
48        path.moveTo(0.0f, 0.0f);
49        path.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f);
50        path.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f);
51        path.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f);
52    }
53
54    public class TextOnPathView extends View {
55        private static final String TEST_STRING = "Hello OpenGL renderer, text on path! ";
56
57        private final Paint mPaint;
58        private final String mText;
59
60        public TextOnPathView(Context c) {
61            super(c);
62
63            mPaint = new Paint();
64            mPaint.setAntiAlias(true);
65            mPaint.setColor(0xff000000);
66
67            StringBuilder builder = new StringBuilder(TEST_STRING.length() * 5);
68            for (int i = 0; i < 5; i++) {
69                builder.append(TEST_STRING);
70            }
71            mText = builder.toString();
72        }
73
74        @Override
75        protected void onDraw(Canvas canvas) {
76            super.onDraw(canvas);
77
78            canvas.drawARGB(255, 255, 255, 255);
79
80            canvas.translate(550.0f, 60.0f);
81            canvas.drawTextOnPath(mText, mPath, 0.0f, 0.0f, mPaint);
82        }
83    }
84}
85