• ViewFlipper实现滑动换图效果


    bangzhu.xml布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
          <ViewFlipper 
         android:id="@+id/newHandHelpViewFlipper"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" /> 
    
    </RelativeLayout>

    anim文件夹下的特效文件  push_left_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- 画面转换位置移动动画效果 
                  开始向左动画
         fromXDelta : 动画开始时 X坐标位置
         toXDelta :动画结束时 X坐标位置
         duration :动画持续时间-->
        <translate 
            android:fromXDelta="100%p" 
            android:toXDelta="0"
            android:duration="500" />
        <!-- 渐变透明的动画效果 -->
        <alpha 
            android:fromAlpha="0.1" 
            android:toAlpha="1.0"
            android:duration="500"/>
    </set>

    push_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" />
        <alpha 
            android:fromAlpha="1.0" 
            android:toAlpha="0.1"
            android:duration="500"/>
    </set>

    push_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" />
        <alpha 
            android:fromAlpha="0.1" 
            android:toAlpha="1.0"
            android:duration="500"/>
    </set> 

    push_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" />
        <alpha 
            android:fromAlpha="1.0" 
            android:toAlpha="0.1"
            android:duration="500"/>
    </set> 


    代码:

    
    import android.app.Activity;
    import android.gesture.GestureOverlayView;
    import android.gesture.GestureOverlayView.OnGestureListener;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.animation.AnimationUtils;
    import android.widget.ImageView;
    import android.widget.ViewFlipper;
    
    /*
     * 新手帮助,引导页
     */
    public class XinshouHelpActivity extends Activity implements android.view.GestureDetector.OnGestureListener{
    
    	private ViewFlipper viewFlipper;
    	private GestureDetector gesture;
    	
    	@Override
    	public void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.bangzhu);
    		viewFlipper=(ViewFlipper)findViewById(R.id.newHandHelpViewFlipper);
    		gesture=new GestureDetector(this);
    		
    		viewFlipper.addView(getImageView(R.drawable.guide1));
    		viewFlipper.addView(getImageView(R.drawable.guide2));
    		viewFlipper.addView(getImageView(R.drawable.guide3));
    		viewFlipper.addView(getImageView(R.drawable.guide5));
    	}
       
    	//添加视图
    	private View getImageView(int resId)
    	{
    		ImageView img=new ImageView(this);
    		img.setImageResource(resId);
    		return img;
    	}
    	
    	//监听触摸事件
    	@Override
    	public boolean onTouchEvent(MotionEvent event)
    	{
    		return gesture.onTouchEvent(event);
    	}
    	
    	@Override
    	public boolean onDown(MotionEvent e) {
    		// TODO Auto-generated method stub
    		return false;
    	}
        //down事件发生而move或则up还没发生前触发该事件
    	@Override
    	public void onShowPress(MotionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
        //一次点击up事件
    	@Override
    	public boolean onSingleTapUp(MotionEvent e) {
    		// TODO Auto-generated method stub
    		return false;
    	}
        
    	//在屏幕上拖动事件
    	@Override
    	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
    			float distanceY) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public void onLongPress(MotionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	//滑动手势事件
    	@Override
    	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    			float velocityY) {
    		if(e1.getX() - e2.getX() > 120)
    		{//向右滑动
    		   viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
    		   viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
    		   viewFlipper.showNext();
    	     }
    		else if(e2.getX() - e1.getX() > 120)
    		 {//向左滑动
    			viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
    			viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
    			viewFlipper.showPrevious();
    		}
    		return false;
    	}
    
    }
    

    效果不是很好,没做到手指滑动时视图跟着滚动的效果,但是勉强还是可以用接受。。。。

  • 相关阅读:
    ZK节点介绍和基本操作
    zooker集群容器搭建
    Redisson 整合Spring测试分布式锁
    Redis主从哨兵容器配置
    Redis分布式锁
    Redis调用Lua脚本并测试
    基于Docker的Mysql 主从架构搭建
    容器安装及使用基础
    ConcurrentHashMap源码解读
    原码反码补码
  • 原文地址:https://www.cnblogs.com/xieyuan/p/3787404.html
Copyright © 2020-2023  润新知