相信大家都体验过android通讯录中的弹窗效果。如图所示:



android中提供了QuickContactBadge来实现这一效果。这里简单演示下。

首先创建布局文件:

view plain
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <QuickContactBadge
  8. android:id="@+id/badge"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:src="@drawable/icon">
  12. </QuickContactBadge>
  13. </LinearLayout>

很简单,在布局中添加一个QuickContactBadge组件即可。

在Activity中配置:

view plain
  1. publicclassQuickcontactActivityextendsActivity{
  2. /**Calledwhentheactivityisfirstcreated.*/
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. QuickContactBadgesmallBadge=(QuickContactBadge)findViewById(R.id.badge);
  8. //从email关联一个contact
  9. smallBadge.assignContactFromEmail("notice520@gmail.com",true);
  10. //设置窗口模式
  11. smallBadge.setMode(ContactsContract.QuickContact.MODE_SMALL);
  12. }
  13. }


注意加入读通讯录的权限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>  

实现效果如图:


但是这个组件局限性很大,弹出窗口中只能是一些contact操作。但是仔细一想,这样的操作并不难,不就是一个带动画的弹窗么。下面就来我们自己实现一个。

实现一个带动画的弹窗并不难,在我的之前一篇博客中有讲过弹窗PopupWindow的使用,不清楚弹窗的朋友可以去看下。在这里实现的难点主要有这些:

1.判断基准view在屏幕中的位置,从而确定弹窗弹出的位置以及动画。这是非常重要的一点,或许基准在屏幕上方,那么就要向下弹出。

2.动态的添加弹窗中的按钮,并实现点击

3.箭头位置的控制。箭头应该保持在基准的下方。

4.动画的匹配。里面有两种动画。一种是PopupWindow弹出动画,我们通过设置弹窗的style来实现(style的用法可以参考我之前的博客)。另一种是弹窗中间的布局的动画。

  了解了难点以后,写起来就方便了。

首先实现弹窗的布局:

view plain
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content">
  5. <FrameLayout
  6. android:layout_marginTop="10dip"
  7. android:id="@+id/header2"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:background="@drawable/quickcontact_top_frame"/>
  11. <ImageView
  12. android:id="@+id/arrow_up"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:src="@drawable/quickcontact_arrow_up"/>
  16. <HorizontalScrollView
  17. android:id="@+id/scroll"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:fadingEdgeLength="0dip"
  21. android:layout_below="@id/header2"
  22. android:background="@drawable/quickcontact_slider_background"
  23. android:scrollbars="none">
  24. <LinearLayout
  25. android:id="@+id/tracks"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:paddingTop="4dip"
  29. android:paddingBottom="4dip"
  30. android:orientation="horizontal">
  31. <ImageView
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:src="@drawable/quickcontact_slider_grip_left"/>
  35. <ImageView
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:src="@drawable/quickcontact_slider_grip_right"/>
  39. </LinearLayout>
  40. </HorizontalScrollView>
  41. <FrameLayout
  42. android:id="@+id/footer"
  43. android:layout_width="fill_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@id/scroll"
  46. android:background="@drawable/quickcontact_bottom_frame"/>
  47. <ImageView
  48. android:id="@+id/arrow_down"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_marginTop="-1dip"
  52. android:layout_below="@id/footer"
  53. android:src="@drawable/quickcontact_arrow_down"/>
  54. </RelativeLayout>

窗体内部使用一个HorizontalScrollView可以实现一个滑动效果。我们可以动态的在这个布局中添加按钮,我们称作Actionitem。

  写一个ActionItem类,使得我们可以用一个ArrayList做容器,动态的添加这些actionitem。这些都是服务于第二个难点。

view plain
  1. packagecom.notice.quickaction;
  2. importandroid.graphics.drawable.Drawable;
  3. importandroid.view.View.OnClickListener;
  4. /**
  5. *Actionitem,每个item里面都有一个ImageView和一个TextView
  6. */
  7. publicclassActionItem{
  8. privateDrawableicon;
  9. privateStringtitle;
  10. privateOnClickListenerlistener;
  11. /**
  12. *构造器
  13. */
  14. publicActionItem(){
  15. }
  16. /**
  17. *带Drawable参数的构造器
  18. */
  19. publicActionItem(Drawableicon){
  20. this.icon=icon;
  21. }
  22. /**
  23. *设置标题
  24. */
  25. publicvoidsetTitle(Stringtitle){
  26. this.title=title;
  27. }
  28. /**
  29. *获得标题
  30. *
  31. *@returnactiontitle
  32. */
  33. publicStringgetTitle(){
  34. returnthis.title;
  35. }
  36. /**
  37. *设置图标
  38. */
  39. publicvoidsetIcon(Drawableicon){
  40. this.icon=icon;
  41. }
  42. /**
  43. *获得图标
  44. */
  45. publicDrawablegetIcon(){
  46. returnthis.icon;
  47. }
  48. /**
  49. *绑定监听器
  50. */
  51. publicvoidsetOnClickListener(OnClickListenerlistener){
  52. this.listener=listener;
  53. }
  54. /**
  55. *获得监听器
  56. */
  57. publicOnClickListenergetListener(){
  58. returnthis.listener;
  59. }
  60. }

接下来就是这个弹窗的实现了,我们继承PopupWindow类。在这个类中我们需要实现通过位置设置动画及弹出位置,并且给出一个方法供实现类调用,来动态添加item和设置动画效果。

代码如下:

view plain
  1. /**
  2. *继承弹窗,构造我们需要的弹窗
  3. */
  4. publicclassQuickActionsextendsPopupWindow{
  5. privatefinalViewroot;
  6. privatefinalImageViewmArrowUp;
  7. privatefinalImageViewmArrowDown;
  8. privatefinalAnimationmTrackAnim;
  9. privatefinalLayoutInflaterinflater;
  10. privatefinalContextcontext;
  11. protectedfinalViewanchor;
  12. protectedfinalPopupWindowwindow;
  13. privateDrawablebackground=null;
  14. protectedfinalWindowManagerwindowManager;
  15. protectedstaticfinalintANIM_GROW_FROM_LEFT=1;
  16. protectedstaticfinalintANIM_GROW_FROM_RIGHT=2;
  17. protectedstaticfinalintANIM_GROW_FROM_CENTER=3;
  18. protectedstaticfinalintANIM_AUTO=4;
  19. privateintanimStyle;
  20. privatebooleananimateTrack;
  21. privateViewGroupmTrack;
  22. privateArrayList<ActionItem>actionList;
  23. /**
  24. *构造器,在这里初始化一些内容
  25. *
  26. *@paramanchor像我之前博客所说的理解成一个基准弹窗以此为基准弹出
  27. */
  28. publicQuickActions(Viewanchor){
  29. super(anchor);
  30. this.anchor=anchor;
  31. this.window=newPopupWindow(anchor.getContext());
  32. //在popwindow外点击即关闭该window
  33. window.setTouchInterceptor(newOnTouchListener(){
  34. @Override
  35. publicbooleanonTouch(Viewv,MotionEventevent){
  36. if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
  37. QuickActions.this.window.dismiss();
  38. returntrue;
  39. }
  40. returnfalse;
  41. }
  42. });
  43. //得到一个windowManager对象,用来得到窗口的一些属性
  44. windowManager=(WindowManager)anchor.getContext().getSystemService(Context.WINDOW_SERVICE);
  45. actionList=newArrayList<ActionItem>();
  46. context=anchor.getContext();
  47. inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  48. //装载布局,root即为弹出窗口的布局
  49. root=(ViewGroup)inflater.inflate(R.layout.quickaction,null);
  50. //得到上下两个箭头
  51. mArrowDown=(ImageView)root.findViewById(R.id.arrow_down);
  52. mArrowUp=(ImageView)root.findViewById(R.id.arrow_up);
  53. setContentView(root);
  54. mTrackAnim=AnimationUtils.loadAnimation(anchor.getContext(),R.anim.rail);
  55. //设置动画的加速效果
  56. mTrackAnim.setInterpolator(newInterpolator(){
  57. publicfloatgetInterpolation(floatt){
  58. finalfloatinner=(t*1.55f)-1.1f;
  59. return1.2f-inner*inner;
  60. }
  61. });
  62. //这个是弹出窗口内的水平布局
  63. mTrack=(ViewGroup)root.findViewById(R.id.tracks);
  64. animStyle=ANIM_AUTO;//设置动画风格
  65. animateTrack=true;
  66. }
  67. /**
  68. *设置一个flag来标识动画显示
  69. */
  70. publicvoidanimateTrack(booleananimateTrack){
  71. this.animateTrack=animateTrack;
  72. }
  73. /**
  74. *设置动画风格
  75. */
  76. publicvoidsetAnimStyle(intanimStyle){
  77. this.animStyle=animStyle;
  78. }
  79. /**
  80. *增加一个action
  81. */
  82. publicvoidaddActionItem(ActionItemaction){
  83. actionList.add(action);
  84. }
  85. /**
  86. *弹出弹窗
  87. */
  88. publicvoidshow(){
  89. //预处理,设置window
  90. preShow();
  91. int[]location=newint[2];
  92. //得到anchor的位置
  93. anchor.getLocationOnScreen(location);
  94. //以anchor的位置构造一个矩形
  95. RectanchorRect=newRect(location[0],location[1],location[0]+anchor.getWidth(),location[1]
  96. +anchor.getHeight());
  97. root.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  98. root.measure(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  99. introotWidth=root.getMeasuredWidth();
  100. introotHeight=root.getMeasuredHeight();
  101. //得到屏幕的宽
  102. intscreenWidth=windowManager.getDefaultDisplay().getWidth();
  103. //设置弹窗弹出的位置的x/y
  104. intxPos=(screenWidth-rootWidth)/2;
  105. intyPos=anchorRect.top-rootHeight;
  106. booleanonTop=true;
  107. //在底部弹出
  108. if(rootHeight>anchorRect.top){
  109. yPos=anchorRect.bottom;
  110. onTop=false;
  111. }
  112. //根据弹出位置,设置不同方向箭头图片
  113. showArrow(((onTop)?R.id.arrow_down:R.id.arrow_up),anchorRect.centerX());
  114. //设置弹出动画风格
  115. setAnimationStyle(screenWidth,anchorRect.centerX(),onTop);
  116. //创建actionlist
  117. createActionList();
  118. //在指定位置弹出弹窗
  119. window.showAtLocation(this.anchor,Gravity.NO_GRAVITY,xPos,yPos);
  120. //设置弹窗内部的水平布局的动画
  121. if(animateTrack)mTrack.startAnimation(mTrackAnim);
  122. }
  123. /**
  124. *预处理窗口
  125. */
  126. protectedvoidpreShow(){
  127. if(root==null){
  128. thrownewIllegalStateException("需要为弹窗设置布局");
  129. }
  130. //背景是唯一能确定popupwindow宽高的元素,这里使用root的背景,但是需要给popupwindow设置一个空的BitmapDrawable
  131. if(background==null){
  132. window.setBackgroundDrawable(newBitmapDrawable());
  133. }else{
  134. window.setBackgroundDrawable(background);
  135. }
  136. window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
  137. window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  138. window.setTouchable(true);
  139. window.setFocusable(true);
  140. window.setOutsideTouchable(true);
  141. //指定布局
  142. window.setContentView(root);
  143. }
  144. /**
  145. *设置动画风格
  146. *
  147. *@paramscreenWidth屏幕宽底
  148. *@paramrequestedX距离屏幕左边的距离
  149. *@paramonTop一个flag用来标识窗口的显示位置,如果为true则显示在anchor的顶部
  150. */
  151. privatevoidsetAnimationStyle(intscreenWidth,intrequestedX,booleanonTop){
  152. //取得屏幕左边到箭头中心的位置
  153. intarrowPos=requestedX-mArrowUp.getMeasuredWidth()/2;
  154. //根据animStyle设置相应动画风格
  155. switch(animStyle){
  156. caseANIM_GROW_FROM_LEFT:
  157. window.setAnimationStyle((onTop)?R.style.Animations_PopUpMenu_Left:R.style.Animations_PopDownMenu_Left);
  158. break;
  159. caseANIM_GROW_FROM_RIGHT:
  160. window.setAnimationStyle((onTop)?R.style.Animations_PopUpMenu_Right:R.style.Animations_PopDownMenu_Right);
  161. break;
  162. caseANIM_GROW_FROM_CENTER:
  163. window.setAnimationStyle((onTop)?R.style.Animations_PopUpMenu_Center:R.style.Animations_PopDownMenu_Center);
  164. break;
  165. caseANIM_AUTO:
  166. if(arrowPos<=screenWidth/4){
  167. window.setAnimationStyle((onTop)?R.style.Animations_PopUpMenu_Left:R.style.Animations_PopDownMenu_Left);
  168. }elseif(arrowPos>screenWidth/4&&arrowPos<3*(screenWidth/4)){
  169. window.setAnimationStyle((onTop)?R.style.Animations_PopUpMenu_Center:R.style.Animations_PopDownMenu_Center);
  170. }else{
  171. window.setAnimationStyle((onTop)?R.style.Animations_PopDownMenu_Right:R.style.Animations_PopDownMenu_Right);
  172. }
  173. break;
  174. }
  175. }
  176. /**
  177. *创建actionlist
  178. */
  179. privatevoidcreateActionList(){
  180. Viewview;
  181. Stringtitle;
  182. Drawableicon;
  183. OnClickListenerlistener;
  184. intindex=1;
  185. for(inti=0;i<actionList.size();i++){
  186. title=actionList.get(i).getTitle();
  187. icon=actionList.get(i).getIcon();
  188. listener=actionList.get(i).getListener();
  189. //得到actionitem
  190. view=getActionItem(title,icon,listener);
  191. view.setFocusable(true);
  192. view.setClickable(true);
  193. //将其加入布局
  194. mTrack.addView(view,index);
  195. index++;
  196. }
  197. }
  198. /**
  199. *获得actionitem
  200. *
  201. *@paramtitleaction的标题
  202. *@paramiconaction的图标
  203. *@paramlisteneraction的点击事件监听器
  204. *@returnaction的item
  205. */
  206. privateViewgetActionItem(Stringtitle,Drawableicon,OnClickListenerlistener){
  207. //装载action布局
  208. LinearLayoutcontainer=(LinearLayout)inflater.inflate(R.layout.action_item,null);
  209. ImageViewimg=(ImageView)container.findViewById(R.id.icon);
  210. TextViewtext=(TextView)container.findViewById(R.id.title);
  211. if(icon!=null){
  212. img.setImageDrawable(icon);
  213. }else{
  214. img.setVisibility(View.GONE);
  215. }
  216. if(title!=null

更多相关文章

  1. android:fitsSystemWindows
  2. TextView——文本省略显示
  3. Android(安卓)界面的基本属性
  4. 有关布局问题:TextView、EditText……(二)
  5. android 状态栏沉浸
  6. Android(安卓)OpenGL 开发
  7. 设置activity为Dialog类型的设置
  8. Android(安卓)ImageView图片显示点击背景切换
  9. 【Android开发基础】应用界面主题Theme使用方法

随机推荐

  1. 使用eclipse编写并运行你的第一个Android
  2. Android(安卓)Studio中无法找到android.o
  3. android中实现“再按一次退出”功能
  4. DELPHI XE5 FOR ANDROID 模仿驾考宝典 TM
  5. Android中xml的处理
  6. Android安全防护/检查root/检查Xposed/反
  7. Android(安卓)M 新的运行时权限开发者需
  8. Android(安卓)卡片效果
  9. 如何将当前布局用代码保存在png图像文件
  10. Android(安卓)Studio常用快捷键---不断更