今天写了个onDoubleTap事件的处理方法,双击屏幕事件的放大缩小的程序,双击屏幕图片放大,再双击屏幕图片缩小到原来的效果。 请看截图的效果!
原图: 双击屏幕放大后的效果:
现把代码贴出来:
一、MainActivty.java类中的代码:
package com.cn.android;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
public class mainActivity extends Activity implements OnGestureListener{
private ImageShowView mImageShowView = null;
private GestureDetector gestureScanner;
private double Scale = 1.0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageShowView = new ImageShowView(this);
setContentView(mImageShowView);
gestureScanner = new GestureDetector(this);
gestureScanner.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener(){
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
// 双击时产生一次
if(Scale == 1.0){
Scale = Scale + 0.5;
mImageShowView.setScale(Scale);
}
else if(Scale == 1.5){
Scale = Scale - 0.5;
mImageShowView.setScale(Scale);
}
Log.v("daming", "onDoubleTap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
// 双击时产生两次
Log.v("daming", "onDoubleTapEvent");
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
//短快的点击算一次单击
Log.v("daming", "onSingleTapConfirmed");
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
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;
}
}
二、ImageShowView.java中的代码:
package com.cn.android;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
public class ImageShowView extends View implements Runnable{
Bitmap mBitmap = null;
int mBitmapWidth = 0;//定义图片的宽
int mBitmapHeight = 0;//定义图片的高
private double Scale = 1.0;
Matrix mMatrix = new Matrix();
public ImageShowView(Context context) {
super(context);
mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.image1)).getBitmap();
mBitmapWidth = mBitmap.getWidth();
mBitmapHeight = mBitmap.getHeight();
//开启线程
new Thread(this).start();
}
public void setScale(double scale){
this.Scale = scale;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mMatrix.reset();
mMatrix.postScale((float)Scale, (float)Scale);//设置缩放
Bitmap mBitmap2 = Bitmap.createBitmap(mBitmap,0,0,mBitmapWidth,mBitmapHeight,mMatrix,true);
//绘制旋转之后的图像
ImageShowView.drawImage(canvas,mBitmap2,(320-mBitmapWidth)/2,10);
mBitmap2 = null;
}
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
postInvalidate();
}
}
private static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y) {
// TODO Auto-generated method stub
canvas.drawBitmap(bitmap, x, y, null);
}
}
说明:以上就是我的全部代码,项目中的图片就不上传了,大家可以自己传个图片上去就可以了,有需要完整代码的可以留邮箱,大家有任何问题,可以给我留言,我看到就回复!大家有好的意见可以提出来,共同学习,共同进步!