上下左右滑动测试

来源:互联网 发布:js绑定两个click事件 编辑:程序博客网 时间:2024/06/15 22:11

运行效果图



修改布局文件activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/Iv"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:scaleType="center"        android:src="@drawable/figure_bg" /></LinearLayout>

修改MainActivity.java:

import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.GestureDetector;<strong>import android.view.GestureDetector.OnGestureListener;</strong>import android.view.Menu;import android.view.MotionEvent;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import android.widget.Toast;public class Show_3Activity extends Activity <strong>implements OnGestureListener</strong> {private GestureDetector detector;//定义手势识别监听器private ImageView Iv;//定义ImageView对象private Animation amUp,amDown,amLeft,amRight;//定义向上,向下,向左,向右的动画@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_show_3);setTitle("滑动测试");detector=new GestureDetector(this);//初始化手势识别监听器findById();initAnimation();//初始化对象的动画效果}private void initAnimation() {<strong>int screenWidth=getWindowManager().getDefaultDisplay().getWidth();//宽度int screenHeight=getWindowManager().getDefaultDisplay().getHeight();//高度</strong>//测试Log.e("screenWidth", screenWidth+"");Log.e("screenHeight", screenHeight+"");//向上的动画设定amUp=new TranslateAnimation(0,0,0,-screenHeight/2);amUp.setDuration(500);//动画开始到结束的执行时间amUp.setRepeatCount(1);//重复次数1amUp.setRepeatMode(Animation.REVERSE);//设置回播amDown=new TranslateAnimation(0, 0,0,screenHeight/2);//设置向下的动画设定amDown.setDuration(500);//动画开始到结束的额执行时间amDown.setRepeatCount(1);//设置重复次数amDown.setRepeatMode(Animation.REVERSE);//设置回播//设置向左的动画设定amLeft=new TranslateAnimation(0,-screenWidth/2,0,0);amLeft.setDuration(500);//动画开始到结束的执行时间amLeft.setRepeatCount(1);//重复次数amLeft.setRepeatMode(Animation.REVERSE);//设置回播reverse//设置向右的动画设定amRight=new TranslateAnimation(0,screenWidth/2,0,0);amRight.setDuration(500);//动画开始到结束的执行时间amRight.setRepeatCount(1);//重复次数amRight.setRepeatMode(Animation.REVERSE);//设置回播reverse}//当页面被触摸时自动回调,把拿到的事件给手势识别器进行处理public boolean <strong>onTouchEvent</strong>(MotionEvent event) {return this.detector.onTouchEvent(event);}private void findById() {Iv=(ImageView) findViewById(R.id.Iv);//获取控件Iv}//当页面被按下时自动回调@Overridepublic boolean onDown(MotionEvent e) {return false;}//当在页面中滑动时自动回调@Overridepublic boolean <strong>onFling</strong>(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {//如果得到滑动位移>100if(e1.getX()-e2.getX()>100){//判断用户是否向左滑动Iv.startAnimation(amLeft);Toast.makeText(Show_3Activity.this, "向左滑动", Toast.LENGTH_SHORT).show();return true;}else if(e1.getX()-e2.getX()<-100){//判断用户是否向右滑动Iv.startAnimation(amRight);Toast.makeText(Show_3Activity.this, "向右滑动", Toast.LENGTH_SHORT).show();return true;}else if(e1.getY()-e2.getY()>100){//判断是否向上滑动Iv.startAnimation(amUp);Toast.makeText(Show_3Activity.this, "向上滑动", Toast.LENGTH_SHORT).show();return true;}else if(e1.getY()-e2.getY()<-100){//判断是否向下Iv.startAnimation(amDown);Toast.makeText(Show_3Activity.this, "向下滑动", Toast.LENGTH_SHORT).show();return true;}return true;}//当界面长按时自动调用@Overridepublic void onLongPress(MotionEvent arg0) {}//界面滚动的时候自动调用@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {return false;}//当轻击界面的时候自动调用@Overridepublic void onShowPress(MotionEvent arg0) {Toast.makeText(Show_3Activity.this, "轻击界面显示", Toast.LENGTH_SHORT).show();}//当界面被按压的时候被调用@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}}



0 0
原创粉丝点击