Android应用开发 ScaleGestureDetector(缩放手势
一、概述
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
}
}
}
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)