屏幕切换指的是在同一个activity内屏幕见的切换,最长见的情况就是在一个framelayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。
通过查看ophone api文档可以发现,有个android.widget.viewanimator类继承至framelayout,viewanimator类的作用是为framelayout里面的view切换提供动画效果。该类有如下几个和动画相关的函数:
l setinanimation:设置view进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.animation;一个接受两个参数,类型为context和int,分别为context对象和定义animation的resourceid。
setoutanimation: 设置view退出屏幕时候使用的动画,参数setinanimation函数一样。
shownext: 调用该函数来显示framelayout里面的下一个view。
showprevious: 调用该函数来显示framelayout里面的上一个view。
一般不直接使用viewanimator而是使用它的两个子类viewflipper和viewswitcher。viewflipper可以用来指定framelayout内多个view之间的切换效果,可以一次指定也可以每次切换的时候都指定单独的效果。该类额外提供了如下几个函数:
isflipping: 用来判断view切换是否正在进行
setfilpinterval:设置view之间切换的时间间隔
startflipping:使用上面设置的时间间隔来开始切换所有的view,切换会循环进行
stopflipping: 停止view切换
viewswitcher 顾名思义switcher特指在两个view之间切换。可以通过该类指定一个viewswitcher.viewfactory 工程类来创建这两个view。该类也具有两个子类imageswitcher、textswitcher分别用于图片和文本切换。
在教程中通过示例介绍viewflipper 的使用,其他的使用方式是类似的。详细信息可以参考文档:
http://androidappdocs-staging.appspot.com/reference/android/widget/viewanimator.html
viewflipper示例
记住,viewflipper是继承至framelayout的,所以它是一个layout里面可以放置多个view。在示例中定义一个viewflipper,里面包含三个viewgroup作为示例的三个屏幕,每个viewgroup中包含一个按钮和一张图片,点击按钮则显示下一个屏幕。代码如下(res\layout\main.xml):
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<viewflipper android:id="@+id/details"
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"
>
<linearlayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<button
android:text="next"
android:id="@+id/button_next1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</button>
<imageview
android:id="@+id/image1"
android:src="@drawable/dell1"
android:layout_width="fill_parent"
</imageview>
</linearlayout>
android:id="@+id/button_next2"
android:id="@+id/image2"
android:src="@drawable/lg"
android:id="@+id/button_next3"
android:id="@+id/image3"
android:src="@drawable/lenovo"
</viewflipper>
</linearlayout>
很简单,在layout定义中指定动画的相关属性就可以了,通过persistentdrawingcache指定缓存策略;flipinterval指定每个view动画之间的时间间隔;inanimation和outanimation分别指定view进出使用的动画效果。动画效果定义如下:
res\anim\push_left_in.xml
<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>
res\anim\push_left_out.xml
android:fromxdelta="0"
android:toxdelta="-100%p"
android:fromalpha="1.0"
android:toalpha="0.0"
</set>
activity代码如下(src\cc\c\testactivity.java):
public class testactivity extends activity {
private viewflipper mviewflipper;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
button buttonnext1 = (button) findviewbyid(r.id.button_next1);
mviewflipper = (viewflipper) findviewbyid(r.id.flipper);
buttonnext1.setonclicklistener(new view.onclicklistener() {
public void onclick(view view) {
//在layout中定义的属性,也可以在代码中指定
// mviewflipper.setinanimation(getapplicationcontext(), r.anim.push_left_in);
// mviewflipper.setoutanimation(getapplicationcontext(), r.anim.push_left_out);
// mviewflipper.setpersistentdrawingcache(viewgroup.persistent_all_caches);
// mviewflipper.setflipinterval(1000);
mviewflipper.shownext();
//调用下面的函数将会循环显示mviewflipper内的所有view。
// mviewflipper.startflipping();
}
});
button buttonnext2 = (button) findviewbyid(r.id.button_next2);
buttonnext2.setonclicklistener(new view.onclicklistener() {
});
button buttonnext3 = (button) findviewbyid(r.id.button_next3);
buttonnext3.setonclicklistener(new view.onclicklistener() {
}
}
通过手势移动屏幕
上面是通过屏幕上的按钮来在屏幕间切换的,这看起来多少有点不符合ophone的风格,如果要是能通过手势的左右滑动来实现屏幕的切换就比较优雅了。
通过android.view.gesturedetector类可以检测各种手势事件,该类有两个回调接口分别用来通知具体的事件:
gesturedetector.ondoubletaplistener:用来通知doubletap事件,类似于鼠标的双击事件,该接口有如下三个回调函数:
1. ondoubletap(motionevent e):通知doubletap手势,
2. ondoubletapevent(motionevent e):通知doubletap手势中的事件,包含down、up和move事件(这里指的是在双击之间发生的事件,例如在同一个地方双击会产生doubletap手势,而在doubletap手势里面还会发生down和up事件,这两个事件由该函数通知);
3. onsingletapconfirmed(motionevent e):用来判定该次点击是singletap而不是doubletap,如果连续点击两次就是doubletap手势,如果只点击一次,ophone系统等待一段时间后没有收到第二次点击则判定该次点击为singletap而不是doubletap,然后触发singletapconfirmed事件。
gesturedetector.ongesturelistener:用来通知普通的手势事件,该接口有如下六个回调函数:
1. ondown(motionevent e):down事件;
2. onsingletapup(motionevent e):一次点击up事件;
3. onshowpress(motionevent e):down事件发生而move或则up还没发生前触发该事件;
4. onlongpress(motionevent e):长按事件;
5. onfling(motionevent e1, motionevent e2, float velocityx, float velocityy):滑动手势事件;
6. onscroll(motionevent e1, motionevent e2, float distancex, float distancey):在屏幕上拖动事件。
这些事件有些定义的不太容易理解,在示例项目中实现了所有的回调函数,在每个函数中输出相关的日志,对这些事件不理解的可以运行项目,通过不同的操作来触发事件,然后观看logcat输出日志可有助于对这些事件的理解。
在上述事件中,如果在程序中处理的该事件就返回true否则返回false,在gesturedetector中也定义了一个simpleongesturelistener类,这是个助手类,实现了上述的所有函数并且都返回false。如果在项目中只需要监听某个事件继承这个类可以少些几个空回调函数。
要走上面的程序中添加滑动手势来实现屏幕切换的话,首先需要定义一个gesturedetector:
private gesturedetector mgesturedetector;
并在oncreate函数中初始化:
mgesturedetector = new gesturedetector(this);
参数是ongesturelistener,然后让testactivity实现 ongesturelistener 和ondoubletaplistener接口:
class testactivity extends activity implements ongesturelistener , ondoubletaplistener
然后在onfling函数中实现切换屏幕的功能:
public boolean onfling(motionevent e1, motionevent e2, float velocityx,
float velocityy) {
log.d(tag, "...onfling...");
if(e1.getx() > e2.getx()) {//move to left
mviewflipper.shownext();
}else if(e1.getx() < e2.getx()) {
mviewflipper.setinanimation(getapplicationcontext(), r.anim.push_right_in);
mviewflipper.setoutanimation(getapplicationcontext(), r.anim.push_right_out);
mviewflipper.showprevious();
mviewflipper.setinanimation(getapplicationcontext(), r.anim.push_left_in);
mviewflipper.setoutanimation(getapplicationcontext(), r.anim.push_left_out);
}else {
return false;
}
return true;
这里实现的功能是从右往左滑动则切换到上一个view,从左往右滑动则切换到下一个view,并且使用不同的in、out 动画使切换效果看起来统一一些。
然后在ondoubletap中实现双击自动切换的效果,再次双击则停止:
public boolean ondoubletap(motionevent e) {
log.d(tag, "...ondoubletap...");
if(mviewflipper.isflipping()) {
mviewflipper.stopflipping();
mviewflipper.startflipping();
到这里手势代码就完成了,现在可以通过左右滑动切换view并且双击可以自动切换view。细心的读者这里可能会发现一个问题,上面在创建mgesturedetector 的时候使用的是如下代码:
这里的参数为ongesturelistener,而且gesturedetector有个函数setondoubletaplistener来设置ondoubletaplistener,在上面的代码中并没有设置ondoubletaplistener,那么ondoubletap事件是如何调用的呢?这里的玄机就要去探探 gesturedetector(ongesturelistener l)这个构造函数的源代码了:
public gesturedetector(ongesturelistener listener) {
this(null, listener, null);
}
调用了另外一个构造函数:
public gesturedetector(context context, ongesturelistener listener, handler handler) {
if (handler != null) {
mhandler = new gesturehandler(handler);
} else {
mhandler = new gesturehandler();
mlistener = listener;
if (listener instanceof ondoubletaplistener) {
setondoubletaplistener((ondoubletaplistener) listener);
init(context);
注意到listener instanceof ondoubletaplistener没有?现在明白了吧。