zoukankan      html  css  js  c++  java
  • ViewFlipper实现翻页效果

    package org.hjw.flipper;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.drawable.AnimationDrawable;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.ImageView;
    import android.widget.ViewFlipper;
    
    public class Flipper extends Activity implements OnGestureListener {
    
        ViewFlipper flipper;
        GestureDetector detector;
        Animation[] animations = new Animation[4];
        final int FLIP_DISTANCE = 50;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_flipper);
    
            detector = new GestureDetector(this, this);
            flipper = (ViewFlipper) findViewById(R.id.flipper);
            flipper.addView(addImageView(R.drawable.cat));
            flipper.addView(addImageView(R.drawable.crazy));
            flipper.addView(addImageView(R.drawable.gz));
    
            animations[0] = AnimationUtils.loadAnimation(this, R.anim.left_in);
            animations[1] = AnimationUtils.loadAnimation(this, R.anim.left_out);
            animations[2] = AnimationUtils.loadAnimation(this, R.anim.right_in);
            animations[3] = AnimationUtils.loadAnimation(this, R.anim.right_out);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.flipper, menu);
            return true;
        }
    
        private View addImageView(int resid) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(resid);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
    
            return imageView;
        }
    
        public boolean onTouchEvent(MotionEvent event) {
            return detector.onTouchEvent(event);
    
        }
    
        @Override
        public boolean onDown(MotionEvent e) {
    
            return false;
        }
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            if (e1.getX() - e2.getX() > FLIP_DISTANCE) {
                flipper.setInAnimation(animations[0]);
                flipper.setOutAnimation(animations[1]);
                flipper.showPrevious();
                return true;
            } else if (e1.getX() - e2.getX() <FLIP_DISTANCE) {
                flipper.setInAnimation(animations[2]);
                flipper.setOutAnimation(animations[2]);
                flipper.showNext();
                return true;
            }
    
            return false;
        }
    
        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
    
    }

    ps.1.实现OnGestureListener接口,将OnTouch事件交给GestureDetector处理

         2.判断滑动事件的关键就是,判断第一个触点事件与第二个触点事件的x距离是否大于或小于距离事件,从而判断是向右滑还是向左滑

  • 相关阅读:
    [Java5新特性]可变参数
    [Java5新特性]自动装箱/拆箱
    CentOS6.5下安装apache2.2和PHP 5.5.28
    VMware Workstation(虚拟机)v10.0.1 简体中文破解版
    [Android]通过js方法回调部分native报错 Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'
    Android WebView与JavaScript交互操作(Demo)
    【CSS3 入门教程系列】CSS3 Media Queries 实现响应式设计
    Bootstrap 3支持IE 8遇到的一个小问题
    php文件大小单位转换GB MB KB
    2个比较经典的PHP加密解密函数分享
  • 原文地址:https://www.cnblogs.com/dennisac/p/3178280.html
Copyright © 2011-2022 走看看