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
android:id="@+id/layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
android:layout_width="fill_parent" android:layout_height="wrap_content" />
android:layout_width="match_parent" android:layout_height="match_parent"
android:scaleType="matrix" />