blob: a4e9b52f4290fb15165d1f820bbb1be6a3cdb384 [file] [log] [blame]
Romain Guy5b4628a2013-01-07 18:11:52 -08001/*
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.animation.ObjectAnimator;
20import android.app.Activity;
21import android.content.Context;
22import android.graphics.Canvas;
23import android.graphics.Paint;
Romain Guye3a9b242013-01-08 11:15:30 -080024import android.graphics.Path;
Romain Guy5b4628a2013-01-07 18:11:52 -080025import android.os.Bundle;
26import android.view.View;
27
28@SuppressWarnings({"UnusedDeclaration"})
29public class ScaledTextActivity extends Activity {
30
31 @Override
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 {
Romain Guye3a9b242013-01-08 11:15:30 -080047 private static final String TEXT = "Hello libhwui! ";
48
Romain Guy5b4628a2013-01-07 18:11:52 -080049 private final Paint mPaint;
Romain Guye3a9b242013-01-08 11:15:30 -080050 private final Paint mShadowPaint;
51 private final Path mPath;
52
Romain Guy5b4628a2013-01-07 18:11:52 -080053 private float mScale = 1.0f;
54
55 public ScaledTextView(Context c) {
56 super(c);
Romain Guyc74f45a2013-02-26 19:10:14 -080057 setLayerType(LAYER_TYPE_HARDWARE, null);
Romain Guy5b4628a2013-01-07 18:11:52 -080058
Romain Guye3a9b242013-01-08 11:15:30 -080059 mPath = makePath();
60
Romain Guy5b4628a2013-01-07 18:11:52 -080061 mPaint = new Paint();
62 mPaint.setAntiAlias(true);
63 mPaint.setTextSize(20.0f);
Romain Guye3a9b242013-01-08 11:15:30 -080064
65 mShadowPaint = new Paint();
66 mShadowPaint.setAntiAlias(true);
67 mShadowPaint.setShadowLayer(3.0f, 0.0f, 3.0f, 0xff000000);
68 mShadowPaint.setTextSize(20.0f);
Romain Guy5b4628a2013-01-07 18:11:52 -080069 }
70
71 public float getTextScale() {
72 return mScale;
73 }
74
75 public void setTextScale(float scale) {
76 mScale = scale;
77 invalidate();
78 }
79
Romain Guye3a9b242013-01-08 11:15:30 -080080 private static Path makePath() {
81 Path path = new Path();
82 buildPath(path);
83 return path;
84 }
85
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
Romain Guy5b4628a2013-01-07 18:11:52 -080093 @Override
94 protected void onDraw(Canvas canvas) {
95 super.onDraw(canvas);
Romain Guy5b4628a2013-01-07 18:11:52 -080096
Romain Guye3a9b242013-01-08 11:15:30 -080097 canvas.drawText(TEXT, 30.0f, 30.0f, mPaint);
Romain Guyc74f45a2013-02-26 19:10:14 -080098 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);
Romain Guye3a9b242013-01-08 11:15:30 -0800102
Romain Guyc74f45a2013-02-26 19:10:14 -0800103 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);
Romain Guye3a9b242013-01-08 11:15:30 -0800118
Romain Guy5b4628a2013-01-07 18:11:52 -0800119 canvas.save();
120 canvas.scale(mScale, mScale);
Romain Guye3a9b242013-01-08 11:15:30 -0800121 canvas.drawText(TEXT, 30.0f, 30.0f, mPaint);
Romain Guy5b4628a2013-01-07 18:11:52 -0800122 canvas.restore();
Romain Guye3a9b242013-01-08 11:15:30 -0800123
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);
Romain Guy5b4628a2013-01-07 18:11:52 -0800138 }
139 }
140}