Android Moveview Sliding Screen Move View Class Complete Example
The program described in this example is an Android Moveview mobile view class. It mainly implements the sliding screen left and right function of the main screen, and displays the display status of the screen when dragging in time. This code also includes complete logic. The complete code is as follows:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; 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 initialized to 30, but it actually 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) {//Processing messages synchronized () {//synchronous isAimationMoving = true; int move_change = (int) (screen_w * WIDTH_RATE / 5); int left = main_show_view.getView().getLeft(); if ( == 1) {//Drag the main screen to the right. . . move(move_change + left);//The parameter is the coordinates of the main screen after being dragged out. } if ( == 11) { isAimationMoving = false; moveToLeft(false); //Select the display to be displayed as required. False is just a determination condition to distinguish the effect of the button. } if ( == 2) {// means that the main screen is dragging to the left. . . move(-1 * move_change + left); } if ( == 12) { isAimationMoving = false; //moveToRight(false); } if ( == 0) { if (now_state == LEFT) {//If the left side is already displayed move(-1 * move_x_v);//Then restore to the main screen } else { move(move_x_v); } } if ( == 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); left_show_view = new ItemView(()); //right_show_view = new ItemView((), "aaa"); } (left_show_view.getView()); //(right_show_view.getView()); (main_show_view.getView()); } public void initContent() { } public void move(int start) {//This function shows the display status of the screen when dragging. 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, the left becomes a positive number if (move_state != MOVE_TO_LEFT) { move_state = MOVE_TO_LEFT; } left_show_view.getView().setVisibility();//Set to the left to see //right_show_view.getView().setVisibility(); } //else if (left < 0) {//If the left side of the screen is moved to the left, left becomes a negative number //if (move_state != MOVE_TO_RIGHT) { // move_state = MOVE_TO_RIGHT; //} //right_show_view.getView().setVisibility(); //left_show_view.getView().setVisibility(); //} 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 in turn. } else { left = (int) (screen_w * WIDTH_RATE);//left is the control space that should be left on the left, or the position on the left of the main screen should be located if (now_state == RIGHT) { left = -1 * left;//If the state is empty on the right, the left side of the main screen will be 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 and display the parameter settings of each view. 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(,//unspecified is unspecified heightMeasureSpec); left_show_view.setWidth((int) (screen_w * WIDTH_RATE));//Set view width (widthMeasureSpec, heightMeasureSpec);//Calling 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 event if (isAimationMoving) { return (ev); } else { int action = (); float x = (); float y = (); switch (action) { case MotionEvent.ACTION_DOWN: (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; (ev); if (!isMoved) { if ((last_y - start_y) > (last_x - start_x)) {//If the distance moving above Y is greater than the distance moving above X. (ev); return true; } else {//X moving distance is greater than Y if ((last_x - start_x) > 5) {//X moving distance is greater than 5, it is considered to be moving. isMoved = true; } } } if (isMoved) { if (this.touch_state == TOUCH_STATE_MOVING) { if ((last_x - start_x) > 10) {//If the distance of movement is greater than 10 int left = main_show_view.getView().getLeft(); ("msg", "left:" + left); ("msg", "x:" + last_x); isMoved = true; int move_x = last_x - start_x; if (move_x > 0 && now_state == LEFT) {//The correct movement is considered a state of movement isMoved = false; break; } if (move_x < 0 && now_state == RIGHT) {//If it is now displayed on the right, then it will not react when it moves to the right. isMoved = false; break; } if (move_x < 0 && now_state ==MAIN){//If you are on the home screen now, moving to the right will also be invalid isMoved = false; break; } if (move_x > 234 && now_state ==MAIN){ isMoved = true;//Set to true, then the movement still happens, and break will still work in ACTIONUP after it is broken break;//The following move() statement will not be executed after break, } if (move_x < -234 && now_state ==LEFT){ isMoved = true; break; } move(move_x);//Call move() with the distance of movement as the parameter to achieve 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 ((last_x - start_x) > min_distance) {// if (now_state == MAIN) { if (move_state == MOVE_TO_LEFT) { (false); } } else { (false); } } else { // if (now_state == MAIN) { (false); } if (now_state == LEFT) { (false); } if (now_state == RIGHT) { //(false); } } move_state = MOVE_TO_REST; } else { (ev); this.touch_state = TOUCH_STATE_REST; return false;// } } (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 assign now_state to left? } // else {//else and the statements in brackets are all prepared for the original button. . .// (new Runnable() {//A new runnable function// // @Override // public void run() { // int move_change = (int) (screen_w * WIDTH_RATE / 5);//One fifth of the width to be displayed// int left = (int) (screen_w * WIDTH_RATE - main_show_view // .getView().getLeft());//getleft() returns the distance from the left edge of the main screen to the left// Message msg = new Message(); // if (left > move_change) {//I don’t understand the brackets, what does it work?// = 1; // (msg); // (this, 10);// Call yourself again, basically it must be smaller than the relationship// } else { // = 11; // (msg); // (this); // } // } // }, 0); // } } public void moveToMain(boolean b) { if (!b) { //right_show_view.getView().setVisibility(); left_show_view.getView().setVisibility(); 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; ("screen", "screen_w:" + w); (true); min_distance = (int) (screen_w / 12.0);//min_distance is initialized here to a scale size of the screen initView(); initContent(); moveToMain(false); } public int getNowState() { return this.now_state; } }
Related Articles
Android realizes direct playback of the sound collected by the microphone
This article mainly introduces the sound collected by Android directly plays the microphone, and involves relevant techniques for Android audio operation. Friends who need it can refer to it.2015-06-06Methods for creating jumps and passing values in Android development
This article mainly introduces relevant information on how to create jumps and pass values in Android development. Friends who need it can refer to it2016-05-05Android development graphics bufferqueue overall process
This article mainly introduces the detailed explanation of the overall process of Android development graphics bufferqueue. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get a promotion as soon as possible.2023-07-07The reasons and solutions for the black or white screen when the Android app is started
This article mainly introduces the reasons and solutions for the black screen or white screen when the Android app is started. Friends who need it can refer to it.2016-09-09BaseActivity custom title bar in Android
This article mainly introduces the BaseActivity custom title bar in Android. It is very practical and has certain reference value. Interested friends can refer to it.2017-01-01Analysis of the detailed explanation of the use of ViewFlipper that implements sliding page turn in Android
There are some scenarios where we need to show a series of pages to the user. For example, when we are developing an application to read comics, we may need to show users a comic picture one by one. Users can use their fingers to swipe the screen to switch between the previous comic and the next comic. ViewFlipper is a good choice at this time2013-05-05Android uses TypeFace to set text fonts of TextView
This article mainly introduces the method of setting text fonts of TextView using TypeFace on Android, helping everyone better utilize Android development. Interested friends can learn about it.2021-01-01How to get the height of the screen, status bar and title bar on Android
In daily development, you often encounter the need to obtain screen height, status bar height, etc., so the following article summarizes and introduces relevant information on how Android can obtain screen, status bar and title bar height. The article introduces the sample code in detail, and friends who need it can refer to it.2017-10-10Android iOS commonly used APP crash log acquisition command method
This article mainly introduces the method of obtaining commands for Android & iOS commonly used APP crash logs. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase.2022-05-05Android Studio connects to MySql to achieve login registration (with source code)
Login and registration is a commonly used function. Today, I use Android studio to make a login software similar to this. Friends who need it, please learn with the editor below.2021-05-05