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

Android通过手势实现答题器翻页效果,第1张

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

本文实例为大家分享了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,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,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软件编程有所帮助。

总结

以上是内存溢出为你收集整理的Android通过手势实现答题器翻页效果全部内容,希望文章能够帮你解决Android通过手势实现答题器翻页效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149330.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存