Thursday, November 24, 2011

Scale bitmap according to ScaleGestureDetector

In the last exercise, "Detect pinch zoom using ScaleGestureDetector" was shown. it's modified to scale a bitmap accordingly. (It's for demonstration only, may be not practical in real use.

Scale bitmap according to ScaleGestureDetector

package com.exercise.AndroidPinchZoom;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.ImageView;
import android.widget.TextView;

public class AndroidPinchZoomActivity extends Activity {

TextView scaleGesture;
ImageView myImageView;
float curScale = 1F;
Bitmap bitmap;
int bmpWidth, bmpHeight;

ScaleGestureDetector scaleGestureDetector;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scaleGesture = (TextView)findViewById(R.id.ScaleGesture);
myImageView = (ImageView)findViewById(R.id.imageview);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
drawMatrix();

scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
}

private void drawMatrix(){

curScale = ((curScale - 1) * 10) + 1;
if (curScale < 0.1){
curScale = 0.1f;
}

Bitmap resizedBitmap;
int newHeight = (int) (bmpHeight * curScale);
int newWidth = (int) (bmpWidth * curScale);
resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
myImageView.setImageBitmap(resizedBitmap);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}

public class simpleOnScaleGestureListener extends SimpleOnScaleGestureListener {

@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
curScale = detector.getScaleFactor();
scaleGesture.setText(String.valueOf(curScale));
drawMatrix();
return true;
}

@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
return true;
}

@Override
public void onScaleEnd(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
super.onScaleEnd(detector);
}

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/ScaleGesture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center" />
</LinearLayout>


Download the files.

Related article:
- Implement Pinch Zoom in OnTouchListener



No comments: