版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android使用ViewFlipper做页面切换Android系统自带有一个多页面管理的控件:ViewFlipper.它可以简单实现子页面的切换,,,它只需使用addView方法添加几个View,每个View对应的是一个页面,即可完成对于多页面的管理,,,
在android上实现手势的识别也比较简单,可以实现OnTouchListener和OnGuestureListener接口,然后在OnTouch函数中注册GestureDetector来判别手势动作,
参考一位大牛的文章:/blog/572886
GestureDetector.OnGestureListener:用来通知普通的手势事件,该接口有如下六个回调函数:
1.
onDown(MotionEvente):down事件;
2.
onSingleTapUp(MotionEvente):一次点击up事件;
3.
onShowPress(MotionEvente):down事件发生而move或则up还没发生前触发该事件;
4.
onLongPress(MotionEvente):长按事件;
5.
onFling(MotionEvente1,MotionEvente2,floatvelocityX,floatvelocityY):滑动手势事件;
6.
onScroll(MotionEvente1,MotionEvente2,floatdistanceX,floatdistanceY):在屏幕上拖动事件。
主要判断是在onFling()函数里面,e1表示开始按下去的位置信息,e2表示抬起时的位置信息,因此可以通过它们在x轴上面的距离差来是左滑还是右滑。。。[java]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?public
boolean
onFling(MotionEvent
e1,
MotionEvent
e2,
float
velocityX,
float
velocityY)
{
//
TODO
Auto-generated
method
stub
if
(e2.getX()-e1.getX()
>
100)
{
//
fling
right
showNextView();
}
else
if
(e1.getX()
-
e2.getX()
>
100)
{
//
fling
left
showPreviousView();
}
return
false;
}
资源文件:[xhtml]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnPrev"
android:text="Previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnNext"
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<ViewFlipper
android:id="@+id/vfFlingTest"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ViewFlipper>
</LinearLayout>
代码:[java]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?package
com.sf.learn;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.GestureDetector;
import
android.view.KeyEvent;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.GestureDetector.OnGestureListener;
import
android.view.View.OnClickListener;
import
android.view.View.OnTouchListener;
import
android.view.ViewGroup.LayoutParams;
import
android.view.animation.AnimationUtils;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.ImageView;
import
android.widget.TextView;
import
android.widget.ViewFlipper;
public
class
FlingTest
extends
Activity
implements
OnTouchListener,
OnGestureListener{
private
Button
btnPrev;
private
Button
btnNext;
private
ViewFlipper
vfFlingTest;
private
TextView
tvFlipper;
private
EditText
etFlipper;
private
ImageView
ivFlipper;
private
GestureDetector
mGestureDetector;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
//
TODO
Auto-generated
method
stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fling_test);
btnPrev
=
(Button)findViewById(R.id.btnPrev);
btnNext
=
(Button)findViewById(R.id.btnNext);
vfFlingTest
=
(ViewFlipper)findViewById(R.id.vfFlingTest);
initViews();
vfFlingTest.addView(tvFlipper);
vfFlingTest.addView(etFlipper);
vfFlingTest.addView(ivFlipper);
vfFlingTest.setOnTouchListener(this);
vfFlingTest.setLongClickable(true);
mGestureDetector
=
new
GestureDetector(this);
btnPrev.setOnClickListener(new
OnClickListener()
{
public
void
onClick(View
v)
{
//
TODO
Auto-generated
method
stub
showPreviousView();
}
});
btnNext.setOnClickListener(new
OnClickListener()
{
public
void
onClick(View
v)
{
//
TODO
Auto-generated
method
stub
showNextView();
}
});
}
public
void
showPreviousView()
{
vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(
this,
R.anim.slide_right_in));
vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(
this,
R.anim.slide_left_out));
vfFlingTest.showPrevious();
}
public
void
showNextView()
{
vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(
this,
R.anim.slide_left_in));
vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(
this,
R.anim.slide_right_out));
vfFlingTest.showNext();
}
private
void
initViews()
{
tvFlipper
=
new
TextView(this);
tvFlipper.setText("this
is
a
text
view!");
etFlipper
=
new
EditText(this);
etFlipper.setText("this
is
a
text
view!");
ivFlipper
=
new
ImageView(this);
ivFlipper.setLayoutParams(new
LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
ivFlipper.setImageResource(R.drawable.pic1);
}
@Override
protected
void
onDestroy()
{
//
TODO
Auto-generated
method
stub
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
@Override
public
boolean
onKeyDown(int
keyCode,
KeyEvent
event)
{
//
TODO
Auto-generated
method
stub
if
(keyCode
==
KeyEvent.KEYCODE_BACK)
{
finish();
return
true;
}
return
super.onKeyDown(keyCode,
event);
}
public
boolean
onTouch(View
view,
MotionEvent
event)
{
//
TODO
Auto-generated
method
stub
return
mGestureDetector.onTouchEvent(event);
}
public
boolean
onDown(MotionEvent
arg0)
{
//
TODO
Auto-generated
method
stub
return
false;
}
public
boolean
onFling(MotionEvent
e1,
MotionEvent
e2,
float
velocityX,
float
velocityY)
{
//
TODO
Auto-generated
method
stub
if
(e2.getX()-e1.getX()
>
100)
{
//
fling
right
showNextView();
}
else
if
(e1.getX()
-
e2.getX()
>
100)
{
//
fling
left
showPreviousView();
}
return
false;
}
public
void
onLongPress(MotionEvent
e)
{
//
TODO
Auto-generated
method
stub
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护理实践中的伦理困境
- 护理教学与健康管理结合
- 2025年社区环保宣传活动 绿色行动我参与
- 中国战略新通道:激活南太平洋岛屿链的“低空-海洋-旅娱”经济走廊
- 在线定制化家纺趋势
- 地下水污染治理-第1篇
- 基本题库复合题库及答案
- 2026 年中职酒店管理(酒店管理常识)试题及答案
- 兽医题目及答案
- 办公设备采购合同协议2025
- 乡镇卫生院检验科检验质量控制管理制度
- 【个案工作介入青少年厌学问题研究12000字(论文)】
- 村级事务监督工作报告
- T/TAC 10-2024机器翻译伦理要求
- 兄妹合伙买房协议书
- 家庭农场项目可行性报告
- 施工升降机防护方案
- 温室大棚可行性报告修改版
- JISG3141-2017冷轧钢板及钢带
- 瑞加诺生注射液-药品临床应用解读
- 2025中医体重管理临床指南
评论
0/150
提交评论