Android Drag Control
Recently, I was studying a Drag Control activity similar to screen saver unlocking. I found a very dazzling activity on github, but I really don't know how to use it, so I just made a simple one. The following is the source code, which is very simple and the visual effect is not so good.
This is MianActivity:
Public class MainActivity extends Activity {ImageView basketball; ImageView circle; TextView arowleft, arowright, player_login, coach_login; boolean isLogin = false; int x, y; // original Imageview location @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // setContentView (R. layout. activity_main); basketball = (ImageView) findV IewById (R. id. basketball); arowleft = (TextView) findViewById (R. id. arowleft); arowright = (TextView) findViewById (R. id. arowright); circle = (ImageView) findViewById (R. id. circle); player_login = (TextView) findViewById (R. id. player_login); coach_login = (TextView) findViewById (R. id. coach_login); basketball. setOnTouchListener (moveListener); // set touch listener} private OnTouchListener moveListener = new OnTouchListener () {in T lastX, lastY; @ Overridepublic boolean onTouch (View v, MotionEvent e) {// TODO Auto-generated method stubswitch (e. getAction () {case MotionEvent. ACTION_DOWN: coach_login.setVisibility (View. VISIBLE); player_login.setVisibility (View. VISIBLE); circle. setVisibility (View. VISIBLE); arowleft. setVisibility (View. VISIBLE); arowright. setVisibility (View. VISIBLE); lastX = (int) e. getRawX (); lastY = (int) e. getRawY (); x = (int) E. getRawX (); y = (int) e. getRawY (); basketball. setImageResource (R. drawable. basketball_light); break; case MotionEvent. ACTION_MOVE: int distancX = (int) e. getRawX ()-x; // calculate the distance from the original location int dx = (int) e. getRawX ()-lastX; int dy = (int) e. getRawY ()-lastY; int left = v. getLeft () + dx; int top = v. getTop (); int right = v. getRight () + dx; int bottom = v. getBottom (); if (distancX> 150 |! IsLogin) // when it is moved to the specified position, it changes the Color of the corresponding Textview {coach_login.setTextColor (Color. YELLOW); arowright. setTextColor (Color. YELLOW); isLogin = true;} else if (isLogin | distancX <150 | distancX> 0) {coach_login.setTextColor (Color. WHITE); arowright. setTextColor (Color. WHITE);} if (distancX <-150 |! IsLogin) {player_login.setTextColor (Color. YELLOW); arowleft. setTextColor (Color. YELLOW); isLogin = true ;}else if (distancX>-150 | distancX <0) {player_login.setTextColor (Color. WHITE); arowleft. setTextColor (Color. WHITE);} v. layout (left, top, right, bottom); // constantly reset the position to achieve the moving effect lastX = (int) e. getRawX (); lastY = (int) e. getRawY (); break; case MotionEvent. ACTION_UP: coach_login.setVisibility (View. GONE); // restores player_login.setVisibility (View. GONE); circle. setVisibility (View. GONE); arowleft. setVisibility (View. GONE); arowright. setVisibility (View. GONE); basketball. setImageResource (R. drawable. basketball); player_login.setTextColor (Color. WHITE); arowleft. setTextColor (Color. WHITE); coach_login.setTextColor (Color. WHITE); arowright. setTextColor (Color. WHITE); break;} return true ;}};}
This is the layout File