当前位置: 移动技术网 > 移动技术>移动开发>Android > Android通过手势实现答题器翻页效果

Android通过手势实现答题器翻页效果

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了android答题器翻页功能,主要使用viewfilpper和gesturedetector来实现,供大家参考,具体内容如下

1.效果图

2.实现思路

把activity的touchevent事件交个gesturedetector来处理,然后使用viewfilpper使用动画控制多个组件的之间的切换效果。手势的一个api就不详细说了,大家如果不了解可以查一下。

3.实现的步骤

1)、构建手势检测器
2)、准备数据
3)、为viewfilpper添加子控件。
4)、初始化animation数组
5)、把activity的touchevent事件交个gesturedetector来处理
6)、实现 onfling方法

4.代码实现

4.1布局文件

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.lidong.demo.view.gesturefilpactivity">

  <viewflipper
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewflipper"/>
</linearlayout>

4.2 动画文件

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="500" />
</set>

left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:fromxdelta="0" android:toxdelta="-100%p"
        android:duration="500" />
</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="500" />
</set>

right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate android:fromxdelta="0" android:toxdelta="100%p"
        android:duration="500" />
</set>

4.3gesturefilpactivity的实现

package com.lidong.demo.view;

import android.os.bundle;
import android.view.gesturedetector;
import android.view.motionevent;
import android.view.view;
import android.view.animation.animation;
import android.view.animation.animationutils;
import android.widget.adapterview;
import android.widget.listview;
import android.widget.textview;
import android.widget.toast;
import android.widget.viewflipper;

import com.lidong.demo.appcomponent;
import com.lidong.demo.baseactivity;
import com.lidong.demo.r;
import com.lidong.demo.view.adapter.chinesemedicinereportadapter;
import com.lidong.demo.view.model.question;

import java.util.arraylist;
import java.util.list;

import butterknife.bind;
import butterknife.butterknife;
/**
*@类名 : gesturefilpactivity
*@描述 : 
*@时间 : 2016/5/3 16:11
*@作者: 李东
*@邮箱 : lidong@chni.com.cn
*@company: chni
*/
public class gesturefilpactivity extends baseactivity implements gesturedetector.ongesturelistener{

 @bind(r.id.viewflipper)
 viewflipper mviewflipper;
 //1.定义手势检测器对象
 gesturedetector mgesturedetector;
 //2.定义一个动画数组,用于为viewfilpper指定切换动画效果。
 animation[] animations = new animation[4];
 //3.定义手势两点之间的最小距离
 final int flip_distance = 50 ;

 list<question> mquestion = new arraylist<>();
 chinesemedicinereportadapter adapter;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_gesture_filp);
  butterknife.bind(this);
  setactivitytitle("答题器的实现");
  //1.构建手势检测器
  mgesturedetector = new gesturedetector(this,this);
  //2准备数据
  list<question> questions = initdata();
  mquestion.addall(questions);
  //3.为viewfilpper添加子控件。
  for (int i = 0;i<mquestion.size();i++){
   question question = mquestion.get(i);
   mviewflipper.addview(addquestionview(question));
  }
  //4.初始化animation数组
  animations[0] = animationutils.loadanimation(this,r.anim.left_in);
  animations[1] = animationutils.loadanimation(this,r.anim.left_out);
  animations[2] = animationutils.loadanimation(this,r.anim.right_in);
  animations[3] = animationutils.loadanimation(this,r.anim.right_out);

 }

 @override
 protected void setupactivitycomponent(appcomponent appcomponent) {

 }

 private view addquestionview(question question){
  view view = view.inflate(this, r.layout.activity_chnihealthreport, null);
  textview tes = (textview) view.findviewbyid(r.id.tv_question);
  listview listview = (listview) view.findviewbyid(r.id.lv_question_answer);
  adapter = new chinesemedicinereportadapter(this,question);
  listview.setadapter(adapter);
  tes.settext(question.getquestion());
  listview.setonitemclicklistener(new adapterview.onitemclicklistener() {
   @override
   public void onitemclick(adapterview<?> parent, view view, int position, long id) {
    toast.maketext(gesturefilpactivity.this,position+"",toast.length_short).show();
    toast.maketext(gesturefilpactivity.this,position+"",toast.length_short).show();
    if (mviewflipper.getdisplayedchild() == mquestion.size() - 1) {
     toast.maketext(gesturefilpactivity.this,"最后一个题",toast.length_short).show();
     mviewflipper.stopflipping();
     return;
    }else {
     mviewflipper.setinanimation(animations[0]);
     mviewflipper.setoutanimation(animations[1]);
     mviewflipper.shownext();
    }
   }
  });
  return view;
 }




 @override
 public boolean ondown(motionevent e) {
  return false;
 }

 @override
 public void onshowpress(motionevent e) {

 }

 @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) {

 }
  //重点实现在这里切换
 @override
 public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {
  if (e2.getx() - e1.getx()>flip_distance){
   if (mviewflipper.getdisplayedchild() == 0) {
    mviewflipper.stopflipping();
    toast.maketext(gesturefilpactivity.this,"第一个题",toast.length_short).show();
    return false;
   } else {
    mviewflipper.setinanimation(animations[2]);
    mviewflipper.setoutanimation(animations[3]);
    mviewflipper.showprevious();
    return true;
   }
  }else if (e1.getx() - e2.getx()>flip_distance){
   if (mviewflipper.getdisplayedchild() == mquestion.size() - 1) {
    toast.maketext(gesturefilpactivity.this,"最后一个题",toast.length_short).show();
    mviewflipper.stopflipping();
    return false;
   }else {
    mviewflipper.setinanimation(animations[0]);
    mviewflipper.setoutanimation(animations[1]);
    mviewflipper.shownext();
    return true;
   }
  }
  return false;
 }


 @override
 public boolean ontouchevent(motionevent event) {
  //将activity上的触发的事件交个gesturedetector处理
  return this.mgesturedetector.ontouchevent(event);
 }

 private list<question> initdata(){
  list<question> questions = new arraylist<>();
  question q1 = new question();
  q1.setquestion("1、\"红娘\"由来是出自下列哪部古典名剧:");
  list<question.answer> ma = new arraylist<>();
  question.answer a1 = new question.answer();
  a1.setanswermessage("a《琵琶记》");
  question.answer a2 = new question.answer();
  a2.setanswermessage("b《西厢记》");
  question.answer a3 = new question.answer();
  a3.setanswermessage("c《长生殿》");
  question.answer a4 = new question.answer();
  a4.setanswermessage("d《桃花扇》");
  ma.add(a1);
  ma.add(a2);
  ma.add(a3);
  ma.add(a4);
  q1.setanswer(ma);
  questions.add(q1);

  question q2 = new question();
  q2.setquestion("2.我国第一部有声影片是:");
  list<question.answer> mb = new arraylist<>();
  question.answer b1 = new question.answer();
  b1.setanswermessage("a《歌女红牡丹》");
  question.answer b2 = new question.answer();
  b2.setanswermessage("b《定军山》");
  question.answer b3 = new question.answer();
  b3.setanswermessage("c《林则徐》");
  question.answer b4 = new question.answer();
  b4.setanswermessage("d《玉人何处》");
  mb.add(b1);
  mb.add(b2);
  mb.add(b3);
  mb.add(b4);
  q2.setanswer(mb);
  questions.add(q2);
  question q3= new question();
  q3.setquestion("3.下列哪座山不属于我国四大佛山之一:( a)");
  list<question.answer> mc = new arraylist<>();
  question.answer c1 = new question.answer();
  c1.setanswermessage("a《歌女红牡丹》");
  question.answer c2 = new question.answer();
  c2.setanswermessage("b《定军山》");
  question.answer c3 = new question.answer();
  c3.setanswermessage("c《林则徐》");
  question.answer c4 = new question.answer();
  c4.setanswermessage("d《玉人何处》");
  mc.add(c1);
  mc.add(c2);
  mc.add(c3);
  mc.add(c4);
  q3.setanswer(mc);
  questions.add(q3);
  question q4 = new question();
  q4.setquestion("4.下面哪个是对“惊蛰”这个节气的正确描述?");
  list<question.answer> md = new arraylist<>();
  question.answer d1 = new question.answer();
  d1.setanswermessage("a《歌女红牡丹》");
  question.answer d2 = new question.answer();
  d2.setanswermessage("b《定军山》");
  question.answer d3 = new question.answer();
  d3.setanswermessage("c《林则徐》");
  question.answer d4 = new question.answer();
  d4.setanswermessage("d《玉人何处》");
  md.add(d1);
  md.add(d2);
  md.add(d3);
  md.add(d4);
  q4.setanswer(md);
  questions.add(q4);

  return questions;
 }
}

5.总结

1.构建手势检测器,2准备数据,3为viewfilpper添加子控件。4.初始化animation数组。5.把activity的touchevent事件交个gesturedetector来处理,6.实现onfling方法。

代码下载:android实现答题器翻页效果

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网