Scale listener
package app.test;
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
public class Test extends Activity {
private ImageView image;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1f;
private Matrix mMatrix = new Matrix();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
mScaleDetector = new ScaleGestureDetector(this, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
mScaleDetector.onTouchEvent(ev);
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
mMatrix.setScale(mScaleFactor, mScaleFactor);
image.setImageMatrix(mMatrix);
image.invalidate();
return true;
}
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TextView android:text="Use the pinch gesture to change the image size"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<ImageView android:id="@+id/image" android:src="@drawable/icon"
android:layout_width="match_parent" android:layout_height="match_parent"
android:scaleType="matrix" />
</LinearLayout>
Related examples in the same category