一、概述
ScaleGestureDetector这个类是专门用来检测两个手指在屏幕上做缩放的手势用的,最简略的使用便是用来缩放图片或许缩放网页。
二、要求
使用ScaleGestureDetector这个类完结图片缩放。
三、完结
新建工程MyScale,修正main.xml文件,在里面增加一个Button和一个SurfaceView,如下:
android:layout_width=fill_parent
android:layout_height=fill_parent
android:orientation=vertical >
android:id=@+id/button
android:layout_width=fill_parent
android:layout_height=wrap_content
android:text=显现图片
/>
android:id=@+id/surfaceview
android:layout_width=fill_parent
android:layout_height=fill_parent
/>
修正MyScaleActivity.java文件,主要是完结按钮的监听和界说一个类完结ScaleGestureDetector.OnScaleGestureListener接口:
package com.nan.scale;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class MyScaleActivity extends Activity
{
private Button mButton = null;
private SurfaceView mSurfaceView = null;
private SurfaceHolder mSurfaceHolder = null;
private ScaleGestureDetector mScaleGestureDetector = null;
private Bitmap mBitmap = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurfaceView = (SurfaceView)this.findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mScaleGestureDetector = new ScaleGestureDetector(this,new ScaleGestureListener());
mButton = (Button)this.findViewById(R.id.button);
//按钮监听
mButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mmm);
//确定整个SurfaceView
Canvas mCanvas = mSurfaceHolder.lockCanvas();
//画图
mCanvas.drawBitmap(mBitmap, 0f, 0f, null);
//制作完结,提交修正
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
//从头锁一次
mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
//回来给ScaleGestureDetector来处理
return mScaleGestureDetector.onTouchEvent(event);
}
public class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener
{
@Override
public boolean onScale(ScaleGestureDetector detector)
{
// TODO Auto-generated method stub
Matrix mMatrix = new Matrix();
//缩放份额
float scale = detector.getScaleFactor()/3;
mMatrix.setScale(scale, scale);
//确定整个SurfaceView
Canvas mCanvas = mSurfaceHolder.lockCanvas();
//清屏
mCanvas.drawColor(Color.BLACK);
//画缩放后的图
mCanvas.drawBitmap(mBitmap, mMatrix, null);
//制作完结,提交修正
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
//从头锁一次
mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
return false;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector)
{
// TODO Auto-generated method stub
//一定要回来true才会进入onScale()这个函数
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector)
{
// TODO Auto-generated method stub
}
}
}