Android Drag Control

Source: Internet
Author: User

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

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.