The program described in this article is a mobile view class for an Android Moveview. It mainly realizes the main screen left and right sliding screen drag function, and timely display drag when the screen display, the code also includes complete logic. The 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;//Here is only initialized to 30, actually does not function 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 (msg) {//Processing message synchronized (moveview.this)
{//sync isaimationmoving = true;
int move_change = (int) (SCREEN_W * WIDTH_RATE/5);
int left = Main_show_view.getview (). GetLeft ();
if (msg.what = = 1) {//The main screen is dragged to the right ...
Move (Move_change + left);//parameter is dragged out the coordinates of the rear screen.
} if (Msg.what = =) {isaimationmoving = false; Movetoleft (FALSE);
Meet the requirements that can be displayed start display, False is only a judgment condition, to distinguish the effect of the button} if (Msg.what = 2) {//on behalf of the main screen left drag ...
Move ( -1 * move_change + left);
} if (Msg.what = =) {isaimationmoving = false;
Movetoright (FALSE); } if (Msg.what = = 0) {if (now_state = = left) {//If left-hand move ( -1 * move_x_v) is now displayed;//Then revert to main screen} else {move
_X_V);
} if (Msg.what = n) {isaimationmoving = false;
Movetomain (FALSE);
}
}
}
};Public Moveview {Super (context);
Public Moveview (context, AttributeSet attrs) {Super (context, attrs);
Public Moveview (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 ());
The public void Initcontent () {} is public void move (int start) {//This function displays the screen when dragging.
int left = Main_show_view.getview (). GetLeft ();//left the coordinates of the position representing the left-hand edge of the main screen. if (now_state = = MAIN) {if (left > 0) {//If the left-hand side of the screen is moved to the right, then it becomes a positive number if (Move_state!= move_to_left) {move_state = M
Ove_to_left; } left_show_view.getview (). setvisibility (view.visible);//setting//right_show_view.getview () visible to the left. Setvisibility (View.gone);
}//else if (left < 0) {//If the left-hand side of the screen is shifted to left, it becomes a negative number//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 argument is left, top, right, and bottom. else {left = (int) (SCREEN_W * width_rate),//left the control space that should be left on the left-hand side, or the position to the left of the main screen if (now_state = right) {leave =-1 *
left;//if the state is vacated on the right, then the left side of the main screen is negative.
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_t
O_rest) {if (now_state = = = MAIN) {int w = (int) (SCREEN_W * width_rate);//Leave a predetermined width to display the parameter settings for 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 (). MEAs
Ure (Widthmeasurespec, Heightmeasurespec);
Left_show_view.getview (). Measure (Measurespec.unspecified,//unspecified is unspecified heightmeasurespec); Left_show_view.setwidth ((int) (SCREEN_W * width_rate))//Set view width super.onmeasure (widthmeasurespec,
HEIGHTMEASURESPEC);//Invoke constructor of 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 super.dispatchtouchevent (E
V);
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 the distance moved above y is greater than the distance moved above X.
Super.ontouchevent (EV);
return true;
else {//x move distance greater than Y if (Math.Abs (last_x-start_x) > 5) {//x move distance greater than 5 is considered to have moved ismoved = true; }} if (ismoved) {if (this.touch_state = = touch_state_moving) {if (Math.Abs (last_x-start_x) > 10) {/
/If the x distance moved is greater than the 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) {//The correct movement of the direction is the moving state ismoved = false;
Break
} if (move_x < 0 && Now_state = = right) {//If now on the right-hand side, then move to the right does not respond.
Ismoved = false;
Break
} if (move_x < 0 && now_state ==main) {//If it is now on the main screen, moving to the right is not valid Ismoved = false;
Break
} if (move_x > 234 && now_state ==main) {ismoved = true;//set to True then move or occur, after the break, it will still work in the Actionup
The following move () statement is not executed after Break;//break,} if (Move_x < -234 && now_state ==left) {ismoved = true;
Break
} move (move_x);//Call Move () on the moving distance for the parameter to implement the moved dynamic display}} 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 do you now assign Now_state to left?
}/else {//else and the statements in parentheses are all prepared for the original button ... Mhandler.postdelayed (New Runnable () {///a newly operational function////@Override//public void Run () {//int move_change = (int ) (Screen_w * WIDTH_RATE/5);//One-fifth the width//int left = (int) (SCREEN_W * Width_rate-main_show_view//. GetView ()) to display.
GetLeft ());//getleft () returns the distance from the left edge of the main screen/msg = new Message ();
if (Left > Move_change) {//bracket inside do not understand, what role?
Msg.what = 1; Mhandler.sendmessage (msg); Mhandler.postdelayed (this, 10);//And call yourself, basically is less than the relationship/} else {//msg.what = one;//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 is initialized to a proportional size of the screen Initview ();
Initcontent ();
Movetomain (FALSE);
public int getnowstate () {return this.now_state; }
}