package com.example.gesturetest;
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.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MainActivity extends Activity implements OnGestureListener {
// 定义手势检测器实例
GestureDetector detector;
// 定义一个动画数组,用于为ViewFlipper指定切换动画的效果
Animation[] animations = new Animation[4];
// 定义手势动作两点之间的最小距离
final int FLIP_DISTANCE = 50;
ImageView iv;
ViewFlipper vf;
// 初始的图片资源
Bitmap bitmap;
// 定义图片的宽和高
int width, height;
// 设置当前的缩放比
float currentScale = 1;
// 控制图片缩放的Matrix对象
Matrix matrix;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detector = new GestureDetector(this);
iv = (ImageView) findViewById(R.id.iv);
vf = (ViewFlipper) findViewById(R.id.flipper);
vf.addView(addImageView(R.drawable.a));
vf.addView(addImageView(R.drawable.two));
vf.addView(addImageView(R.drawable.three));
vf.addView(addImageView(R.drawable.four));
animations[0] = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
animations[1] = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
animations[2] = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
animations[3] = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
matrix = new Matrix();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
width = bitmap.getWidth();
height = bitmap.getHeight();
iv.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.a));
}
View addImageView(int resID) {
ImageView iv = new ImageView(this);
iv.setImageResource(resID);
iv.setScaleType(ImageView.ScaleType.CENTER);
return iv;
}
// 将Activity上的触碰事件交给GestureDetector
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return detector.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* 当触碰事件按下时触发该方法
*/
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
showToast("onDown");
return false;
}
/**
* 当用户在触摸屏上"拖过"时触发该方法。其中 float velocityX, float velocityY代表"拖动"动作在横向.纵向上的速度
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
showToast("onFling");
/*
* 图片缩放 velocityX = velocityX > 4000 ? 4000 : velocityX; velocityX =
* velocityX < -4000 ? -4000 : velocityX; 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 bd = (BitmapDrawable)
* iv.getDrawable(); // 如何图片还未回收,先强制回收该图片 if (bd != null &&
* !bd.getBitmap().isRecycled()) { bd.getBitmap().recycle(); } //
* 根据原始位图和Matrix创建新图片 Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0,
* width, height, matrix, true); iv.setImageBitmap(bitmap2);
*/
// 切换图片
/*
* 如果第一个触点的X坐标大于第二个出点的X坐标超过FLIP_DISTANCE 代表从右向左滑动
*/
if (e1.getX() - e2.getX() > FLIP_DISTANCE) {
vf.setInAnimation(animations[0]);
vf.setOutAnimation(animations[1]);
vf.showPrevious();
return true;
}
if (e2.getX() - e1.getX() > FLIP_DISTANCE) {
vf.setInAnimation(animations[2]);
vf.setOutAnimation(animations[3]);
vf.showNext();
return true;
}
return false;
}
/**
* 当用户在屏幕上长按时触发该方法
*/
@Override
public void onLongPress(MotionEvent e) {
showToast("onLongPress");
}
/**
* 当用户在屏幕上"滚动"的时候触发该方法
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
showToast("onScroll");
return false;
}
/**
* 当用户在触摸屏上按下,但为滚动时候,触发该方法
*/
@Override
public void onShowPress(MotionEvent e) {
showToast("onShowPress");
}
/**
* 用户在触摸屏上的轻击事件将会触发该方法
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
showToast("onSingleTapUp");
return false;
}
private void showToast(String content) {
Toast.makeText(this, "点击:" + content, Toast.LENGTH_SHORT).show();
}
}