Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例
程序员文章站
2023-12-18 19:24:52
本文实例讲述了android使用viewflipper和gestruedetector共同实现滑屏效果。分享给大家供大家参考,具体如下:
关于gesturedetecto...
本文实例讲述了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, motionevent e2, 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, animation.relative_to_parent, 0.0f, animation.relative_to_parent, 0.0f); infromright.setduration(500); infromright.setinterpolator(new accelerateinterpolator()); return infromright; } protected animation outtoleftanimation() { animation outtoleft = new translateanimation( animation.relative_to_parent, 0.0f, animation.relative_to_parent, -1.0f, animation.relative_to_parent, 0.0f, animation.relative_to_parent, 0.0f); outtoleft.setduration(500); outtoleft.setinterpolator(new accelerateinterpolator()); return outtoleft; } protected animation infromleftanimation() { animation infromleft = new translateanimation( animation.relative_to_parent, -1.0f, animation.relative_to_parent, 0.0f, animation.relative_to_parent, 0.0f, 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, animation.relative_to_parent, +1.0f, animation.relative_to_parent, 0.0f, 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程序设计有所帮助。