android Gesture 简单Demo 源码
来源:互联网 发布:win8.1投影软件 编辑:程序博客网 时间:2024/05/13 03:15
今天想学习一下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函数来处理手势@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubreturn mGestureDetector.onTouchEvent(event);} // 用户轻点触摸屏,由1个MotionEvent ACTION_DOWN触发@Overridepublic boolean onDown(MotionEvent e) {return false;}// 用户轻触触摸屏后松开,由1个MotionEvent ACTION_UP触发@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}// 用户轻点触摸屏,尚末松开或者拖动,由1个MotionEvent ACTION_DOWN触发// 注意和onDown()的区别,是没有松开或者拖动的状态@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}// 用户按下触摸屏并拖动 由1个MotionEvent EVENT_DOWN 多个EVENT_MOVE触发@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}// 用户长按触摸屏,由多个MotionEvent EVENT_DOWN触发@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}// 用户按下触摸屏,快速移动后松开,由1个MotionEvent ACTION_DOWN // 多个EVENT_MOVE 和1个EVENT_UP触发@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stub// 参数解释: // e1:第1个ACTION_DOWN MotionEvent // e2:最后一个ACTION_MOVE MotionEvent // velocityX:X轴上的移动速度,像素/秒 // velocityY:Y轴上的移动速度,像素/秒 // 触发条件 : // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒int FLING_MIN_DISTANCE = 100; int FLING_MIN_VELOCITY = 200;if ((e1.getX() - e2.getX()) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {System.out.println(TAG + "onFling left");if (mCurrentIndex == imageId.length - 1) {Toast.makeText(mContext, "当前已经是最后一张了", Toast.LENGTH_LONG).show();} else {mCurrentIndex++;leftInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.left_in);ivImage.setAnimation(leftInAnimation);ivImage.setImageResource(imageId[mCurrentIndex]);}} else if ((e2.getX() - e1.getX()) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {System.out.println(TAG + "onFling right");if (mCurrentIndex == 0) {Toast.makeText(mContext, "当前已经是第一张了", Toast.LENGTH_LONG).show();} else {mCurrentIndex--;rightInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.right_in);ivImage.setAnimation(rightInAnimation);ivImage.setImageResource(imageId[mCurrentIndex]);}} else if ((e1.getY() - e2.getY()) > FLING_MIN_DISTANCE && Math.abs(velocityY) > FLING_MIN_VELOCITY) {System.out.println(TAG + "onFling up");if (mCurrentIndex == imageId.length - 1) {Toast.makeText(mContext, "当前已经是最后一张了", Toast.LENGTH_LONG).show();} else {mCurrentIndex++;//ivImage.setAnimation(rightOutAnimation);//leftInAnimation.setDuration(1000);upInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.up_in);ivImage.setAnimation(upInAnimation);ivImage.setImageResource(imageId[mCurrentIndex]);}} else if ((e2.getY() - e1.getY()) > FLING_MIN_DISTANCE && Math.abs(velocityY) > FLING_MIN_VELOCITY) {System.out.println(TAG + "onFling down");if (mCurrentIndex == 0) {Toast.makeText(mContext, "当前已经是第一张了", Toast.LENGTH_LONG).show();} else {mCurrentIndex--;downInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.down_in);ivImage.setAnimation(downInAnimation);ivImage.setImageResource(imageId[mCurrentIndex]);}}return false;}}
下面是动画效果XML实现
down_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:fromYDelta="-100%p" android:toYDelta="0" android:duration="900" /></set>
left_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800" /></set>
right_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="800" /></set>
up_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="900" /></set>
布局文件:
main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
记得添加图片哈。
- android Gesture 简单Demo 源码
- Android Gesture
- android Gesture
- Android Gesture
- Android手势源码浅析------手势的形成(Gesture)
- Android手势源码浅析------手势的形成(Gesture)
- Android手势源码浅析------手势的形成(Gesture)
- Gesture简单实例
- android 源码DEMO列表
- Android天气预报 简单Demo
- Android Media 简单 Demo
- android MediaRecorder简单Demo
- android gesture的学习
- Android Gesture Detector
- Android Gesture Detector
- android手势Gesture识别
- Android的Gesture Detector
- android - Gesture学习 1
- Jaa之产生随机数!...
- Paxos算法学习总结
- Linux-2.6.30.4在2440上的移植之触摸屏驱动
- 聚集索引(准备建模比赛)
- git daemon
- android Gesture 简单Demo 源码
- Ubuntu vim显示行号语法高亮自动缩进
- LPSTR、LPCSTR、LPTSTR、LPCTSTR的来源及意义
- http://projecteuler.net/problem=34 [Answer:40730]
- 转:ubuntu11.10面板上输入法图标消失解决办法
- JAVA线程中函数比较
- C++之构造函数与析构函数
- 平衡二叉树的创建
- Android 应用程序下载