android UI進階之彈窗的使用(2)--實現通訊錄的彈窗效果

相信大家都體驗過android通訊錄中的彈窗效果。如圖所示:

 

android中提供了QuickContactBadge來實現這一效果。這裏簡單演示下。

首先創建佈局文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns: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中配置:

  1. public class QuickcontactActivity extends Activity {  
  2.  
  3.     /** Called when the activity is first created. */ 
  4.     @Override 
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.  
  9.         QuickContactBadge smallBadge = (QuickContactBadge) findViewById(R.id.badge);  
  10.         // 從email關聯一個contact  
  11.         smallBadge.assignContactFromEmail("[email protected]"true);  
  12.         // 設置窗口模式  
  13.         smallBadge.setMode(ContactsContract.QuickContact.MODE_SMALL);  
  14.     }  

注意加入讀通訊錄的權限

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

實現效果如圖:

但是這個組件侷限性很大,彈出窗口中只能是一些contact操作。但是仔細一想,這樣的操作並不難,不就是一個帶動畫的彈窗麼。下面就來我們自己實現一個。

實現一個帶動畫的彈窗並不難,在我的之前一篇博客中有講過彈窗PopupWindow的使用,不清楚彈窗的朋友可以去看下。在這裏實現的難點主要有這些:

1.判斷基準view在屏幕中的位置,從而確定彈窗彈出的位置以及動畫。這是非常重要的一點,或許基準在屏幕上方,那麼就要向下彈出。

2.動態的添加彈窗中的按鈕,並實現點擊

3.箭頭位置的控制。箭頭應該保持在基準的下方。

4.動畫的匹配。裏面有兩種動畫。一種是PopupWindow彈出動畫,我們通過設置彈窗的style來實現(style的用法可以參考我之前的博客)。另一種是彈窗中間的佈局的動畫。

瞭解了難點以後,寫起來就方便了。

首先實現彈窗的佈局:

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

窗體內部使用一個HorizontalScrollView可以實現一個滑動效果。我們可以動態的在這個佈局中添加按鈕,我們稱作Actionitem。

寫一個ActionItem類,使得我們可以用一個ArrayList做容器,動態的添加這些actionitem。這些都是服務於第二個難點。

 

  1. package com.notice.quickaction;  
  2.  
  3. import android.graphics.drawable.Drawable;  
  4. import android.view.View.OnClickListener;  
  5.  
  6. /**  
  7.  * Action item, 每個item裏面都有一個ImageView和一個TextView  
  8.  */ 
  9. public class ActionItem {  
  10.  
  11.     private Drawable        icon;  
  12.     private String          title;  
  13.     private OnClickListener listener;  
  14.  
  15.     /**  
  16.      * 構造器  
  17.      */ 
  18.     public ActionItem() {  
  19.     }  
  20.  
  21.     /**  
  22.      * 帶Drawable參數的構造器  
  23.      */ 
  24.     public ActionItem(Drawable icon) {  
  25.         this.icon = icon;  
  26.     }  
  27.  
  28.     /**  
  29.      * 設置標題  
  30.      */ 
  31.     public void setTitle(String title) {  
  32.         this.title = title;  
  33.     }  
  34.  
  35.     /**  
  36.      * 獲得標題  
  37.      *   
  38.      * @return action title  
  39.      */ 
  40.     public String getTitle() {  
  41.         return this.title;  
  42.     }  
  43.  
  44.     /**  
  45.      * 設置圖標  
  46.      */ 
  47.     public void setIcon(Drawable icon) {  
  48.         this.icon = icon;  
  49.     }  
  50.  
  51.     /**  
  52.      * 獲得圖標  
  53.      */ 
  54.     public Drawable getIcon() {  
  55.         return this.icon;  
  56.     }  
  57.  
  58.     /**  
  59.      * 綁定監聽器  
  60.      */ 
  61.     public void setOnClickListener(OnClickListener listener) {  
  62.         this.listener = listener;  
  63.     }  
  64.  
  65.     /**  
  66.      * 獲得監聽器  
  67.      */ 
  68.     public OnClickListener getListener() {  
  69.         return this.listener;  
  70.     }  

接下來就是這個彈窗的實現了,我們繼承PopupWindow類。在這個類中我們需要實現通過位置設置動畫及彈出位置,並且給出一個方法供實現類調用,來動態添加item和設置動畫效果。

代碼如下:

 

  1. package com.notice.quickaction;  
  2.  
  3. import java.util.ArrayList;  
  4.  
  5. import android.content.Context;  
  6. import android.graphics.Rect;  
  7. import android.graphics.drawable.BitmapDrawable;  
  8. import android.graphics.drawable.Drawable;  
  9. import android.view.Gravity;  
  10. import android.view.LayoutInflater;  
  11. import android.view.MotionEvent;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.view.View.OnTouchListener;  
  15. import android.view.ViewGroup;  
  16. import android.view.ViewGroup.LayoutParams;  
  17. import android.view.WindowManager;  
  18. import android.view.animation.Animation;  
  19. import android.view.animation.AnimationUtils;  
  20. import android.view.animation.Interpolator;  
  21. import android.widget.ImageView;  
  22. import android.widget.LinearLayout;  
  23. import android.widget.PopupWindow;  
  24. import android.widget.TextView;  
  25.  
  26. /**  
  27.  * 繼承彈窗,構造我們需要的彈窗  
  28.  */ 
  29. public class QuickActions extends PopupWindow {  
  30.  
  31.     private final View            root;  
  32.     private final ImageView       mArrowUp;  
  33.     private final ImageView       mArrowDown;  
  34.     private final Animation       mTrackAnim;  
  35.     private final LayoutInflater  inflater;  
  36.     private final Context         context;  
  37.  
  38.     protected final View          anchor;  
  39.     protected final PopupWindow   window;  
  40.     private Drawable              background            = null;  
  41.     protected final WindowManager windowManager;  
  42.  
  43.     protected static final int    ANIM_GROW_FROM_LEFT   = 1;  
  44.     protected static final int    ANIM_GROW_FROM_RIGHT  = 2;  
  45.     protected static final int    ANIM_GROW_FROM_CENTER = 3;  
  46.     protected static final int    ANIM_AUTO             = 4;  
  47.  
  48.     private int                   animStyle;  
  49.     private boolean               animateTrack;  
  50.     private ViewGroup             mTrack;  
  51.     private ArrayList<ActionItem> actionList;  
  52.  
  53.     /**  
  54.      * 構造器,在這裏初始化一些內容  
  55.      *   
  56.      * @param anchor 像我之前博客所說的理解成一個基準 彈窗以此爲基準彈出  
  57.      */ 
  58.     public QuickActions(View anchor) {  
  59.         super(anchor);  
  60.  
  61.         this.anchor = anchor;  
  62.         this.window = new PopupWindow(anchor.getContext());  
  63.  
  64.         // 在popwindow外點擊即關閉該window  
  65.         window.setTouchInterceptor(new OnTouchListener() {  
  66.  
  67.             @Override 
  68.             public boolean onTouch(View v, MotionEvent event) {  
  69.                 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {  
  70.                     QuickActions.this.window.dismiss();  
  71.  
  72.                     return true;  
  73.                 }  
  74.  
  75.                 return false;  
  76.             }  
  77.         });  
  78.  
  79.         // 得到一個windowManager對象,用來得到窗口的一些屬性  
  80.         windowManager = (WindowManager) anchor.getContext().getSystemService(Context.WINDOW_SERVICE);  
  81.  
  82.         actionList = new ArrayList<ActionItem>();  
  83.         context = anchor.getContext();  
  84.         inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  85.  
  86.         // 裝載佈局,root即爲彈出窗口的佈局  
  87.         root = (ViewGroup) inflater.inflate(R.layout.quickaction, null);  
  88.  
  89.         // 得到上下兩個箭頭  
  90.         mArrowDown = (ImageView) root.findViewById(R.id.arrow_down);  
  91.         mArrowUp = (ImageView) root.findViewById(R.id.arrow_up);  
  92.  
  93.         setContentView(root);  
  94.  
  95.         mTrackAnim = AnimationUtils.loadAnimation(anchor.getContext(), R.anim.rail);  
  96.  
  97.         // 設置動畫的加速效果  
  98.         mTrackAnim.setInterpolator(new Interpolator() {  
  99.  
  100.             public float getInterpolation(float t) {  
  101.  
  102.                 final float inner = (t * 1.55f) - 1.1f;  
  103.  
  104.                 return 1.2f - inner * inner;  
  105.             }  
  106.         });  
  107.  
  108.         // 這個是彈出窗口內的水平佈局  
  109.         mTrack = (ViewGroup) root.findViewById(R.id.tracks);  
  110.         animStyle = ANIM_AUTO;// 設置動畫風格  
  111.         animateTrack = true;  
  112.     }  
  113.  
  114.     /**  
  115.      * 設置一個flag來標識動畫顯示  
  116.      */ 
  117.     public void animateTrack(boolean animateTrack) {  
  118.         this.animateTrack = animateTrack;  
  119.     }  
  120.  
  121.     /**  
  122.      * 設置動畫風格  
  123.      */ 
  124.     public void setAnimStyle(int animStyle) {  
  125.         this.animStyle = animStyle;  
  126.     }  
  127.  
  128.     /**  
  129.      * 增加一個action  
  130.      */ 
  131.     public void addActionItem(ActionItem action) {  
  132.         actionList.add(action);  
  133.     }  
  134.  
  135.     /**  
  136.      * 彈出彈窗  
  137.      */ 
  138.     public void show() {  
  139.         // 預處理,設置window  
  140.         preShow();  
  141.  
  142.         int[] location = new int[2];  
  143.         // 得到anchor的位置  
  144.         anchor.getLocationOnScreen(location);  
  145.  
  146.         // 以anchor的位置構造一個矩形  
  147.         Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1]  
  148.                                                                                               + anchor.getHeight());  
  149.  
  150.         root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  151.         root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  152.  
  153.         int rootWidth = root.getMeasuredWidth();  
  154.         int rootHeight = root.getMeasuredHeight();  
  155.  
  156.         // 得到屏幕的寬  
  157.         int screenWidth = windowManager.getDefaultDisplay().getWidth();  
  158.  
  159.         // 設置彈窗彈出的位置的x/y  
  160.         int xPos = (screenWidth - rootWidth) / 2;  
  161.         int yPos = anchorRect.top - rootHeight;  
  162.  
  163.         boolean onTop = true;  
  164.  
  165.         // 在底部彈出  
  166.         if (rootHeight > anchorRect.top) {  
  167.             yPos = anchorRect.bottom;  
  168.             onTop = false;  
  169.         }  
  170.  
  171.         // 根據彈出位置,設置不同方向箭頭圖片  
  172.         showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), anchorRect.centerX());  
  173.  
  174.         // 設置彈出動畫風格  
  175.         setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);  
  176.  
  177.         // 創建action list  
  178.         createActionList();  
  179.  
  180.         // 在指定位置彈出彈窗  
  181.         window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);  
  182.  
  183.         // 設置彈窗內部的水平佈局的動畫  
  184.         if (animateTrack) mTrack.startAnimation(mTrackAnim);  
  185.     }  
  186.  
  187.     /**  
  188.      * 預處理窗口  
  189.      */ 
  190.     protected void preShow() {  
  191.         if (root == null) {  
  192.             throw new IllegalStateException("需要爲彈窗設置佈局");  
  193.         }  
  194.  
  195.         // 背景是唯一能確定popupwindow寬高的元素,這裏使用root的背景,但是需要給popupwindow設置一個空的BitmapDrawable  
  196.         if (background == null) {  
  197.             window.setBackgroundDrawable(new BitmapDrawable());  
  198.         } else {  
  199.             window.setBackgroundDrawable(background);  
  200.         }  
  201.  
  202.         window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);  
  203.         window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);  
  204.         window.setTouchable(true);  
  205.         window.setFocusable(true);  
  206.         window.setOutsideTouchable(true);  
  207.         // 指定佈局  
  208.         window.setContentView(root);  
  209.     }  
  210.  
  211.     /**  
  212.      * 設置動畫風格  
  213.      *   
  214.      * @param screenWidth 屏幕寬底  
  215.      * @param requestedX 距離屏幕左邊的距離  
  216.      * @param onTop 一個flag用來標識窗口的顯示位置,如果爲true則顯示在anchor的頂部  
  217.      */ 
  218.     private void setAnimationStyle(int screenWidth, int requestedX, boolean onTop) {  
  219.         // 取得屏幕左邊到箭頭中心的位置  
  220.         int arrowPos = requestedX - mArrowUp.getMeasuredWidth() / 2;  
  221.         // 根據animStyle設置相應動畫風格  
  222.         switch (animStyle) {  
  223.             case ANIM_GROW_FROM_LEFT:  
  224.                 window.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);  
  225.                 break;  
  226.  
  227.             case ANIM_GROW_FROM_RIGHT:  
  228.                 window.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right : R.style.Animations_PopDownMenu_Right);  
  229.                 break;  
  230.  
  231.             case ANIM_GROW_FROM_CENTER:  
  232.                 window.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);  
  233.                 break;  
  234.  
  235.             case ANIM_AUTO:  
  236.                 if (arrowPos <= screenWidth / 4) {  
  237.                     window.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);  
  238.                 } else if (arrowPos > screenWidth / 4 && arrowPos < 3 * (screenWidth / 4)) {  
  239.                     window.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);  
  240.                 } else {  
  241.                     window.setAnimationStyle((onTop) ? R.style.Animations_PopDownMenu_Right : R.style.Animations_PopDownMenu_Right);  
  242.                 }  
  243.  
  244.                 break;  
  245.         }  
  246.     }  
  247.  
  248.     /**  
  249.      * 創建action list  
  250.      */ 
  251.     private void createActionList() {  
  252.         View view;  
  253.         String title;  
  254.         Drawable icon;  
  255.         OnClickListener listener;  
  256.         int index = 1;  
  257.  
  258.         for (int i = 0; i < actionList.size(); i++) {  
  259.             title = actionList.get(i).getTitle();  
  260.             icon = actionList.get(i).getIcon();  
  261.             listener = actionList.get(i).getListener();  
  262.             // 得到action item  
  263.             view = getActionItem(title, icon, listener);  
  264.  
  265.             view.setFocusable(true);  
  266.             view.setClickable(true);  
  267.  
  268.             // 將其加入佈局  
  269.             mTrack.addView(view, index);  
  270.  
  271.             index++;  
  272.         }  
  273.     }  
  274.  
  275.     /**  
  276.      * 獲得 action item  
  277.      *   
  278.      * @param title action的標題  
  279.      * @param icon action的圖標  
  280.      * @param listener action的點擊事件監聽器  
  281.      * @return action的item  
  282.      */ 
  283.     private View getActionItem(String title, Drawable icon, OnClickListener listener) {  
  284.         // 裝載action佈局  
  285.         LinearLayout container = (LinearLayout) inflater.inflate(R.layout.action_item, null);  
  286.         ImageView img = (ImageView) container.findViewById(R.id.icon);  
  287.         TextView text = (TextView) container.findViewById(R.id.title);  
  288.  
  289.         if (icon != null) {  
  290.             img.setImageDrawable(icon);  
  291.         } else {  
  292.             img.setVisibility(View.GONE);  
  293.         }  
  294.  
  295.         if (title != null) {  
  296.             text.setText(title);  
  297.         } else {  
  298.             text.setVisibility(View.GONE);  
  299.         }  
  300.  
  301.         if (listener != null) {  
  302.             container.setOnClickListener(listener);  
  303.         }  
  304.  
  305.         return container;  
  306.     }  
  307.  
  308.     /**  
  309.      * 顯示箭頭  
  310.      *   
  311.      * @param 箭頭資源id  
  312.      * @param 距離屏幕左邊的距離  
  313.      */ 
  314.     private void showArrow(int whichArrow, int requestedX) {  
  315.         final View showArrow = (whichArrow == R.id.arrow_up) ? mArrowUp : mArrowDown;  
  316.         final View hideArrow = (whichArrow == R.id.arrow_up) ? mArrowDown : mArrowUp;  
  317.  
  318.         final int arrowWidth = mArrowUp.getMeasuredWidth();  
  319.  
  320.         showArrow.setVisibility(View.VISIBLE);  
  321.  
  322.         ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams) showArrow.getLayoutParams();  
  323.  
  324.         // 以此設置距離左邊的距離  
  325.         param.leftMargin = requestedX - arrowWidth / 2;  
  326.  
  327.         hideArrow.setVisibility(View.INVISIBLE);  
  328.     }  
  329.  

有點長,不過註釋都寫的很清楚了。show()方法完成窗口的彈出。裏面調用其他方法設置了窗口彈出的位置,設置了相應的動畫彈出風格和箭頭朝向以及位置,創建了action item。大家可以從這個方法裏開始看,看每個的實現。

最後寫個測試類。放一個Button在屏幕頂部,一個在屏幕底部。點擊彈出彈窗。

  1. package com.notice.quickaction;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.Toast;  
  9.  
  10. /**  
  11.  * 實現activity  
  12.  */ 
  13. public class MyQuick extends Activity {  
  14.  
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.  
  19.         setContentView(R.layout.main);  
  20.  
  21.         // 得到一個actionItem對象  
  22.         final ActionItem chart = new ActionItem();  
  23.  
  24.         // 設置標題,圖標,點擊事件  
  25.         chart.setTitle("Chart");  
  26.         chart.setIcon(getResources().getDrawable(R.drawable.chart));  
  27.         chart.setOnClickListener(new OnClickListener() {  
  28.  
  29.             @Override 
  30.             public void onClick(View v) {  
  31.                 Toast.makeText(MyQuick.this"Chart selected", Toast.LENGTH_SHORT).show();  
  32.             }  
  33.         });  
  34.  
  35.         final ActionItem production = new ActionItem();  
  36.  
  37.         production.setTitle("Products");  
  38.         production.setIcon(getResources().getDrawable(R.drawable.production));  
  39.         production.setOnClickListener(new OnClickListener() {  
  40.  
  41.             @Override 
  42.             public void onClick(View v) {  
  43.                 Toast.makeText(MyQuick.this"Products selected", Toast.LENGTH_SHORT).show();  
  44.             }  
  45.         });  
  46.  
  47.         Button btn1 = (Button) this.findViewById(R.id.btn1);  
  48.         // 點擊按鈕彈出  
  49.         btn1.setOnClickListener(new View.OnClickListener() {  
  50.  
  51.             @Override 
  52.             public void onClick(View v) {  
  53.                 // 初始化一個QuickActions  
  54.                 QuickActions qa = new QuickActions(v);  
  55.                 // 爲他添加actionitem  
  56.                 qa.addActionItem(chart);  
  57.                 qa.addActionItem(production);  
  58.                 qa.addActionItem(production);  
  59.                 qa.addActionItem(production);  
  60.                 // 設置動畫風格  
  61.                 qa.setAnimStyle(QuickActions.ANIM_AUTO);  
  62.  
  63.                 qa.show();  
  64.             }  
  65.         });  
  66.  
  67.         final ActionItem dashboard = new ActionItem();  
  68.  
  69.         dashboard.setIcon(getResources().getDrawable(R.drawable.dashboard));  
  70.         dashboard.setOnClickListener(new OnClickListener() {  
  71.  
  72.             @Override 
  73.             public void onClick(View v) {  
  74.                 Toast.makeText(MyQuick.this"dashboard selected", Toast.LENGTH_SHORT).show();  
  75.             }  
  76.         });  
  77.  
  78.         final ActionItem users = new ActionItem();  
  79.  
  80.         users.setIcon(getResources().getDrawable(R.drawable.users));  
  81.         users.setOnClickListener(new OnClickListener() {  
  82.  
  83.             @Override 
  84.             public void onClick(View v) {  
  85.                 Toast.makeText(MyQuick.this"Products selected", Toast.LENGTH_SHORT).show();  
  86.             }  
  87.         });  
  88.  
  89.         Button btn2 = (Button) this.findViewById(R.id.btn2);  
  90.         btn2.setOnClickListener(new OnClickListener() {  
  91.  
  92.             @Override 
  93.             public void onClick(View v) {  
  94.                 QuickActions qa = new QuickActions(v);  
  95.  
  96.                 qa.addActionItem(dashboard);  
  97.                 qa.addActionItem(users);  
  98.                 qa.setAnimStyle(QuickActions.ANIM_GROW_FROM_CENTER);  
  99.  
  100.                 qa.show();  
  101.             }  
  102.         });  
  103.     }  

再講下PopupWindow的風格的實現。其中一個風格代碼如下:

  1. <style name="Animations.PopDownMenu.Left"> 
  2.         <item name="@android:windowEnterAnimation">@anim/grow_from_topleft_to_bottomright</item> 
  3.         <item name="@android:windowExitAnimation">@anim/shrink_from_bottomright_to_topleft</item> 
  4.     </style> 

寫兩個item,分別實現彈出和消失動畫。因爲篇幅有限(好像已經很長了。。。),就不全部貼出來了。動畫都是一個scale加一個alpha,對動畫不熟悉的朋友可以自己研究下,從底部彈出的動畫文件grow_from_bottom.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.     <scale 
  4.         android:fromXScale="0.3" android:toXScale="1.0" 
  5.         android:fromYScale="0.3" android:toYScale="1.0" 
  6.         android:pivotX="50%" android:pivotY="100%" 
  7.         android:duration="@android:integer/config_shortAnimTime" 
  8.     /> 
  9.     <alpha 
  10.         android:interpolator="@android:anim/decelerate_interpolator" 
  11.         android:fromAlpha="0.0" android:toAlpha="1.0" 
  12.         android:duration="@android:integer/config_shortAnimTime" 
  13.     /> 
  14. </set> 

最後來看看實現效果:

 

 

好了 希望大家喜歡   有問題可以留言交流~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章