android pinch zoom
I implemented a pinch zoom for my TextView
, using this tutorial. The resulting code is this:
private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;
and in onCreate():
// Zoom handlers
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
// We can be in one of these 2 states
static final int NONE = 0;
static final int ZOOM = 1;
int mode = NONE;
static final int MIN_FONT_SIZE = 10;
static final int MAX_FONT_SIZE = 50;
float oldDist = 1f;
@Override
public boolean onTouch(View v, MotionEvent event) {
TextView textView = (TextView) findViewById(R.id.text);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM) {
float newDist = spacing(event);
// If you want to tweak font scaling, this is the place to go.
if (newDist > 10f) {
float scale = newDist / oldDist;
if (scale > 1) {
scale = 1.1f;
} else if (scale < 1) {
scale = 0.95f;
}
float currentSize = textView.getTextSize() * scale;
if ((currentSize < MAX_FONT_SIZE && currentSize > MIN_FONT_SIZE)
||(currentSize >= MAX_FONT_SIZE && scale < 1)
|| (currentSize <= MIN_FONT_SIZE && scale > 1)) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize);
}
}
}
break;
}
return false;
}
Magic constants 1.1 and 0.95 were chosen empirically (using scale
variable for this purpose made my TextView
behave kind of weird).
Updated Answer
Code can be found here : official-doc
Answer Outdated
Check out the following links which may help you
Best examples are provided in the below links, which you can refactor to meet your requirements.
Implementing the pinch zoom gesture
Android-pinch
GestureDetector.SimpleOnGestureListener
For android 2.2+ (api level8), you can use ScaleGestureDetector.
you need a member:
private ScaleGestureDetector mScaleDetector;
in your constructor (or onCreate()) you add:
mScaleDetector = new ScaleGestureDetector(context, new OnScaleGestureListener() {
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d(LOG_KEY, "zoom ongoing, scale: " + detector.getScaleFactor());
return false;
}
});
You override onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent event) {
mScaleDetector.onTouchEvent(event);
return true;
}
If you draw your View by hand, in the onScale() you probably do store the scale factor in a member, then call invalidate() and use the scale factor when drawing in your onDraw(). Otherwise you can directly modify font sizes or things like that in the onScale().