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.animation.ObjectAnimator;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.graphics.Canvas;
23 import android.graphics.Paint;
24 import android.graphics.Path;
25 import android.os.Bundle;
26 import android.view.View;
27 
28 @SuppressWarnings({"UnusedDeclaration"})
29 public class ScaledTextActivity extends Activity {
30 
31     @Override
onCreate(Bundle savedInstanceState)32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34 
35         final ScaledTextView view = new ScaledTextView(this);
36         setContentView(view);
37 
38         ObjectAnimator animation = ObjectAnimator.ofFloat(view, "textScale", 1.0f, 10.0f);
39         animation.setDuration(3000);
40         animation.setRepeatCount(ObjectAnimator.INFINITE);
41         animation.setRepeatMode(ObjectAnimator.REVERSE);
42         animation.start();
43 
44     }
45 
46     public static class ScaledTextView extends View {
47         private static final String TEXT = "Hello libhwui! ";
48 
49         private final Paint mPaint;
50         private final Paint mShadowPaint;
51         private final Path mPath;
52 
53         private float mScale = 1.0f;
54 
ScaledTextView(Context c)55         public ScaledTextView(Context c) {
56             super(c);
57             setLayerType(LAYER_TYPE_HARDWARE, null);
58 
59             mPath = makePath();
60 
61             mPaint = new Paint();
62             mPaint.setAntiAlias(true);
63             mPaint.setTextSize(20.0f);
64 
65             mShadowPaint = new Paint();
66             mShadowPaint.setAntiAlias(true);
67             mShadowPaint.setShadowLayer(3.0f, 0.0f, 3.0f, 0xff000000);
68             mShadowPaint.setTextSize(20.0f);
69         }
70 
getTextScale()71         public float getTextScale() {
72             return mScale;
73         }
74 
setTextScale(float scale)75         public void setTextScale(float scale) {
76             mScale = scale;
77             invalidate();
78         }
79 
makePath()80         private static Path makePath() {
81             Path path = new Path();
82             buildPath(path);
83             return path;
84         }
85 
buildPath(Path path)86         private static void buildPath(Path path) {
87             path.moveTo(0.0f, 0.0f);
88             path.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f);
89             path.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f);
90             path.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f);
91         }
92 
93         @Override
onDraw(Canvas canvas)94         protected void onDraw(Canvas canvas) {
95             super.onDraw(canvas);
96 
97             canvas.drawText(TEXT, 30.0f, 30.0f, mPaint);
98             mPaint.setTextAlign(Paint.Align.CENTER);
99             canvas.drawText(TEXT, 30.0f, 50.0f, mPaint);
100             mPaint.setTextAlign(Paint.Align.RIGHT);
101             canvas.drawText(TEXT, 30.0f, 70.0f, mPaint);
102 
103             canvas.save();
104             canvas.translate(400.0f, 0.0f);
105             canvas.scale(3.0f, 3.0f);
106             mPaint.setTextAlign(Paint.Align.LEFT);
107             mPaint.setStrikeThruText(true);
108             canvas.drawText(TEXT, 30.0f, 30.0f, mPaint);
109             mPaint.setStrikeThruText(false);
110             mPaint.setTextAlign(Paint.Align.CENTER);
111             canvas.drawText(TEXT, 30.0f, 50.0f, mPaint);
112             mPaint.setTextAlign(Paint.Align.RIGHT);
113             canvas.drawText(TEXT, 30.0f, 70.0f, mPaint);
114             canvas.restore();
115 
116             mPaint.setTextAlign(Paint.Align.LEFT);
117             canvas.translate(0.0f, 100.0f);
118 
119             canvas.save();
120             canvas.scale(mScale, mScale);
121             canvas.drawText(TEXT, 30.0f, 30.0f, mPaint);
122             canvas.restore();
123 
124             canvas.translate(0.0f, 250.0f);
125             canvas.save();
126             canvas.scale(3.0f, 3.0f);
127             canvas.drawText(TEXT, 30.0f, 30.0f, mShadowPaint);
128             canvas.translate(100.0f, 0.0f);
129 //            canvas.drawTextOnPath(TEXT + TEXT + TEXT, mPath, 0.0f, 0.0f, mPaint);
130             canvas.restore();
131 
132             float width = mPaint.measureText(TEXT);
133 
134             canvas.translate(500.0f, 0.0f);
135             canvas.rotate(45.0f, width * 3.0f / 2.0f, 0.0f);
136             canvas.scale(3.0f, 3.0f);
137             canvas.drawText(TEXT, 30.0f, 30.0f, mPaint);
138         }
139     }
140 }
141