package com.example.gesturetest; android
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; app
@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)); ide
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;
} this
// 將Activity上的觸碰事件交給GestureDetector
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return detector.onTouchEvent(event);
} code
@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;
} three
/**
* 當用戶在觸摸屏上"拖過"時觸發該方法。其中 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(); } }