下面的例子是用手势来缩放图片,手指往右划是放大,往左是缩小
下面效果在手机模拟器上呈现的只能是鼠标光标的划动了
package WangLi.IO.GestureZoom;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ImageView;
public class GestureZoom extends Activity implements OnGestureListener
{
GestureDetector detector;
ImageView imageView;
//初始的图片资源
Bitmap bitmap;
//定义图片的宽,高
int width,height;
//记录当前的缩放比
float currentScale = 1;
//控制图片缩放的Matrix对象
Matrix matrix;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建手势检测器
detector = new GestureDetector(this);
imageView = (ImageView)findViewById(R.id.show);
matrix = new Matrix();
//获取被缩放的源图片
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hotel);
//获得位图宽
width = bitmap.getWidth();
//获得位图高
height = bitmap.getHeight();
//设置ImageView初始化时显示的图片
imageView.setImageBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.hotel));
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
//将Activity上的触碰事件交给GestureDetector处理
return detector.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
velocityX = velocityX > 4000 ? 4000 : velocityX;
velocityX = velocityX < -4000 ? -4000 : velocityX;
//根据手势的速度来计算缩放比,如果velocity > 0,放大图像,否则缩小图像
currentScale += currentScale * velocityX / 4000.0f;
//保证currentScale不会等于0
currentScale = currentScale > 0.01 ? currentScale : 0.01f;
//重置Matrix
matrix.reset();
//缩放Matrix
matrix.setScale(currentScale, currentScale ,160, 200);
BitmapDrawable tmp = (BitmapDrawable)imageView.getDrawable();
//如果图片还未回收,先强制回收该图片
if(!tmp.getBitmap().isRecycled())
{
tmp.getBitmap().recycle();
}
//根据原始位图和Matrix创建新图片
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,true);
imageView.setImageBitmap(bitmap2);
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}