本文实例讲述了AndroID使用VIEwFlipper和GestrueDetector共同实现滑屏效果。分享给大家供大家参考,具体如下:
关于GestureDetector的相关知识,前面已经介绍过了,不懂的大家可以去了解一下。
1.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:background="@drawable/bg" androID:orIEntation="vertical" > <VIEwFlipper androID:ID="@+ID/vIEwFlipper1" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:persistentDrawingCache="animation" androID:flipInterval="1000" androID:inAnimation="@anim/push_left_in" androID:outAnimation="@anim/push_left_out" > <include androID:ID="@+ID/lay1" layout="@layout/layout1"/> <include androID:ID="@+ID/lay2" layout="@layout/layout2"/> </VIEwFlipper></linearLayout>
注:layout1和layout2 布局很简单,就是有一个textvIEw和一个button,就不在这里写出了。其中包含了两个特效xml文件,放在res/anim下
1.push_left_in
<?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"/> <Alpha androID:fromAlpha="0.0" androID:toAlpha="1.0" androID:duration="500" /></set>
2 push_left_out
<?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"/> <Alpha androID:fromAlpha="1.0" androID:toAlpha="0.0" androID:duration="500" /></set>
主类:
package com.wj.gesture;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.GestureDetector;import androID.vIEw.GestureDetector.OnDoubleTapListener;import androID.vIEw.GestureDetector.OnGestureListener;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEw.OntouchListener;import androID.vIEw.animation.AccelerateInterpolator;import androID.vIEw.animation.Animation;import androID.vIEw.animation.TranslateAnimation;import androID.Widget.button;import androID.Widget.TextVIEw;import androID.Widget.Toast;import androID.Widget.VIEwFlipper;public class GestureDetectorTest extends Activity implements OnClickListener,OntouchListener,OnGestureListener,OnDoubleTapListener{ GestureDetector gestureDetector; private button next = null,up = null; private VIEwFlipper flipper = null; private static final int FliNG_MIN_disTANCE = 100; private static final int FliNG_MIN_VELociTY = 200; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); initvIEw(); setListener(); } private voID initvIEw(){ gestureDetector = new GestureDetector(this); next = (button)this.findVIEwByID(R.ID.button1); up = (button)this.findVIEwByID(R.ID.button2); next.setonClickListener(this); up.setonClickListener(this); flipper = (VIEwFlipper)this.findVIEwByID(R.ID.vIEwFlipper1); flipper.setLongClickable(true); } private voID setListener(){ flipper.setontouchListener(this); } @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { //Toast.makeText(this,"ontouch",1000).show(); return gestureDetector.ontouchEvent(event); } @OverrIDe public boolean onDown(MotionEvent e) { // Todo auto-generated method stub return false; } @OverrIDe public voID onShowPress(MotionEvent e) { // Todo auto-generated method stub } @OverrIDe public boolean onSingleTapUp(MotionEvent e) { // Todo auto-generated method stub return false; } @OverrIDe public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) { return false; } @OverrIDe public voID onLongPress(MotionEvent e) { // Todo auto-generated method stub } @OverrIDe public boolean onFling(MotionEvent e1,float veLocityX,float veLocityY) { if (e1.getX()-e2.getX()>FliNG_MIN_disTANCE&&Math.abs(veLocityX)>FliNG_MIN_VELociTY) { flipper.setInAnimation(inFromrightAnimation()); flipper.setoutAnimation(outToleftAnimation()); flipper.showNext(); }else if (e2.getX()-e1.getX()>FliNG_MIN_disTANCE&&Math.abs(veLocityX) > FliNG_MIN_VELociTY) { flipper.setInAnimation(inFromleftAnimation()); flipper.setoutAnimation(outToRightAnimation()); flipper.showPrevIoUs(); } return false; } protected Animation inFromrightAnimation() { Animation inFromright = new TranslateAnimation( Animation.relative_TO_PARENT,+1.0f,Animation.relative_TO_PARENT,0.0f,0.0f); inFromright.setDuration(500); inFromright.setInterpolator(new AccelerateInterpolator()); return inFromright; } protected Animation outToleftAnimation() { Animation outtoleft = new TranslateAnimation( Animation.relative_TO_PARENT,-1.0f,0.0f); outtoleft.setDuration(500); outtoleft.setInterpolator(new AccelerateInterpolator()); return outtoleft; } protected Animation inFromleftAnimation() { Animation inFromleft = new TranslateAnimation( Animation.relative_TO_PARENT,0.0f); inFromleft.setDuration(500); inFromleft.setInterpolator(new AccelerateInterpolator()); return inFromleft; } protected Animation outToRightAnimation() { Animation outtoRight = new TranslateAnimation( Animation.relative_TO_PARENT,0.0f); outtoRight.setDuration(500); outtoRight.setInterpolator(new AccelerateInterpolator()); return outtoRight; } @OverrIDe public boolean onSingleTapConfirmed(MotionEvent e) { // Todo auto-generated method stub return false; } @OverrIDe public boolean onDoubleTap(MotionEvent e) { // Todo auto-generated method stub return false; } @OverrIDe public boolean onDoubleTapEvent(MotionEvent e) { // Todo auto-generated method stub return false; } @OverrIDe public voID onClick(VIEw v) { if (v==next) { flipper.showNext(); }else{ flipper.showPrevIoUs(); } }}
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android多媒体 *** 作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例全部内容,希望文章能够帮你解决Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)