Android使用ViewFlipper做页面切换,与手势滑动切换的使用
来源:互联网 发布:遥感卫星数据 编辑:程序博客网 时间:2024/06/06 03:37
它可以简单实现子页面的切换,,,
它只需使用addView方法添加几个View,每个View对应的是一个页面,即可完成对于多页面的管理,,,
在android上实现手势的识别也比较简单,可以实现OnTouchListener和OnGuestureListener接口,
然后在OnTouch函数中注册GestureDetector来判别手势动作,
参考一位大牛的文章:
http://wang-peng1.iteye.com/blog/572886
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):在屏幕上拖动事件。
主要判断是在onFling()函数里面,e1表示开始按下去的位置信息,e2表示抬起时的位置信息,因此可以通过它们在x轴上面 的距离差来是左滑还是右滑。。。
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; }
资源文件:
<?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"> <LinearLayout xmlns:android="http://schemas.android.com/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>
代码:
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.right_in)); vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation( this, R.anim.left_out)); vfFlingTest.showPrevious(); } public void showNextView() { vfFlingTest.setInAnimation(AnimationUtils.loadAnimation( this, R.anim.left_in)); vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation( this, R.anim.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 } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } }
还有就是一个页面切换的简单动画效果(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" /> </set>
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" /> </set>
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" /> </se
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" /> </set>
- Android使用ViewFlipper做页面切换,与手势滑动切换的使用。
- Android使用ViewFlipper做页面切换,与手势滑动切换的使用
- Android使用ViewFlipper做页面切换,与手势滑动切换的使用
- Android程序:使用ViewFlipper实现屏幕切换动画及手势滑动切换功能
- Android 滑动切换页面-ViewFlipper
- 使用ViewFlipper实现图片自动切换,并支持手势滑动切换
- Android中的手势切换——使用ViewFlipper
- Android中的手势切换——使用ViewFlipper
- Android ViewFlipper的使用 实现页面滑动
- ViewFlipper效果-------滑动切换页面
- ViewFlipper结合手势OnGestureListener制作的滑动切换效果
- ViewFlipper结合手势OnGestureListener制作的滑动切换效果
- ViewFlipper的使用---屏幕切换
- Android 滑动切换页面 以及屏幕手势
- Android 滑动切换页面 以及屏幕手势
- Android 滑动切换页面 以及屏幕手势
- Android 滑动切换页面 以及屏幕手势
- Android 滑动切换页面 以及屏幕手势
- Android 的获取当前时间 的各种情况(学习)
- 一分钟破解WPA/WPA2
- 敏捷测试的思考和新发展
- Lotus 错误 "Could not create field %1"
- Quartz.NET一个开源的作业调度框架
- Android使用ViewFlipper做页面切换,与手势滑动切换的使用
- 宏定义和内联函数的学习
- linux下各个命令的学习(1)
- 30分钟教你学会正则表达式
- C# 中的委托和事件
- Oracle10g的Flashback之Flashback Transaction Query
- matl
- 连接字串中Database Mirroring的Failover Partner设置的说明
- .xls .txt文件生成下载