|
2 | 2 |
|
3 | 3 | import android.app.Activity;
|
4 | 4 | import android.graphics.Matrix;
|
| 5 | +import android.graphics.Point; |
5 | 6 | import android.graphics.PointF;
|
6 | 7 | import android.graphics.drawable.Drawable;
|
7 | 8 | import android.os.Bundle;
|
8 | 9 | import android.util.Log;
|
| 10 | +import android.view.Display; |
9 | 11 | import android.view.MotionEvent;
|
10 | 12 | import android.view.ScaleGestureDetector;
|
11 | 13 | import android.view.View;
|
@@ -52,33 +54,44 @@ public class TouchActivity extends Activity implements OnTouchListener {
|
52 | 54 | private ScaleGestureDetector mScaleDetector;
|
53 | 55 | private RotateGestureDetector mRotateDetector;
|
54 | 56 | private MoveGestureDetector mMoveDetector;
|
55 |
| - private ShoveGestureDetector mShoveDetector; |
| 57 | + private ShoveGestureDetector mShoveDetector; |
56 | 58 |
|
| 59 | + @SuppressWarnings("deprecation") |
57 | 60 | @Override
|
58 | 61 | public void onCreate(Bundle savedInstanceState) {
|
59 | 62 | super.onCreate(savedInstanceState);
|
60 | 63 | setContentView(R.layout.main);
|
61 | 64 |
|
| 65 | + // Determine the center of the screen to center 'earth' |
| 66 | + Display display = getWindowManager().getDefaultDisplay(); |
| 67 | + mFocusX = display.getWidth()/2f; |
| 68 | + mFocusY = display.getHeight()/2f; |
| 69 | + |
| 70 | + // Set this class as touchListener to the ImageView |
62 | 71 | ImageView view = (ImageView) findViewById(R.id.imageView);
|
63 | 72 | view.setOnTouchListener(this);
|
64 | 73 |
|
65 |
| - // View is scaled by matrix, so scale initially |
66 |
| - mMatrix.postScale(mScaleFactor, mScaleFactor); |
67 |
| - view.setImageMatrix(mMatrix); |
68 |
| - |
69 |
| - // Dimensions of image |
| 74 | + // Determine dimensions of 'earth' image |
70 | 75 | Drawable d = this.getResources().getDrawable(R.drawable.earth);
|
71 | 76 | mImageHeight = d.getIntrinsicHeight();
|
72 | 77 | mImageWidth = d.getIntrinsicWidth();
|
73 |
| - Log.d(LOG_TAG, "Image dimensions -> height: " + mImageHeight + "px, width: " + mImageWidth + "px"); |
| 78 | + |
| 79 | + // View is scaled and translated by matrix, so scale and translate initially |
| 80 | + float scaledImageCenterX = (mImageWidth*mScaleFactor)/2; |
| 81 | + float scaledImageCenterY = (mImageHeight*mScaleFactor)/2; |
| 82 | + |
| 83 | + mMatrix.postScale(mScaleFactor, mScaleFactor); |
| 84 | + mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY); |
| 85 | + view.setImageMatrix(mMatrix); |
74 | 86 |
|
75 | 87 | // Setup Gesture Detectors
|
76 |
| - mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener()); |
| 88 | + mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener()); |
77 | 89 | mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());
|
78 |
| - mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener()); |
79 |
| - mShoveDetector = new ShoveGestureDetector(getApplicationContext(), new ShoveListener()); |
| 90 | + mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener()); |
| 91 | + mShoveDetector = new ShoveGestureDetector(getApplicationContext(), new ShoveListener()); |
80 | 92 | }
|
81 | 93 |
|
| 94 | + @SuppressWarnings("deprecation") |
82 | 95 | public boolean onTouch(View v, MotionEvent event) {
|
83 | 96 | mScaleDetector.onTouchEvent(event);
|
84 | 97 | mRotateDetector.onTouchEvent(event);
|
|
0 commit comments