Android Moveview slides the full instance of the view class

  • 2020-06-03 08:18:18
  • OfStack

The program in this article's example is an Android Moveview mobile view class. It mainly realizes the left and right slider dragging function of the home screen, and timely displays the display situation of the screen when dragging, the code also includes complete logic. Its complete code is as follows:


import android.study.shift.ItemView;
import android.study.shift.MainView;
import android.study.shift.Moveview;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
public class Moveview extends ViewGroup{
 private final static int TOUCH_STATE_REST = 0;
 private final static int TOUCH_STATE_MOVING = 1;
 private final static int MOVE_TO_LEFT = 1;
 //private final static int MOVE_TO_RIGHT = 2;
 private final static int MOVE_TO_REST = 0;
 public final static int MAIN = 0;
 public final static int LEFT = 1;
 public final static int RIGHT = 2;
 private int touch_state = TOUCH_STATE_REST;
 private int move_state = MOVE_TO_REST;
 private int now_state = MAIN;
 private final float WIDTH_RATE = 0.18f;
 private MainView main_show_view;
 private ItemView left_show_view;
 private int min_distance = 30;// This is just initialization 30  It doesn't work 
 private int screen_w;
 private int screen_h;
 private int move_x_v;
 private boolean isAimationMoving = false;
 private Handler mHandler = new Handler() {
 public void handleMessage(Message msg) {// Process the message 
  synchronized (Moveview.this) {// synchronous 
  isAimationMoving = true;
  int move_change = (int) (screen_w * WIDTH_RATE / 5);
  int left = main_show_view.getView().getLeft();
  if (msg.what == 1) {// The home screen is dragging to the right... 
   move(move_change + left);// Parameter is the coordinate of the home screen after being dragged out. 
  }
  if (msg.what == 11) {
   isAimationMoving = false;
   moveToLeft(false); // Meet the requirements of display and start to display, false just 1 A decision condition to distinguish the effect of the button 
  }
  if (msg.what == 2) {// Represents the home screen dragging to the left... 
   move(-1 * move_change + left);
  }
  if (msg.what == 12) {
   isAimationMoving = false;
   //moveToRight(false);
  }
  if (msg.what == 0) {
   if (now_state == LEFT) {// If the left side is already displayed 
   move(-1 * move_x_v);// So let's go back to the home screen 
   } else {
   move(move_x_v);
   }
  }
  if (msg.what == 10) {
   isAimationMoving = false;
   moveToMain(false);
  }
  }
 }
 };
 public Moveview(Context context) {
 super(context);
 }
 public Moveview(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 public Moveview(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 }
 public void initView() {
 if (main_show_view == null) {
  main_show_view = new MainView(this.getContext(), this);
  left_show_view = new ItemView(this.getContext());
  //right_show_view = new ItemView(this.getContext(), "aaa");
 }
 this.addView(left_show_view.getView());
 //this.addView(right_show_view.getView());
 this.addView(main_show_view.getView());
 }
 public void initContent() {
 }
 public void move(int start) {// This function shows what the screen looks like when you drag it. 
 int left = main_show_view.getView().getLeft();//left Represents the position coordinates of the left edge of the home screen. 
 if (now_state == MAIN) {
  if (left > 0) {// If the left side of the screen is moved to the right left Into a positive 
  if (move_state != MOVE_TO_LEFT) {
   move_state = MOVE_TO_LEFT;
  }
  left_show_view.getView().setVisibility(View.VISIBLE);// Set the left side visible 
  //right_show_view.getView().setVisibility(View.GONE);
  } //else if (left < 0) {// If the left side of the screen is moved to the left left Turned negative 
  //if (move_state != MOVE_TO_RIGHT) {
  // move_state = MOVE_TO_RIGHT;
  //}
  //right_show_view.getView().setVisibility(View.VISIBLE);
  //left_show_view.getView().setVisibility(View.GONE);
  //}
   else {
  move_state = MOVE_TO_REST;
  }
  main_show_view.getView().layout(start, 0, start + screen_w,
   screen_h);// The parameters are left, top, right and bottom. 
 } else {
  left = (int) (screen_w * WIDTH_RATE);//left Space for controls that should be left, or where the left side of the home screen should be 
  if (now_state == RIGHT) {
  left = -1 * left;// If the state is empty on the right, the left side of the home screen is negative 
  }
  left = left + start;
  main_show_view.getView().layout(left, 0, left + screen_w, screen_h);
 }
 }
 @Override
 protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
 if (move_state == MOVE_TO_REST) {
  if (now_state == MAIN) {
  int w = (int) (screen_w * WIDTH_RATE);// Leave a predetermined width to show each view Parameter Settings for. 
  main_show_view.getView().layout(0, 0, screen_w, screen_h);
  left_show_view.getView().layout(0, 0, w, screen_h);
  } else if (now_state == LEFT) {
  moveToLeft(false);
  } else {
  }
 }
 }
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 main_show_view.getView().measure(widthMeasureSpec, heightMeasureSpec);
 left_show_view.getView().measure(MeasureSpec.UNSPECIFIED,//unspecified For unspecified 
  heightMeasureSpec);
 left_show_view.setWidth((int) (screen_w * WIDTH_RATE));// Set up the view The width of the 
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);// Call the constructor of the parent class 
 }
 private int start_x;
 private int start_y;
 private boolean isMoved;
 public boolean dispatchTouchEvent(MotionEvent ev) {// Distribute touch message events 
 if (isAimationMoving) {
  return super.dispatchTouchEvent(ev);
 } else {
  int action = ev.getAction();
  float x = ev.getX();
  float y = ev.getY();
  switch (action) {
  case MotionEvent.ACTION_DOWN:
  super.dispatchTouchEvent(ev);
  start_y = (int) y;
  move_x_v = 0;
  if (this.touch_state == TOUCH_STATE_REST) {
   this.touch_state = TOUCH_STATE_MOVING;
   start_x = (int) x;
   isMoved = false;
   move_state = MOVE_TO_REST;
  }
  break;
  case MotionEvent.ACTION_MOVE:
  int last_y = (int) y;
  int last_x = (int) x;
  super.dispatchTouchEvent(ev);
  if (!isMoved) {
   if (Math.abs(last_y - start_y) > Math.abs(last_x - start_x)) {// if Y The distance up here is greater than X The distance that the top moves. 
   super.onTouchEvent(ev);
   return true;
   } else {//X Travel distance greater than Y
   if (Math.abs(last_x - start_x) > 5) {//X Travel distance greater than 5 It's considered to have moved 
    isMoved = true;
   }
   }
  }
  if (isMoved) {
   if (this.touch_state == TOUCH_STATE_MOVING) {
   if (Math.abs(last_x - start_x) > 10) {// If moving X Distance is greater than the 10
    int left = main_show_view.getView().getLeft();
    Log.d("msg", "left:" + left);
    Log.d("msg", "x:" + last_x);
    isMoved = true;
    int move_x = last_x - start_x;
    if (move_x > 0 && now_state == LEFT) {// A move in the right direction is a move 
    isMoved = false;
    break;
    }
    if (move_x < 0 && now_state == RIGHT) {// If you are now on the right side of the display, moving to the right will not respond. 
    isMoved = false;
    break;
    }
    if (move_x < 0 && now_state ==MAIN){// If you are on the home screen, moving to the right will not work 
    isMoved = false;
    break;
    }
    if (move_x > 234 && now_state ==MAIN){
    isMoved = true;// Set to true So the movement is still happening, break It's still going to be there ACTIONUP Play a role 
    break;//break The following will not be executed later move() statements ,
    }
    if (move_x < -234 && now_state ==LEFT){
    isMoved = true;
    break;
    }
    move(move_x);// Called with the distance moved as an argument move () Realize the dynamic display of movement 
   }
   }
   return false;
  }
  break;
  case MotionEvent.ACTION_UP://
  if (this.touch_state == TOUCH_STATE_MOVING) {
   if (isMoved) {
   last_x = (int) x;
   if (Math.abs(last_x - start_x) > min_distance) {//
    if (now_state == MAIN) {
    if (move_state == MOVE_TO_LEFT) {
     this.moveToLeft(false);
    }
    } else {
    this.moveToMain(false);
    }
   } else {  //
    if (now_state == MAIN) {
    this.moveToMain(false);
    }
    if (now_state == LEFT) {
    this.moveToLeft(false);
    }
    if (now_state == RIGHT) {
    //this.moveToRight(false);
    }
   }
   move_state = MOVE_TO_REST;
   }  else {
   super.dispatchTouchEvent(ev);
   this.touch_state = TOUCH_STATE_REST;
   return false;//
   }
  }
  super.onTouchEvent(ev);
  this.touch_state = TOUCH_STATE_REST;
  break;
  }
  return true;
 }
 }
 public boolean getIsMoved() {
 return isMoved;
 }
 public void moveToLeft(boolean b) {
 if (!b) {
  int move_x = (int) (screen_w * WIDTH_RATE);
  left_show_view.getView().layout(0, 0, screen_w, screen_h);
  //right_show_view.getView().layout(move_x, 0, move_x * 2, screen_h);
  main_show_view.getView().layout(move_x, 0, move_x + screen_w,
   screen_h);
  now_state = LEFT;// Why now now_state The assignment for left ? 
 }
//  else {//else And the statements in parentheses are for the original button... 
//  mHandler.postDelayed(new Runnable() {//1 New runnable functions 
//
//  @Override
//  public void run() {
//   int move_change = (int) (screen_w * WIDTH_RATE / 5);//5 m 1 The width to display 
//   int left = (int) (screen_w * WIDTH_RATE - main_show_view
//    .getView().getLeft());//getleft() Returns the distance from the left edge of the home screen 
//   Message msg = new Message();
//   if (left > move_change) {// I don't understand the parentheses. What does it do? 
//   msg.what = 1;
//   mHandler.sendMessage(msg);
//   mHandler.postDelayed(this, 10);// It calls itself, basically, must be less than 
//   } else {
//   msg.what = 11;
//   mHandler.sendMessage(msg);
//   mHandler.removeCallbacks(this);
//   }
//  }
//  }, 0);
// }
 }
 public void moveToMain(boolean b) {
 if (!b) {
  //right_show_view.getView().setVisibility(View.VISIBLE);
  left_show_view.getView().setVisibility(View.VISIBLE);
  int w = (int) (screen_w * WIDTH_RATE);
  main_show_view.getView().layout(0, 0, screen_w, screen_h);
  left_show_view.getView().layout(0, 0, w, screen_h);
  now_state = MAIN;
 }
 }
 public void initScreenSize(int w, int h) {
 this.screen_w = w;
 this.screen_h = h;
 Log.d("screen", "screen_w:" + w);
 this.setKeepScreenOn(true);
 min_distance = (int) (screen_w / 12.0);//min_distance Here it is initialized again to the screen 1 Proportional size 
 initView();
 initContent();
 moveToMain(false);
 }
 public int getNowState() {
 return this.now_state;
 }
}

Related articles: