Android custom message prompt box similar to QQ
When I saw the QQ message prompt box, I felt that the effect was very good. I made a similar display, as shown below:
Click to view short videos
The effect is good. The overall result is the combination of the Translate animation and the FrameLayout layout. Let's take a look at the code below:
Activiy_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
Xmlns: tools = http://schemas.android.com/tools Android: layout_width = match_parent Android: layout_height = match_parent Android: orientation = vertical> <Framelayout Android: layout_height = wrap_content> Android: id = @ + id/toast Android: layout_width = wrap_content Android: layout_height = wrap_content Android: layout_gravity = center Android: layout_marginBottom = 20dp Android: layout_marginTop = 40dp Android: background = @ color/transparent_black Android: gravity = center Android: paddingBottom = 5dp Android: paddingLeft = 40dp Android: paddingRight = 40dp Android: paddingTop = 5dp Android: text = @ string/toast Android: textColor = @ color/white Android: textSize = 16sp Android: visibility = invisible/> Android: id = @ + id/title Android: layout_width = match_parent Android: layout_height = wrap_content Android: background = @ color/blue Android: gravity = center Android: padding = 5dp Android: text = @ string/title Android: textColor = @ color/white Android: textSize = 20sp/> Android: id = @ + id/info Android: layout_width = match_parent Android: layout_height = wrap_content Android: layout_marginTop = 10dp Android: gravity = center Android: text = @ string/hello_world/> Android: id = @ + id/btn Android: layout_width = match_parent Android: layout_height = wrap_content Android: layout_marginTop = 30dp Android: gravity = center Android: text = @ string/btn> </Framelayout |
MainActivity. java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
Package cn. androiddevelop;
Import android. app. Activity; Import android. OS. Bundle; Import android. OS. Handler; Import android. OS. Message; Import android. view. View; Import android. view. animation. Animation; Import android. view. animation. TranslateAnimation; Import android. widget. Button; Import android. widget. TextView; Import cn. androiddevelop. test. R;
Public class MainActivity extends Activity { TextView rootView; Button btn; Handler handler; TextView TV; Animation mTranslateInAnimation, mTranslateOutAnimation; Boolean flag = true;
@ Override Protected void onCreate (Bundle savedInstanceState ){ Super. onCreate (savedInstanceState ); SetContentView (R. layout. activity_main ); RootView = (TextView) findViewById (R.id.info ); Btn = (Button) findViewById (R. id. btn ); TV = (TextView) findViewById (R. id. toast );
Handler = new Handler (){ @ Override Public void handleMessage (Message msg ){ TV. startAnimation (mTranslateOutAnimation ); TV. setGravity (View. INVISIBLE ); Super. handleMessage (msg ); }
};
// Define the animation for entering and exiting MTranslateInAnimation = new TranslateAnimation ( Animation. RELATIVE_TO_SELF, 0, Animation. RELATIVE_TO_SELF, 0, Animation. RELATIVE_TO_SELF,-1.5f, Animation. RELATIVE_TO_SELF, 0 ); MTranslateInAnimation. setDuration (1000 );
MTranslateOutAnimation = new TranslateAnimation ( Animation. RELATIVE_TO_SELF, 0, Animation. RELATIVE_TO_SELF, 0, Animation. RELATIVE_TO_SELF, 0f, Animation. RELATIVE_TO_SELF, -1.5f ); MTranslateOutAnimation. setDuration (1000 );
MTranslateOutAnimation // hide the TV control after the animation is displayed. . SetAnimationListener (new Animation. AnimationListener (){ @ Override Public void onAnimationStart (Animation animation ){ }
@ Override Public void onAnimationEnd (Animation animation ){ TV. setVisibility (View. INVISIBLE ); Flag = true; // restore the response }
@ Override Public void onAnimationRepeat (Animation animation ){ } });
Btn. setOnClickListener (new Button. OnClickListener (){
@ Override Public void onClick (View v ){ If (flag) {// click the button for the first time to take effect. No response is returned before the message box exits. Flag = false; } Else { Return; }
// Display the message box TV. startAnimation (mTranslateInAnimation ); TV. setVisibility (View. VISIBLE );
New Thread (){ @ Override Public void run (){ Try { Sleep (3000 ); Handler. sendEmptyMessage (0 ); } Catch (InterruptedException e ){ E. printStackTrace (); } } }. Start (); } }); } } |