Android_左右滑动切换背景

来源:互联网 发布:韩语网络班 编辑:程序博客网 时间:2024/05/27 21:15

最近想做一个左右滑动切换背景图片的应用,特地将自己的研究分享一下:

这个需要继承2个监听接口 OnGestureListener,  OnTouchListener

关于这2个接口大家可以在网上查一下

同事需要设置2个属性 bgLayout.setOnTouchListener(this);

             bgLayout.setLongClickable(true);

并且在这个函数中有如下这几句话

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

return this.mGesture.onTouchEvent(event);

}

附送代码:

[html] view plaincopy
  1. public class SwitcherActivity extends Activity implements OnGestureListener,  
  2.         OnTouchListener {  
  3.     /** Called when the activity is first created. */  
  4.     LinearLayout bgLayout = null;  
  5.     private GestureDetector mGesture = null;  
  6.     private int flag = 3;  
  7.   
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.   
  13.         mGesture = new GestureDetector(this);  
  14.   
  15.         bgLayout = (LinearLayout) findViewById(R.id.bg);  
  16.         bgLayout.setBackgroundResource(R.drawable.bg3);  
  17.         bgLayout.setOnTouchListener(this);  
  18.         bgLayout.setLongClickable(true);  
  19.     }  
  20.   
  21.     public boolean onDown(MotionEvent e) {  
  22.         // TODO Auto-generated method stub  
  23.         return false;  
  24.     }  
  25.   
  26.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  27.             float velocityY) {  
  28.         // 处理左右滑动  
  29.         if (e1.getX() - e2.getX() > 100) { // 向左滑动  
  30.             if (flag == 3) {  
  31.                 bgLayout.setBackgroundResource(R.drawable.bg4);  
  32.                 flag = 4;  
  33.                 return true;  
  34.             }  
  35.             if (flag == 4) {  
  36.                 bgLayout.setBackgroundResource(R.drawable.bg5);  
  37.                 flag = 5;  
  38.                 return true;  
  39.             }  
  40.             if (flag == 1) {  
  41.                 bgLayout.setBackgroundResource(R.drawable.bg2);  
  42.                 flag = 2;  
  43.                 return true;  
  44.             }  
  45.             if (flag == 2) {  
  46.                 bgLayout.setBackgroundResource(R.drawable.bg3);  
  47.                 flag = 3;  
  48.                 return true;  
  49.             }  
  50.         } else if (e1.getX() - e2.getX() < -100) { // 向右滑动  
  51.             if (flag == 3) {  
  52.                 bgLayout.setBackgroundResource(R.drawable.bg2);  
  53.                 flag = 2;  
  54.                 return true;  
  55.             }  
  56.             if (flag == 2) {  
  57.                 bgLayout.setBackgroundResource(R.drawable.bg1);  
  58.                 flag = 1;  
  59.                 return true;  
  60.             }  
  61.             if (flag == 5) {  
  62.                 bgLayout.setBackgroundResource(R.drawable.bg4);  
  63.                 flag = 4;  
  64.                 return true;  
  65.             }  
  66.             if (flag == 4) {  
  67.                 bgLayout.setBackgroundResource(R.drawable.bg3);  
  68.                 flag = 3;  
  69.                 return true;  
  70.             }  
  71.         }  
  72.         return false;  
  73.     }  
  74.   
  75.     public void onLongPress(MotionEvent e) {  
  76.         // TODO Auto-generated method stub  
  77.   
  78.     }  
  79.   
  80.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  81.             float distanceY) {  
  82.         // TODO Auto-generated method stub  
  83.         return false;  
  84.     }  
  85.   
  86.     public void onShowPress(MotionEvent e) {  
  87.         // TODO Auto-generated method stub  
  88.   
  89.     }  
  90.   
  91.     public boolean onSingleTapUp(MotionEvent e) {  
  92.         // TODO Auto-generated method stub  
  93.         return false;  
  94.     }  
  95.   
  96.     public boolean onTouch(View v, MotionEvent event) {  
  97.         // TODO Auto-generated method stub  
  98.         return this.mGesture.onTouchEvent(event);  
  99.     }  
  100. }  
0 0