欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现手势滑动和简单动画效果

程序员文章站 2023-12-05 22:28:16
一、手势滑动 1.activity都具有响应触摸事件,也就是说只要触摸activity,他都会回调一个ontouchevent()方法。但是在这个方法里无法处理事件,需要...

一、手势滑动

1.activity都具有响应触摸事件,也就是说只要触摸activity,他都会回调一个ontouchevent()方法。但是在这个方法里无法处理事件,需要配合使用手势识别器(gesturedetector)中的方法ontouchevent对事件(event)进行分析处理,我们只需要重写这个方法中的操作来达到我们的需求。

   /**
   * activity被触摸后,会回调此方法ontouchevent,并回传一个event对象
   * event对象封装了触摸时的动作信息,包括x、y坐标等等信息 
   */
   @override
  public boolean ontouchevent(motionevent event) {
    // todo auto-generated method stub
    boolean ontouchevent = gesturedetector.ontouchevent(event);
    //如果这个方法消费了这个这个event事件,就返回true,否则false。
    return super.ontouchevent(event);
  }

2.我们需要根据滑动手势做界面跳转,gesturedetector封装了可以监听不同手势的方法。这个方法在哪个activity中使用,这个activity就能监听这个activity接收的动作,在这些方法里可以重写需要的动作。

  gesturedetector gesturedetector = new gesturedetector(this, new ongesturelistener(){
      @override
      public boolean ondown(motionevent e) {
        // 按下
        return false;
      }
      @override
      public void onshowpress(motionevent e) {
        // todo auto-generated method stub       
      }
      @override
      public boolean onsingletapup(motionevent e) {
        // 点击
        return false;
      }
      @override
      public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) {
        // 滑动
        return false;
      }
      @override
      public void onlongpress(motionevent e) {
        // 长按        
      }
      /**
      *滑动, e1为起点坐标,e2为终点坐标
      *在这里消费掉event事件,重写其中的方法以达到需求。
      /
      @override
      public boolean onfling(motionevent e1, motionevent e2,float velocityx, float velocityy) {
       if(e1.getrawx()-e2.getrawx()>200){
          flingnextpage(); //向左滑(与滑动动画配合效果较好)
        }else if(e2.getrawx()-e1.getrawx()>200){
          flingpreviouspage();//向右滑
          }
        if(math.abs(e1.getrawy()-e2.getrawy())>200){
        system.out.println("不能斜着滑动");
        return true;//true表示我们消费了这个触摸事件
          }
        if(math.abs(velocityx)<150 ||math.abs(velocityy)<100){
        system.out.println("滑动的太慢了,请滑快点");
        return true;
          }        
      return true;
      }      
    });

注:页面坐标

Android实现手势滑动和简单动画效果

二、简单动画

目的:用xml文件写一个activity跳转页面的过渡动画。

1. 在res目录下新建anim文件夹,创建两个xml文件并编辑动画动作,一个是进入动作,另一个退出动作。

Android实现手势滑动和简单动画效果

(1)在anim_next_in.xml中写好进入动作。

<?xml version="1.0" encoding="utf-8"?>
<translate
  android:fromxdelta="100%p" <!—当前界面的右侧界面,从右侧滑入当前界面-->
  android:toxdelta="0"
  android:duration="100" <!--移动时间--> 
  xmlns:android="http://schemas.android.com/apk/res/android">
</translate>

(2)在anim_next_out.xml中写好退出动作。

<?xml version="1.0" encoding="utf-8"?>
<translate
  android:fromxdelta="0" <!—当前界面向左滑出-->
  android:toxdelta="-100%p"
  android:duration="100"
   xmlns:android="http://schemas.android.com/apk/res/android">
</translate>

2.调用activity的overridependingtransition方法,来实现平移动画。

/**
* overridependingtransition(int enteranim, int exitanim)
* enteranim为进入当前界面的动画资源id, exitanim为退出当前界面的动画资源id
/
@override
public void flingnextpage() {
  // todo auto-generated method stub
   startactivity(new intent(this, newactivity.class));
   overridependingtransition(r.anim.anim_next_in, r.anim.anim_next_out);
   finish();//关闭当前界面
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。