Android應(yīng)用開(kāi)發(fā) ScaleGestureDetector(縮放手勢(shì)檢測(cè))
掃描二維碼
隨時(shí)隨地手機(jī)看文章
一、概述
ScaleGestureDetector這個(gè)類是專門用來(lái)檢測(cè)兩個(gè)手指在屏幕上做縮放的手勢(shì)用的,最簡(jiǎn)單的應(yīng)用就是用來(lái)縮放圖片或者縮放網(wǎng)頁(yè)。
二、要求
利用ScaleGestureDetector這個(gè)類實(shí)現(xiàn)圖片縮放。
三、實(shí)現(xiàn)
新建工程MyScale,修改main.xml文件,在里面添加一個(gè)Button和一個(gè)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文件,主要是實(shí)現(xiàn)按鈕的監(jiān)聽(tīng)和定義一個(gè)類實(shí)現(xiàn)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);
//按鈕監(jiān)聽(tīng)
mButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mmm);
//鎖定整個(gè)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來(lái)處理
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);
//鎖定整個(gè)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才會(huì)進(jìn)入onScale()這個(gè)函數(shù)
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector)
{
// TODO Auto-generated method stub
}
}
}