zoukankan      html  css  js  c++  java
  • android之OnGestureListener实现图片的左右滑动

    先来看看效果图吧

    welcome_glide.xml 

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

      Java代码

    import android.app.Activity;  
    import android.content.Intent;  
    import android.os.Bundle;  
    import android.util.DisplayMetrics;  
    import android.util.Log;  
    import android.view.GestureDetector;  
    import android.view.KeyEvent;  
    import android.view.MotionEvent;  
    import android.view.View;  
    import android.view.ViewGroup;  
    import android.view.Window;  
    import android.view.WindowManager;  
    import android.view.GestureDetector.OnGestureListener;  
    import android.view.animation.AnimationUtils;  
    import android.widget.ImageView;  
    import android.widget.ViewFlipper;  
    import static android.view.KeyEvent.KEYCODE_BACK;    
      
    public class WelcomeGlide extends Activity implements OnGestureListener{  
        private static final String TAG = "Fuchangle.AggHelp";  
        private int mScreenWidth ;  
        private int mScreenHeight ;  
        private GestureDetector detector;  
        private ViewFlipper flipper;  
        private int mIndexHelpPic = 0;  
        private int[] mBgList = {  
                R.drawable.guide01,  
                R.drawable.guide02,  
                R.drawable.guide03,  
                R.drawable.guide04,  
                R.drawable.guide05  
        };  
          
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
              
            requestWindowFeature(Window.FEATURE_NO_TITLE);  
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
              
            setContentView(R.layout.welcome_glide);  
          
            DisplayMetrics dm =  new DisplayMetrics();  
            getWindowManager().getDefaultDisplay().getMetrics(dm);  
            mScreenWidth = dm.widthPixels;  
            mScreenHeight = dm.heightPixels;  
            if(mScreenWidth <240){  
                mScreenWidth = 240;  
            }  
              
            if(mScreenHeight < 320){  
                mScreenHeight = 320;  
            }  
            flipper = (ViewFlipper)this.findViewById(R.id.viewflipper);  
            flipper.addView(addImageByID(mBgList[mIndexHelpPic]), new ViewGroup.LayoutParams  
                    (ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));  
            detector = new GestureDetector(this);  
        }  
          
        @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() > 120){  
                if(++mIndexHelpPic >= mBgList.length){  
                    mIndexHelpPic = mBgList.length - 1;  
                    backToMainDesk();  
                    return true;  
                }  
                  
                this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));  
                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));  
                  
                flipper.addView(addImageByID(mBgList[mIndexHelpPic]),new ViewGroup.LayoutParams(  
                        ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));  
                this.flipper.showNext();  
            }else if(e1.getX() - e2.getX()< -120){  
                if(--mIndexHelpPic < 0){  
                    mIndexHelpPic = 0;  
                    return true;  
                }  
                  
                this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));  
                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));  
                this.flipper.showPrevious();  
                return true;  
            }  
            return true;  
        }  
          
        private void backToMainDesk(){  
            startActivity(new Intent(this,QQActivity.class));  
            finish();  
        }  
          
        @Override  
        public boolean onKeyDown(int keyCode, KeyEvent event) {    
               switch (keyCode) {    
                  case KEYCODE_BACK:    
                     backToMainDesk();    
                 return true;    
                 default:    
                     break;    
               }    
               return super.onKeyDown(keyCode, event);    
           }    
      
          
        private View addImageByID(int id){  
            ImageView img = new ImageView(this);  
            img.setImageResource(id);  
            img.setAdjustViewBounds(true);  
            img.setMaxWidth(mScreenWidth);  
            img.setMaxHeight(mScreenHeight);  
            img.setScaleType(ImageView.ScaleType.FIT_XY);  
            return img;  
        }  
          
        @Override  
        public boolean onTouchEvent(MotionEvent event) {  
            Log.i("Fling",  "Activity onTouchEvent!");  
            return this.detector.onTouchEvent(event);    
        }  
          
        @Override  
        public void onLongPress(MotionEvent e) {  
        }  
      
        @Override  
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {  
            return false;  
        }  
      
        @Override  
        public void onShowPress(MotionEvent e) {  
        }  
      
        @Override  
        public boolean onSingleTapUp(MotionEvent e) {  
            return false;  
        }  
          
    }  
    

      下面的文件放在res/anim里面 

    push_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"/>  
        <alpha android:fromAlpha="1.0" 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="1.0"    
            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="0" android:toXDelta="100%p"  
            android:duration="500" />  
        <alpha android:fromAlpha="1.0" 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="-100%p" android:toXDelta="0"    
            android:duration="500" />    
        <alpha android:fromAlpha="1.0" android:toAlpha="1.0"    
            android:duration="500" />    
    </set>   
    

      原文章地址:http://fuchangle.iteye.com/admin/blogs/1497208

     

     之二:

     

    [code=java]
    package com.sl.quwei.ui.manage;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.view.*;
    import android.view.animation.AnimationUtils;
    import android.widget.*;
    import com.sl.quwei.QuweiApplication;
    import com.sl.quwei.R;

    import static android.view.KeyEvent.KEYCODE_BACK;

    /**
    * Created by IntelliJ IDEA.
    * User: wulong
    * Date: 12-2-22
    * Time: 上午4:15
    * To change this template use File | Settings | File Templates.
    */
    public class AggHelp extends Activity implements GestureDetector.OnGestureListener {
    private static final String TAG = "Quwei.AggHelp";
    private int mScreenWidth = 240;
    private int mScreenHeight = 320;
    private GestureDetector detector;
    private ViewFlipper flipper;
    private int mIndexHelpPic = 0;
    private int[] mBgList = {
    R.drawable.agg_help01, //滑动需要的几张图片
    R.drawable.agg_help02,
    R.drawable.agg_help03,
    R.drawable.agg_help04,
    R.drawable.agg_help05,
    R.drawable.agg_help06
    };

    /**
    * Called when the activity is first created.
    */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.agg_bangzhuzhiyin);

    CharSequence title = getTitle();
    String loginName = ((QuweiApplication) getApplication()).getLoginUserName();
    String titleName = AggTools.getNewTitle(title, loginName);
    setTitle(titleName);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    mScreenWidth = dm.widthPixels;
    mScreenHeight = dm.heightPixels;
    if (mScreenWidth < 240) {
    mScreenWidth = 240;
    }

    if (mScreenHeight < 320) {
    mScreenHeight = 320;
    }


    flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);

    // flipper.addView(addButtonByText("按钮"), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    flipper.addView(addImageByID(mBgList[mIndexHelpPic]), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    detector = new GestureDetector(this);
    }

    private void backToMainDesk() {
    startActivity(new Intent(this, AggManage.class));
    finish();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KEYCODE_BACK:
    backToMainDesk();
    return true;
    default:
    break;
    }
    return super.onKeyDown(keyCode, event);
    }

    private View addImageByID(int id) {
    ImageView img = new ImageView(this);
    img.setImageResource(id);
    img.setAdjustViewBounds(true);
    img.setMaxWidth(mScreenWidth);
    img.setMaxHeight(mScreenHeight);
    img.setScaleType(ImageView.ScaleType.FIT_XY);
    return img;
    }

    public View addButtonByText(String text) {
    Button btn = new Button(this);
    btn.setText(text);
    return btn;
    }

    public View addTextByText(String text) {
    TextView tv = new TextView(this);
    tv.setText(text);
    tv.setGravity(1);
    return tv;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    Log.i("Fling", "Activity onTouchEvent!");
    return this.detector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
    }

    /**
    * 监听滑动
    */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
    // TODO Auto-generated method stub
    Log.i("Fling", "Fling Happened!");
    if (e1.getX() - e2.getX() > 120) {
    if (++mIndexHelpPic >= mBgList.length) {
    mIndexHelpPic = mBgList.length - 1;
    backToMainDesk();
    return true;
    }

    this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
    this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
    // this.flipper.addView(addTextByText("文本框"), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    flipper.addView(addImageByID(mBgList[mIndexHelpPic]), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    this.flipper.showNext();
    return true;
    } else if (e1.getX() - e2.getX() < -120) {
    if (--mIndexHelpPic < 0) {
    mIndexHelpPic = 0;
    return true;
    }

    this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
    this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
    this.flipper.showPrevious();
    return true;
    }
    return true;
    }

    @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;
    }
    }
    [/code]

    配置文件:

    agg_bangzhuzhiyin.xml
    [code=html]
    <?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"
    android:background="@drawable/agg_sub_bg">
    <ViewFlipper android:id="@+id/ViewFlipper01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    </ViewFlipper>
    </LinearLayout>
    [/code]

    push_left_in.xml 放在anim
    [code=html]
    <?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>
    [/code]

    push_left_out.xml放在anim

    [code=html]
    <?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>
    [/code]

    push_right_in.xml放在anim
    [code=html]
    <?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>
    [/code]

    push_right_out.xml放在anim

    [code=html]
    <?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>
    [/code]

     

  • 相关阅读:
    POJ2516 构图+k次费用流
    POJ 1511 最短路径之和(spfa或dijkstra+heap)
    windows中配置mongodb
    原型设计
    Erlang的参考资源
    用Erlang实现递归查找文件
    list相关的习题
    springmvc基础知识
    汇编实验4
    实验3 转移指令跳转原理及其简单应用编程
  • 原文地址:https://www.cnblogs.com/gzggyy/p/2472036.html
Copyright © 2011-2022 走看看