本篇文章給大家分享的是有關(guān)Android開(kāi)發(fā)中實(shí)現(xiàn)一個(gè)彈出框的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的萬(wàn)載網(wǎng)站建設(shè)公司,萬(wàn)載接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行萬(wàn)載網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
截圖:
動(dòng)畫(huà)效果介紹:
1.點(diǎn)擊ActionBar上“+”按鈕,菜單從上方彈出(帶反彈效果);
2.再次點(diǎn)擊“+”、點(diǎn)擊空白區(qū)域或者點(diǎn)擊返回鍵,菜單向上方收起;
3.點(diǎn)擊彈出框上的按鈕時(shí),該按鈕放大,其它按鈕縮小,菜單整體漸變退出。
主體代碼:
1.Activity.
/** * 仿易信動(dòng)畫(huà)彈出框 */ public class MainActivity extends ActionBarActivity { //用于標(biāo)記頁(yè)面頂端位置 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; //仿易信更多彈出框 private void showPopup() { if (popupWindow == null) { View contentView = LayoutInflater.from(this).inflate(R.layout.yixin_pop_layout, null); //點(diǎn)擊空白區(qū)域關(guān)閉 View blankView = contentView.findViewById(R.id.yixin_more_blank); View blankView2 = contentView.findViewById(R.id.yixin_more_blank2); initItems(contentView); //測(cè)量高度 int line2Height = ViewUtils.getViewMeasuredHeight(itemViews[0]); line1DeltaY = -getActionBarHeight() - 40; line2DeltaY = 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)); //隨便設(shè)置一個(gè)drawable作為背景 popupWindow.setBackgroundDrawable(new ColorDrawable()); } if (!popupWindow.isShowing()) { popupWindow.showAsDropDown(topView, 0, 0); for (int i = 0; i < itemViews.length; i++) { if (i < 3) { //第一行 itemViews[i].startAnimation(AnimationHelper.createPopupAnimIn(this, line1DeltaY)); } else { //第二行 itemViews[i].startAnimation(AnimationHelper.createPopupAnimIn(this, line2DeltaY)); } } popupWindow.getContentView().startAnimation(AnimationHelper.createPopupBgFadeInAnim()); } } 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) { //第一行 itemViews[i].startAnimation(AnimationHelper.createPopupAnimOut(this, line1DeltaY)); } else { //第二行 itemViews[i].startAnimation(AnimationHelper.createPopupAnimOut(this, line2DeltaY)); } } //動(dòng)畫(huà)結(jié)束時(shí)隱藏popupWindow contentView.postDelayed(new Runnable() { @Override public void run() { popupWindow.dismiss(); } }, AnimationHelper.TIME_OUT + 10); } private View[] itemViews; //初始化popupWindow上的按鈕 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_item4, R.id.yixin_more_item5, R.id.yixin_more_item6}; itemViews = new View[viewIds.length]; int itemWidth = ScreenUtils.getScreenW(this) / 3; OnClickImpl l = 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(); //背景動(dòng)畫(huà) popupWindow.getContentView().startAnimation(AnimationHelper.createPopupBgFadeOutAnim(AnimationHelper.TIME_OUT_CLICK)); //動(dòng)畫(huà)結(jié)束時(shí)隱藏popupWindow v.postDelayed(new Runnable() { @Override public void run() { popupWindow.dismiss(); //動(dòng)畫(huà)結(jié)束時(shí)響應(yīng)點(diǎn)擊事件 handleEvent(viewId); } }, AnimationHelper.TIME_OUT_CLICK + 10); //按鈕動(dòng)畫(huà) for (View item : itemViews) { if (item.getId() == v.getId()) { //點(diǎn)擊的按鈕,放大 item.startAnimation(AnimationHelper.createPopupItemBiggerAnim(MainActivity.this)); } else { //其它按鈕,縮小 item.startAnimation(AnimationHelper.createPopupItemSmallerAnim(MainActivity.this)); } } } } //popupWindow上按鈕的點(diǎn)擊事件 private void handleEvent(int viewId) { Toast.makeText(this, "點(diǎn)擊了按鈕:" + viewId, Toast.LENGTH_SHORT).show(); } private int getActionBarHeight() { return getSupportActionBar().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(); if (id == R.id.action_more) { if (popupWindow == null || !popupWindow.isShowing()) { showPopup(); } else { dismissPopup(); } return true; } return super.onOptionsItemSelected(item); } //點(diǎn)擊返回鍵時(shí),如果popupWindow是顯示狀態(tài),則關(guān)閉它 @Override public void onBackPressed() { if (popupWindow != null && popupWindow.isShowing()) { dismissPopup(); return; } super.onBackPressed(); } }
2.動(dòng)畫(huà)工具類(lèi)。
/** * AnimationHelper */ public class AnimationHelper { /** * 進(jìn)入動(dòng)畫(huà)的時(shí)間 */ public static final int TIME_IN = 300; /** * 進(jìn)入動(dòng)畫(huà)之后的反彈動(dòng)畫(huà)時(shí)間 */ public static final int TIME_IN_BACK = 100; /** * 退出動(dòng)畫(huà)的時(shí)間 */ public static final int TIME_OUT = 300; /** * 點(diǎn)擊PopupWindow上菜單后退出動(dòng)畫(huà)的時(shí)間 */ public static final int TIME_OUT_CLICK = 500; /** * PopupWindow上菜單進(jìn)入動(dòng)畫(huà) */ public static Animation createPopupAnimIn(Context context, int fromYDelta) { AnimationSet animationSet = new AnimationSet(context, null); // animationSet.setInterpolator(new BounceInterpolator()); //結(jié)束時(shí)彈跳 animationSet.setFillAfter(true); //移動(dòng) TranslateAnimation translateAnim = new TranslateAnimation(0, 0, fromYDelta, 20); translateAnim.setDuration(TIME_IN); animationSet.addAnimation(translateAnim); //回彈效果 TranslateAnimation translateAnim2 = new TranslateAnimation(0, 0, 0, -20); translateAnim2.setStartOffset(TIME_IN); translateAnim2.setDuration(TIME_IN_BACK); animationSet.addAnimation(translateAnim2); return animationSet; } /** * PopupWindow上菜單離開(kāi)動(dòng)畫(huà) */ public static Animation createPopupAnimOut(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; } /** * PopupWindow背景進(jìn)入動(dòng)畫(huà)(透明度漸變) */ public static Animation createPopupBgFadeInAnim() { AlphaAnimation anim = new AlphaAnimation(0, 1.0f); anim.setDuration(TIME_IN); anim.setFillAfter(true); return anim; } /** * PopupWindow背景離開(kāi)動(dòng)畫(huà)(透明度漸變) */ public static Animation createPopupBgFadeOutAnim(int duration) { AlphaAnimation anim = new AlphaAnimation(1.0f, 0); anim.setDuration(duration); anim.setFillAfter(true); return anim; } /** * PopupWindow按鈕點(diǎn)擊動(dòng)畫(huà) */ public static Animation createPopupItemBiggerAnim(Context context) { AnimationSet animationSet = new AnimationSet(context, null); animationSet.setFillAfter(true); //放大(設(shè)置縮放的中心點(diǎn)為自己的中心) 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); //漸變 AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0); alphaAnim.setInterpolator(new AccelerateInterpolator()); alphaAnim.setDuration(TIME_OUT_CLICK); animationSet.addAnimation(alphaAnim); return animationSet; } /** * PopupWindow按鈕點(diǎn)擊時(shí)其它按鈕的動(dòng)畫(huà) */ public static Animation createPopupItemSmallerAnim(Context context) { //放大(設(shè)置縮放的中心點(diǎn)為自己的中心) 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; } }
以上就是Android開(kāi)發(fā)中實(shí)現(xiàn)一個(gè)彈出框的方法,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。