SoFunction
Updated on 2025-04-08

Android programming method to realize left and right sliding switch background

This article describes the method of Android programming to achieve left and right sliding switching background. Share it for your reference, as follows:

Recently, I want to make an application to switch background pictures with left and right sliding, so I will share my research:

This requires inheriting 2 listening interfaces. OnGestureListener, OnTouchListener

You can check these 2 interfaces online

2 attributes are required to be set at the same time

(this);
(true);

And there are the following sentences in this function

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return (event);
}

Attached code:

public class SwitcherActivity extends Activity implements OnGestureListener,
    OnTouchListener {
  /** Called when the activity is first created. */
  LinearLayout bgLayout = null;
  private GestureDetector mGesture = null;
  private int flag = 3;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    mGesture = new GestureDetector(this);
    bgLayout = (LinearLayout) findViewById();
    (.bg3);
    (this);
    (true);
  }
  public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {
    // Handle left and right sliding    if (() - () > 100) { // Swipe left      if (flag == 3) {
        (.bg4);
        flag = 4;
        return true;
      }
      if (flag == 4) {
        (.bg5);
        flag = 5;
        return true;
      }
      if (flag == 1) {
        (.bg2);
        flag = 2;
        return true;
      }
      if (flag == 2) {
        (.bg3);
        flag = 3;
        return true;
      }
    } else if (() - () < -100) { // Swipe right      if (flag == 3) {
        (.bg2);
        flag = 2;
        return true;
      }
      if (flag == 2) {
        (.bg1);
        flag = 1;
        return true;
      }
      if (flag == 5) {
        (.bg4);
        flag = 4;
        return true;
      }
      if (flag == 4) {
        (.bg3);
        flag = 3;
        return true;
      }
    }
    return false;
  }
  public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
  }
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) {
    // TODO Auto-generated method stub
    return false;
  }
  public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub
  }
  public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
  }
  public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return (event);
  }
}

For more information about Android pictures and special effects related to readers who are interested in view the special topic of this site:Android development animation skills summary"and"Summary of Android graphics and image processing skills

I hope this article will be helpful to everyone's Android programming design.