Android: exquisite pop-up box for animation implementation (similar to Yixin) and android Yixin
Demo apk download: Link: http://pan.baidu.com/s/1eQgurse password: g91r
:
Animation effects:
1. Click the "+" button on the ActionBar. The menu is displayed from the top (with a bounce effect );
2. Click "+" again, click the blank area, or click "back". The menu is collapsed up;
3. When you click the button in the pop-up box, the button is enlarged. Other buttons are zoomed out, and the overall menu gradient exits.
Subject code:
1. Activity.
/*** Email-like animation pop-up box */public class MainActivity extends ActionBarActivity {// used to mark the top position of the page private View topView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); topView = findViewById (R. id. main_top);} private PopupWindow popupWindow; private int line1DeltaY, line2DeltaY; // pop-up box for more information about imitation letter private void showPopup () {if (po PupWindow = null) {View contentView = LayoutInflater. from (this ). inflate (R. layout. yixin_pop_layout, null); // click the blank area to close View blankView = contentView. findViewById (R. id. yixin_more_blank); View blankView2 = contentView. findViewById (R. id. yixin_more_blk2); initItems (contentView); // int line2Height = ViewUtils. getViewMeasuredHeight (itemViews [0]); line1DeltaY =-getActionBarHeight ()-40; line2Delt AY = line1DeltaY-line2Height; blankView. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {dismissPopup () ;}}); blankView2.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {dismissPopup () ;}}); popupWindow = new PopupWindow (contentView, ScreenUtils. getScreenW (this), ScreenUtils. getScreenH (this); // set a drawable as the background po. PupWindow. setBackgroundDrawable (new ColorDrawable ();} if (! PopupWindow. isShowing () {popupWindow. showAsDropDown (topView, 0, 0); for (int I = 0; I <itemViews. length; I ++) {if (I <3) {// The first line itemViews [I]. startAnimation (AnimationHelper. createPopupAnimIn (this, line1DeltaY);} else {// The second line itemViews [I]. startAnimation (AnimationHelper. createPopupAnimIn (this, line2DeltaY) ;}} popupWindow. getContentView (). startAnimation (AnimationHelper. createPopupBgFadeInAni M () ;}} private void dismissPopup () {if (popupWindow = null |! PopupWindow. isShowing () {return;} ViewGroup contentView = (ViewGroup) popupWindow. getContentView (); contentView. startAnimation (AnimationHelper. createPopupBgFadeOutAnim (AnimationHelper. TIME_OUT); for (int I = 0; I <itemViews. length; I ++) {if (I <3) {// The first line itemViews [I]. startAnimation (AnimationHelper. createPopupAnimOut (this, line1DeltaY);} else {// The second line itemViews [I]. startAnimation (AnimationHe Lper. createPopupAnimOut (this, line2DeltaY) ;}// hide the popupWindow contentView when the animation ends. postDelayed (new Runnable () {@ Override public void run () {popupWindow. dismiss () ;}}, AnimationHelper. TIME_OUT + 10);} private View [] itemViews; // initialize the private void initItems (View parent) {int [] viewIds = new int [] {R. id. yixin_more_item1, R. id. yixin_more_item2, R. id. yixin_more_item3, R. id. yixin_more_ I Tem4, R. id. yixin_more_item5, R. id. yixin_more_item6}; itemViews = new View [viewIds. length]; int itemWidth = ScreenUtils. getScreenW (this)/3; OnClickImpl = new OnClickImpl (); for (int I = 0; I <viewIds. length; I ++) {int id = viewIds [I]; itemViews [I] = parent. findViewById (id); GridLayout. layoutParams p = (GridLayout. layoutParams) itemViews [I]. getLayoutParams (); p. width = itemWidth; itemViews [I]. SetLayoutParams (p); itemViews [I]. setOnClickListener (l) ;}} private class OnClickImpl implements View. onClickListener {@ Override public void onClick (View v) {final int viewId = v. getId (); // background animation popupWindow. getContentView (). startAnimation (AnimationHelper. createPopupBgFadeOutAnim (AnimationHelper. TIME_OUT_CLICK); // hide popupWindow v when the animation ends. postDelayed (new Runnable () {@ Override public void run () {po PupWindow. dismiss (); // response to the Click Event handleEvent (viewId) when the animation ends; }}, AnimationHelper. TIME_OUT_CLICK + 10); // button animation for (View item: itemViews) {if (item. getId () = v. getId () {// click the button to enlarge the item. startAnimation (AnimationHelper. createPopupItemBiggerAnim (MainActivity. this);} else {// other buttons to narrow down the item. startAnimation (AnimationHelper. createPopupItemSmallerAnim (MainActivity. this) ;}}}// Click Event priv on the popupWindow Ate void handleEvent (int viewId) {Toast. makeText (this, "click:" + viewId, Toast. LENGTH_SHORT ). show ();} private int getActionBarHeight () {return getsuppactionactionbar (). getHeight () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {int id = item. getItemId (); I F (id = R. id. action_more) {if (popupWindow = null |! PopupWindow. isShowing () {showPopup ();} else {dismissPopup ();} return true;} return super. onOptionsItemSelected (item);} // when you click the return key, if popupWindow is displayed, close it @ Override public void onBackPressed () {if (popupWindow! = Null & popupWindow. isShowing () {dismissPopup (); return;} super. onBackPressed ();}}
2. animation tools.
/*** AnimationHelper */public class AnimationHelper {/*** enter the animation time */public static final int TIME_IN = 300; /* the animation time after entering the animation */public static final int TIME_IN_BACK = 100;/* the animation exit time */public static final int TIME_OUT = 300; /*** the animation exit time after clicking the menu on PopupWindow */public static final int TIME_OUT_CLICK = 500; /*** enter the Animation menu on the PopupWindow */public static Animation createPopupAnimIn (Context context, int fromYDelta) {AnimationSet animationSet = new AnimationSet (context, null); // animationSet. setInterpolator (new BounceInterpolator (); // bounce at the end of the animation set. setFillAfter (true); // move TranslateAnimation translateAnim = new TranslateAnimation (0, 0, fromYDelta, 20); translateAnim. setDuration (TIME_IN); animationSet. addAnimation (translateAnim); // rebound effect TranslateAnimation translateAnim2 = new TranslateAnimation (0, 0, 0,-20); merge (TIME_IN); merge (TIME_IN_BACK); animationSet. addAnimation (translateAnim2); return animationSet;}/*** menu on PopupWindow exit Animation */public static Animation extract (Context context, int toYDelta) {AnimationSet animationSet = new AnimationSet (Context, null); animationSet. setFillAfter (true); TranslateAnimation translateAnim = new TranslateAnimation (0, 0, 0, toYDelta); translateAnim. setDuration (TIME_OUT); animationSet. addAnimation (translateAnim); return animationSet;}/*** enter the Animation background (Transparency gradient) */public static Animation createPopupBgFadeInAnim () {AlphaAnimation anim = new AlphaAnimation (0, 1.0f); anim. setDuration (TIME_IN); anim. setFillAfter (true); return anim;}/*** PopupWindow background exit Animation (opacity gradient) */public static Animation createPopupBgFadeOutAnim (int duration) {AlphaAnimation anim = new AlphaAnimation (1.0f, 0); anim. setDuration (duration); anim. setFillAfter (true); return anim;}/*** PopupWindow button click Animation */public static Animation createPopupItemBiggerAnim (Context context) {AnimationSet animationSet = new AnimationSet (context, null ); animationSet. setFillAfter (true); // zoom in (set the zoom center to its center) ScaleAnimation scaleAnim = new ScaleAnimation (1.0f, 2.0f, 1.0f, 2.0f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); scaleAnim. setDuration (TIME_OUT_CLICK); animationSet. addAnimation (scaleAnim); // gradient AlphaAnimation alphaAnim = new AlphaAnimation (1.0f, 0); alphaAnim. setInterpolator (new AccelerateInterpolator (); alphaAnim. setDuration (TIME_OUT_CLICK); animationSet. addAnimation (alphaAnim); return animationSet;}/*** Animation of other buttons when the PopupWindow button is clicked */public static Animation createPopupItemSmallerAnim (Context context) {// zoom in (set the center of the zoom to your center) ScaleAnimation scaleAnim = new ScaleAnimation (1.0f, 0, 1.0f, 0, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); scaleAnim. setDuration (TIME_OUT_CLICK); scaleAnim. setFillAfter (true); return scaleAnim ;}}
======
Download the complete source code:
Http://download.csdn.net/detail/books1958/8623665