Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.google.android.test.transform; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Matrix; |
| 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.os.Bundle; |
| 25 | import android.util.DisplayMetrics; |
| 26 | import android.util.Log; |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 27 | import android.view.MotionEvent; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 28 | import android.view.ScaleGestureDetector; |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.widget.LinearLayout; |
| 31 | |
| 32 | public class TransformTestActivity extends Activity { |
| 33 | public TransformTestActivity() { |
| 34 | super(); |
| 35 | init(false); |
| 36 | } |
| 37 | |
| 38 | public TransformTestActivity(boolean noCompat) { |
| 39 | super(); |
| 40 | init(noCompat); |
| 41 | } |
| 42 | |
| 43 | public void init(boolean noCompat) { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected void onCreate(Bundle savedInstanceState) { |
| 49 | super.onCreate(savedInstanceState); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 50 | |
| 51 | this.setTitle(R.string.act_title); |
| 52 | LinearLayout root = new LinearLayout(this); |
| 53 | root.setOrientation(LinearLayout.VERTICAL); |
| 54 | |
| 55 | TransformView view = new TransformView(getApplicationContext()); |
| 56 | Drawable drawable = getResources().getDrawable(R.drawable.logo); |
| 57 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicWidth()); |
| 58 | view.setDrawable(drawable); |
| 59 | |
| 60 | root.addView(view); |
| 61 | setContentView(root); |
| 62 | } |
| 63 | |
| 64 | private class TransformView extends View { |
| 65 | private Drawable mDrawable; |
| 66 | private float mPosX; |
| 67 | private float mPosY; |
| 68 | private float mScale = 1.f; |
| 69 | private Matrix mMatrix; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 70 | private ScaleGestureDetector mDetector; |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 71 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 72 | private float mLastX; |
| 73 | private float mLastY; |
| 74 | |
| 75 | private class Listener implements ScaleGestureDetector.OnScaleGestureListener { |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 76 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 77 | public boolean onScale(ScaleGestureDetector detector) { |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 78 | float scale = detector.getScaleFactor(); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 79 | |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 80 | Log.d("ttest", "Scale: " + scale); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 81 | |
| 82 | // Limit the scale so our object doesn't get too big or disappear |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 83 | if (mScale * scale > 0.1f) { |
| 84 | if (mScale * scale < 10.f) { |
| 85 | mScale *= scale; |
| 86 | } else { |
| 87 | mScale = 10.f; |
| 88 | } |
| 89 | } else { |
| 90 | mScale = 0.1f; |
| 91 | } |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 92 | |
| 93 | Log.d("ttest", "mScale: " + mScale + " mPos: (" + mPosX + ", " + mPosY + ")"); |
| 94 | |
| 95 | float sizeX = mDrawable.getIntrinsicWidth()/2; |
| 96 | float sizeY = mDrawable.getIntrinsicHeight()/2; |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 97 | float centerX = detector.getFocusX(); |
| 98 | float centerY = detector.getFocusY(); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 99 | float diffX = centerX - mPosX; |
| 100 | float diffY = centerY - mPosY; |
| 101 | diffX = diffX*scale - diffX; |
| 102 | diffY = diffY*scale - diffY; |
| 103 | mPosX -= diffX; |
| 104 | mPosY -= diffY; |
| 105 | mMatrix.reset(); |
| 106 | mMatrix.postTranslate(-sizeX, -sizeY); |
| 107 | mMatrix.postScale(mScale, mScale); |
| 108 | mMatrix.postTranslate(mPosX, mPosY); |
| 109 | |
| 110 | invalidate(); |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 115 | public boolean onScaleBegin(ScaleGestureDetector detector) { |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 119 | public void onScaleEnd(ScaleGestureDetector detector) { |
| 120 | mLastX = detector.getFocusX(); |
| 121 | mLastY = detector.getFocusY(); |
| 122 | } |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public TransformView(Context context) { |
| 126 | super(context); |
| 127 | mMatrix = new Matrix(); |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 128 | mDetector = new ScaleGestureDetector(context, new Listener()); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 129 | DisplayMetrics metrics = context.getResources().getDisplayMetrics(); |
| 130 | mPosX = metrics.widthPixels/2; |
| 131 | mPosY = metrics.heightPixels/2; |
| 132 | } |
| 133 | |
| 134 | public void setDrawable(Drawable d) { |
| 135 | mDrawable = d; |
| 136 | |
| 137 | float sizeX = mDrawable.getIntrinsicWidth()/2; |
| 138 | float sizeY = mDrawable.getIntrinsicHeight()/2; |
| 139 | mMatrix.reset(); |
| 140 | mMatrix.postTranslate(-sizeX, -sizeY); |
| 141 | mMatrix.postScale(mScale, mScale); |
| 142 | mMatrix.postTranslate(mPosX, mPosY); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean onTouchEvent(MotionEvent event) { |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 147 | mDetector.onTouchEvent(event); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 148 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 149 | // Handling single finger pan |
| 150 | if (!mDetector.isInProgress()) { |
| 151 | switch (event.getAction()) { |
| 152 | case MotionEvent.ACTION_DOWN: |
| 153 | mLastX = event.getX(); |
| 154 | mLastY = event.getY(); |
| 155 | break; |
| 156 | |
| 157 | case MotionEvent.ACTION_MOVE: |
| 158 | final float x = event.getX(); |
| 159 | final float y = event.getY(); |
| 160 | mPosX += x - mLastX; |
| 161 | mPosY += y - mLastY; |
| 162 | mLastX = x; |
| 163 | mLastY = y; |
| 164 | |
| 165 | float sizeX = mDrawable.getIntrinsicWidth()/2; |
| 166 | float sizeY = mDrawable.getIntrinsicHeight()/2; |
| 167 | |
| 168 | mMatrix.reset(); |
| 169 | mMatrix.postTranslate(-sizeX, -sizeY); |
| 170 | mMatrix.postScale(mScale, mScale); |
| 171 | mMatrix.postTranslate(mPosX, mPosY); |
| 172 | invalidate(); |
| 173 | break; |
| 174 | } |
| 175 | } |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 176 | |
Adam Powell | ae542ff | 2010-01-13 16:29:27 -0800 | [diff] [blame] | 177 | return true; |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void onDraw(Canvas canvas) { |
| 182 | int saveCount = canvas.getSaveCount(); |
| 183 | canvas.save(); |
| 184 | canvas.concat(mMatrix); |
| 185 | mDrawable.draw(canvas); |
| 186 | canvas.restoreToCount(saveCount); |
| 187 | } |
| 188 | } |
| 189 | } |