Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 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; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 24 | import android.graphics.Path; |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.view.View; |
| 27 | |
| 28 | @SuppressWarnings({"UnusedDeclaration"}) |
| 29 | public 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 Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 47 | private static final String TEXT = "Hello libhwui! "; |
| 48 | |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 49 | private final Paint mPaint; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 50 | private final Paint mShadowPaint; |
| 51 | private final Path mPath; |
| 52 | |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 53 | private float mScale = 1.0f; |
| 54 | |
| 55 | public ScaledTextView(Context c) { |
| 56 | super(c); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 57 | setLayerType(LAYER_TYPE_HARDWARE, null); |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 58 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 59 | mPath = makePath(); |
| 60 | |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 61 | mPaint = new Paint(); |
| 62 | mPaint.setAntiAlias(true); |
| 63 | mPaint.setTextSize(20.0f); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 64 | |
| 65 | mShadowPaint = new Paint(); |
| 66 | mShadowPaint.setAntiAlias(true); |
| 67 | mShadowPaint.setShadowLayer(3.0f, 0.0f, 3.0f, 0xff000000); |
| 68 | mShadowPaint.setTextSize(20.0f); |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public float getTextScale() { |
| 72 | return mScale; |
| 73 | } |
| 74 | |
| 75 | public void setTextScale(float scale) { |
| 76 | mScale = scale; |
| 77 | invalidate(); |
| 78 | } |
| 79 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 80 | 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 Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 93 | @Override |
| 94 | protected void onDraw(Canvas canvas) { |
| 95 | super.onDraw(canvas); |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 96 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 97 | canvas.drawText(TEXT, 30.0f, 30.0f, mPaint); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 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); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 102 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 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); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 118 | |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 119 | canvas.save(); |
| 120 | canvas.scale(mScale, mScale); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 121 | canvas.drawText(TEXT, 30.0f, 30.0f, mPaint); |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 122 | canvas.restore(); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 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); |
Romain Guy | 5b4628a | 2013-01-07 18:11:52 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |