Android中使用ViewFlipper进行手势切换实例
程序员文章站
2022-04-30 23:03:34
本文实例讲述了android中使用viewflipper进行手势切换的方法,分享给大家供大家参考。具体实现步骤如下:
首先在layout的xml文件中定义一个viewfl...
本文实例讲述了android中使用viewflipper进行手势切换的方法,分享给大家供大家参考。具体实现步骤如下:
首先在layout的xml文件中定义一个viewflipper:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<viewflipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<textview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
/>
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<textview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00ff00"
/>
</linearlayout>
</viewflipper>
<viewflipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<textview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
/>
</linearlayout>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<textview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00ff00"
/>
</linearlayout>
</viewflipper>
声明一个新的gesturedetector,重写它的onfling()函数,在这个函数里面判断手势,这里为横向的拖动:
复制代码 代码如下:
public class mygesturedetector extends simpleongesturelistener
{
private static final int swipe_min_distance = 120;
private static final int swipe_max_off_path = 250;
private static final int swipe_threshold_velocity = 200;
@override
public boolean onfling(motionevent e1, motionevent e2,
float velocityx, float velocityy)
{
try
{
if (math.abs(e1.gety() - e2.gety()) > swipe_max_off_path)
{
return false;
}
if(e1.getx() - e2.getx() > swipe_min_distance &&
math.abs(velocityx) > swipe_threshold_velocity)
{
viewflipper.setinanimation(slideleftin);
viewflipper.setoutanimation(slideleftout);
viewflipper.shownext();
}
else
if (e2.getx() - e1.getx() > swipe_min_distance &&
math.abs(velocityx) > swipe_threshold_velocity)
{
viewflipper.setinanimation(sliderightin);
viewflipper.setoutanimation(sliderightout);
viewflipper.showprevious();
}
}
catch (exception e)
{
}
return false;
}
}
{
private static final int swipe_min_distance = 120;
private static final int swipe_max_off_path = 250;
private static final int swipe_threshold_velocity = 200;
@override
public boolean onfling(motionevent e1, motionevent e2,
float velocityx, float velocityy)
{
try
{
if (math.abs(e1.gety() - e2.gety()) > swipe_max_off_path)
{
return false;
}
if(e1.getx() - e2.getx() > swipe_min_distance &&
math.abs(velocityx) > swipe_threshold_velocity)
{
viewflipper.setinanimation(slideleftin);
viewflipper.setoutanimation(slideleftout);
viewflipper.shownext();
}
else
if (e2.getx() - e1.getx() > swipe_min_distance &&
math.abs(velocityx) > swipe_threshold_velocity)
{
viewflipper.setinanimation(sliderightin);
viewflipper.setoutanimation(sliderightout);
viewflipper.showprevious();
}
}
catch (exception e)
{
}
return false;
}
}
注意在activity中重写ontouchevent()函数:
复制代码 代码如下:
private viewflipper viewflipper;
private animation slideleftin;
private animation slideleftout;
private animation sliderightin;
private animation sliderightout;
private gesturedetector gesturedetector;
@override
public void oncreate(bundle savedinstancestate)
{
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
viewflipper = (viewflipper)findviewbyid(r.id.viewflipper);
slideleftin = animationutils.loadanimation(this, r.anim.slide_left_in);
slideleftout = animationutils.loadanimation(this, r.anim.slide_left_out);
sliderightin = animationutils.loadanimation(this, r.anim.slide_right_in);
sliderightout = animationutils.loadanimation(this, r.anim.slide_right_out);
gesturedetector = new gesturedetector(new mygesturedetector());
new view.ontouchlistener()
{
public boolean ontouch(view v, motionevent event)
{
if (gesturedetector.ontouchevent(event))
{
return true;
}
return false;
}
};
}
@override
public boolean ontouchevent(motionevent event)
{
if (gesturedetector.ontouchevent(event))
{
return true;
}
else
{
return false;
}
}
private animation slideleftin;
private animation slideleftout;
private animation sliderightin;
private animation sliderightout;
private gesturedetector gesturedetector;
@override
public void oncreate(bundle savedinstancestate)
{
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
viewflipper = (viewflipper)findviewbyid(r.id.viewflipper);
slideleftin = animationutils.loadanimation(this, r.anim.slide_left_in);
slideleftout = animationutils.loadanimation(this, r.anim.slide_left_out);
sliderightin = animationutils.loadanimation(this, r.anim.slide_right_in);
sliderightout = animationutils.loadanimation(this, r.anim.slide_right_out);
gesturedetector = new gesturedetector(new mygesturedetector());
new view.ontouchlistener()
{
public boolean ontouch(view v, motionevent event)
{
if (gesturedetector.ontouchevent(event))
{
return true;
}
return false;
}
};
}
@override
public boolean ontouchevent(motionevent event)
{
if (gesturedetector.ontouchevent(event))
{
return true;
}
else
{
return false;
}
}
最后,在res文件夹下新建一个anim文件夹,用来存放view之间切换的动画文件:
1.slide_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"
/>
<alpha
android:fromalpha="0.0"
android:toalpha="1.0"
android:duration="500"
/>
</set>
<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.slide_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"
/>
<alpha
android:fromalpha="1.0"
android:toalpha="0.0"
android:duration="500"
/>
</set>
<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>
3.slide_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"
/>
<alpha
android:fromalpha="0.0"
android:toalpha="1.0"
android:duration="500"
/>
</set>
<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>
4.slide_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"
/>
<alpha
android:fromalpha="1.0"
android:toalpha="0.0"
android:duration="500"
/>
</set>
<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>
希望本文所述对大家的android程序设计有所帮助