ScaledTextActivity.java revision e3a9b24b5e3f9b2058486814a6d27729e51ad466
1e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy/* 2e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Copyright (C) 2010 The Android Open Source Project 3e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * 4e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Licensed under the Apache License, Version 2.0 (the "License"); 5e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * you may not use this file except in compliance with the License. 6e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * You may obtain a copy of the License at 7e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * 8e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * http://www.apache.org/licenses/LICENSE-2.0 9e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * 10e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Unless required by applicable law or agreed to in writing, software 11e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * distributed under the License is distributed on an "AS IS" BASIS, 12e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * See the License for the specific language governing permissions and 14e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * limitations under the License. 15e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy */ 16e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy 17a1db574036c9bc2d397b69f8200594027e1fff16Romain Guypackage com.android.test.hwui; 18a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy 19e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guyimport android.animation.ObjectAnimator; 20e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroyimport android.app.Activity; 21e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guyimport android.content.Context; 226554943a1dd6854c0f4976900956e556767b49e1Romain Guyimport android.graphics.Canvas; 233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyimport android.graphics.Paint; 243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyimport android.graphics.Path; 25e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guyimport android.os.Bundle; 26e5e0c50f7dfaccc220725c5595080e921ffda1e4Romain Guyimport android.view.View; 276554943a1dd6854c0f4976900956e556767b49e1Romain Guy 28b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian@SuppressWarnings({"UnusedDeclaration"}) 29e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guypublic class ScaledTextActivity extends Activity { 303b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 313b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy @Override 326554943a1dd6854c0f4976900956e556767b49e1Romain Guy protected void onCreate(Bundle savedInstanceState) { 336554943a1dd6854c0f4976900956e556767b49e1Romain Guy super.onCreate(savedInstanceState); 346554943a1dd6854c0f4976900956e556767b49e1Romain Guy 358f0095cd33558e9cc8a440047908e53b68906f5fRomain Guy final ScaledTextView view = new ScaledTextView(this); 36ce0537b80087a6225273040a987414b1dd081aa0Romain Guy setContentView(view); 37c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy 38f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy ObjectAnimator animation = ObjectAnimator.ofFloat(view, "textScale", 1.0f, 10.0f); 39c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy animation.setDuration(3000); 40079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy animation.setRepeatCount(ObjectAnimator.INFINITE); 41a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy animation.setRepeatMode(ObjectAnimator.REVERSE); 4261c8c9c5b2006d18e9310b6521c65b36ffe75ce4Romain Guy animation.start(); 4385bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy 44e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy } 45b051e895ccb696604349c6c5efe7c4747e1d1ab6Romain Guy 466c319ca1275c8db892c39b48fc54864c949f9171Romain Guy public static class ScaledTextView extends View { 4785bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy private static final String TEXT = "Hello libhwui! "; 4806f96e2652e4855b6520ad9dd70583677605b79aRomain Guy 490baaac5e9adf3ee280ae1239e2e58754a9d2b099Romain Guy private final Paint mPaint; 509d5316e3f56d138504565ff311145ac01621dff4Romain Guy private final Paint mShadowPaint; 51113e0824d6bddf4376240681f9cf6a2deded9498John Reck private final Path mPath; 5285bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy 53d1d4bb70704e8f37d0823837eacdae21ebe0ed05Romain Guy private float mScale = 1.0f; 54b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio 55e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy public ScaledTextView(Context c) { 56e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy super(c); 57e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy 589d5316e3f56d138504565ff311145ac01621dff4Romain Guy mPath = makePath(); 599d5316e3f56d138504565ff311145ac01621dff4Romain Guy 60163935113919a184122b8b3bd672ef08c8df65dcRomain Guy mPaint = new Paint(); 61163935113919a184122b8b3bd672ef08c8df65dcRomain Guy mPaint.setAntiAlias(true); 62163935113919a184122b8b3bd672ef08c8df65dcRomain Guy mPaint.setTextSize(20.0f); 63163935113919a184122b8b3bd672ef08c8df65dcRomain Guy 64163935113919a184122b8b3bd672ef08c8df65dcRomain Guy mShadowPaint = new Paint(); 65163935113919a184122b8b3bd672ef08c8df65dcRomain Guy mShadowPaint.setAntiAlias(true); 66163935113919a184122b8b3bd672ef08c8df65dcRomain Guy mShadowPaint.setShadowLayer(3.0f, 0.0f, 3.0f, 0xff000000); 67244ada1d35419b7be9de0fc833bb03955b725ffaRomain Guy mShadowPaint.setTextSize(20.0f); 68bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy } 69244ada1d35419b7be9de0fc833bb03955b725ffaRomain Guy 70bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy public float getTextScale() { 71bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy return mScale; 72e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy } 73bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy 74bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy public void setTextScale(float scale) { 75bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy mScale = scale; 765baa3a62a97544669fba6d65a11c07f252e654ddSteve Block invalidate(); 77bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy } 78bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy 79bdb801ca993bcccbf2a4c2401d1720c3fa523777Romain Guy private static Path makePath() { 80e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy Path path = new Path(); 81a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy buildPath(path); 82a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy return path; 83a168d7372132d6c87835878794b6ed43d0d282fdRomain Guy } 849d5316e3f56d138504565ff311145ac01621dff4Romain Guy 859d5316e3f56d138504565ff311145ac01621dff4Romain Guy private static void buildPath(Path path) { 869d5316e3f56d138504565ff311145ac01621dff4Romain Guy path.moveTo(0.0f, 0.0f); 877ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy path.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f); 889d5316e3f56d138504565ff311145ac01621dff4Romain Guy path.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f); 899d5316e3f56d138504565ff311145ac01621dff4Romain Guy path.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f); 90e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy } 91244ada1d35419b7be9de0fc833bb03955b725ffaRomain Guy 927d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy @Override 937d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy protected void onDraw(Canvas canvas) { 94bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy super.onDraw(canvas); 9536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat canvas.drawARGB(255, 255, 255, 255); 96bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy 9736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat canvas.drawText(TEXT, 30.0f, 30.0f, mPaint); 98bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy 99bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy canvas.translate(0.0f, 50.0f); 100bdf7609867a3f886455c51dba91623a86cceb6e2Romain Guy 10136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat canvas.save(); 1028ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.scale(mScale, mScale); 10336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat canvas.drawText(TEXT, 30.0f, 30.0f, mPaint); 1048ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.restore(); 10536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat 1068ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.translate(0.0f, 250.0f); 1078ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.save(); 1088ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.scale(3.0f, 3.0f); 1098ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.drawText(TEXT, 30.0f, 30.0f, mShadowPaint); 1108ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.translate(100.0f, 0.0f); 1118ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy// canvas.drawTextOnPath(TEXT + TEXT + TEXT, mPath, 0.0f, 0.0f, mPaint); 1128ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy canvas.restore(); 1138ff6b9ebeeb24a6161ec6098e6bfdf8790ee5695Romain Guy 1147d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy float width = mPaint.measureText(TEXT); 1153b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1163b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy canvas.translate(500.0f, 0.0f); 1173b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy canvas.rotate(45.0f, width * 3.0f / 2.0f, 0.0f); 1183b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy canvas.scale(3.0f, 3.0f); 11917ab38f8a87bc90eab11373f878f220ce3031de6Ashok Bhat canvas.drawText(TEXT, 30.0f, 30.0f, mPaint); 1203b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy } 1213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy } 12217ab38f8a87bc90eab11373f878f220ce3031de6Ashok Bhat} 12317ab38f8a87bc90eab11373f878f220ce3031de6Ashok Bhat