android Gesture 简单Demo 源码
今天想学习一下anddroid Gesture.在网上找了些资料,自己写了个小Demo.
实现了手动拖动图片
用户可以从左往右、从右往左、从上到下、从下到上拖动图片。
图片进入时添加了简单的Animation动画.
有需要的同学可以下载下来看看。
擦擦,发现不能上传rar文件
那只能贴出来了。。
Java类:
package com.covics.zfh;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HelloGestureActivity extends Activity implements OnTouchListener, OnGestureListener{
private static final String TAG = "GestureActivity------->";
private GestureDetector mGestureDetector = new GestureDetector(this);
private TextView tv;
private ImageView ivImage;
private int[] imageId = {R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7};
private int mCurrentIndex = 0;
private Animation leftInAnimation;
private Animation upInAnimation;
private Animation rightInAnimation;
private Animation downInAnimation;
private Context mContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = getApplicationContext();
init();
ivImage.setOnTouchListener(this);
ivImage.setFocusable(true);
ivImage.setClickable(true);
ivImage.setLongClickable(true);
ivImage.setImageResource(imageId[mCurrentIndex]);
mGestureDetector.setIsLongpressEnabled(true);
}
private void init(){
ivImage = (ImageView) findViewById(R.id.image);
leftInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.left_in);
upInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.up_in);
rightInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.right_in);
downInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.down_in);
}
// 在onTouch()方法中我们调用GestureDetector的onTouchEvent方法,将捕捉到 的
// MotionEvent交给GestureDetector 来分析是否有合适的callBack函数来处理手势
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
// 用户轻点触摸屏,由1个MotionEvent ACTION_DOWN触发
@Override
public boolean onDown(MotionEvent e) {
return false;
}
// 用户轻触触摸屏后松开,由1个MotionEvent ACTION_UP触发
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
// 用户轻点触摸屏,尚末松开或者拖动,由1个MotionEvent ACTION_DOWN触发
// 注意和onDown()的区别,是没有松开或者拖动的状态
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
// 用户按下触摸屏并拖动 由1个MotionEvent EVENT_DOWN 多个EVENT_MOVE触发
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
// 用户长按触摸屏,由多个MotionEvent EVENT_DOWN触发
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
// 用户按下触摸屏,快速移动后松开,由1个MotionEvent ACTION_DOWN
// 多个EVENT_MOVE 和1个EVENT_UP触发
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
// 参数解释:
 
补充:移动开发 , Android ,