热门IT资讯网

Android的ViewFlipper与GestureDet

发表于:2024-11-21 作者:热门IT资讯网编辑
编辑最后更新 2024年11月21日,用ViewFlipper与GestureDetector来实现图片的左右滑动效果。首先在布局xml中添加ViewFlipper控件: 在Activity中实现GestureDetector的O

用ViewFlipper与GestureDetector来实现图片的左右滑动效果。

首先在布局xml中添加ViewFlipper控件:

     

在Activity中实现GestureDetector的OnGestureListener监听:

mGesture = new GestureDetector(this, this);
public boolean onDown(MotionEvent e) {                return false;        }        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                        float velocityY) {                return false;        }        public void onLongPress(MotionEvent e) {        }        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,                        float distanceY) {                return false;        }        public void onShowPress(MotionEvent e) {        }        public boolean onSingleTapUp(MotionEvent e) {                return false;        }

为ViewFilpper加入View,如图片、文字等:

mFilpper = (ViewFlipper) findViewById(R.id.flipper);                mFilpper.addView(addImageView(R.drawable.chrysanthemum));                mFilpper.addView(addImageView(R.drawable.desert));                mFilpper.addView(addImageView(R.drawable.ic_launcher));                mFilpper.addView(addImageView(R.drawable.koala));                mFilpper.addView(addImageView(R.drawable.penguins));                mFilpper.addView(addImageView(R.drawable.lighthouse));                mFilpper.addView(addText("你好"));
public ImageView addImageView(int resId) {                ImageView p_w_picpath = new ImageView(this);                p_w_picpath.setImageResource(resId);                return p_w_picpath;        }        private View addText(String string) {                TextView textView = new TextView(this);                textView.setText(string);                return textView;        }

在onFling()中编写滑动效果:

@Override        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                        float velocityY) {                Log.e("onFling", "onFling");                if (e1.getX() - e2.getX() > 120) {                        mFilpper.setInAnimation(AnimationUtils.loadAnimation(this,                                        R.anim.left_in));                        mFilpper.setOutAnimation(AnimationUtils.loadAnimation(this,                                        R.anim.left_out));                        mFilpper.showPrevious();                } else if (e2.getX() - e1.getX() > 120) {                        mFilpper.setInAnimation(AnimationUtils.loadAnimation(this,                                        R.anim.rigth_in));                        mFilpper.setOutAnimation(AnimationUtils.loadAnimation(this,                                        R.anim.right_out));                        mFilpper.showNext();                }                return false;        }

其中的left_in.xml如下:

        

left_out.xml:

        

right_in.xml:

        

right_out.xml:

        

最后还要重写onTouchEvent()方法在其中调用GestureDetector的onTouchEvent()方法:

@Override        public boolean onTouchEvent(MotionEvent event) {                Log.e("onTouchEvent", "onTouchEvent");                return mGesture.onTouchEvent(event);        }
0