微信右上角的操作菜单看起来很好用,就照着仿了一下,不过是旧版微信的,手里刚好有一些旧版微信的资源图标,给大家分享一下。

      不知道微信是用什么实现的,我使用popupwindow来实现,主要分为几块内容:

   1、窗口布局文件:popwin_share.xml

 

Java代码  

android 仿微信裁图 android仿微信源码_xml

1. <?xml version="1.0" encoding="utf-8"?>  
2. <LinearLayout xmlns:android="http:///apk/res/android"  
3. "wrap_content"  
4. "wrap_content"  
5. "@drawable/title_tools_bg"  
6. "vertical" >  
7.   
8.     <LinearLayout  
9. "@+id/layout_share"  
10. "@style/fill_width"  
11. "horizontal"   
12. "@drawable/menu_left_item_selector"  
13. "5dp">  
14.   
15.         <ImageView  
16. "7dp"  
17. "@style/wrap"  
18. "fitCenter"  
19. "@drawable/share" />  
20.   
21.         <TextView  
22. "@style/wrap"  
23. "@color/white"  
24. "@dimen/text18"  
25. "5dp"  
26. "分享内容" />  
27.     </LinearLayout>  
28.       
29.     <LinearLayout  
30. "@+id/layout_copy"  
31. "@style/fill_width"  
32. "horizontal"   
33. "@drawable/menu_left_item_selector"  
34. "5dp">  
35.   
36.         <ImageView  
37. "5dp"  
38. "@style/wrap"  
39. "fitCenter"  
40. "@drawable/copy_pressed" />  
41.   
42.         <TextView  
43. "@style/wrap"  
44. "@color/white"  
45. "@dimen/text18"  
46. "5dp"  
47. "复制结果" />  
48.     </LinearLayout>  
49.   
50. </LinearLayout>

      采用线性布局,因为里面是一行一行竖排的菜单,线性布局更容易控制。大布局里面放了两个垂直排列的线性布局,每个线性布局中分别有横向排列的imageview和textview,很简单的布局。大布局的背景用了一个图片,当然也可以自定义一些其他颜色。

 

    2、popupwindow代码,我这里是自定义一个popupwindows类,继承自PopupWindow:

 

Java代码  

android 仿微信裁图 android仿微信源码_xml

1. package com.xjw.view;  
2.   
3. import com.xjw.translate.R;  
4.   
5. import .Activity;  
6. import android.graphics.drawable.ColorDrawable;  
7. import android.view.LayoutInflater;  
8. import android.view.View;  
9. import android.widget.AdapterView;  
10. import android.widget.BaseAdapter;  
11. import android.widget.LinearLayout;  
12. import android.widget.ListView;  
13. import android.widget.PopupWindow;  
14. /**       
15.  * 项目名称:translate   
16.  * 实现功能:  翻译详情界面,分享弹出窗口 
17.  * 类名称:PopWinShare    
18.  * 类描述:(该类的主要功能) 
19.  * 创建人:徐纪伟  
20.  * E-mail: xujiwei558@126.com 
21.  * 创建时间:2014年10月18日 下午4:37:25    
22.  * 修改人:    
23.  * 修改时间:    
24.  * 修改备注:    
25.  * @version     
26.  */  
27. public class PopWinShare extends PopupWindow{  
28. private View mainView;  
29. private LinearLayout layoutShare, layoutCopy;  
30.   
31. public PopWinShare(Activity paramActivity, View.OnClickListener paramOnClickListener, int paramInt1, int paramInt2){  
32. super(paramActivity);  
33. //窗口布局  
34. null);  
35. //分享布局  
36.         layoutShare = ((LinearLayout)mainView.findViewById(.layout_share));  
37. //复制布局  
38.         layoutCopy = (LinearLayout)mainView.findViewById(.layout_copy);  
39. //设置每个子布局的事件监听器  
40. if (paramOnClickListener != null){  
41.             layoutShare.setOnClickListener(paramOnClickListener);  
42.             layoutCopy.setOnClickListener(paramOnClickListener);  
43.         }  
44.         setContentView(mainView);  
45. //设置宽度  
46.         setWidth(paramInt1);  
47. //设置高度  
48.         setHeight(paramInt2);  
49. //设置显示隐藏动画  
50.         setAnimationStyle(R.style.AnimTools);  
51. //设置背景透明  
52. new ColorDrawable(0));  
53.     }  
54. }

       里面提供了一个构造方法,包含四个参数,第一个参数是上下文的activity,第二个是菜单的点击事件,从外边传递进来的,要绑定给每一行的菜单,具体的事件实现当然要写在activity中,后面两个分别是弹出窗口的宽度和高度。里面还包含了一个动画样式,窗口打开和关闭时出现动画的样式。

 

    3、动画样式,显示动画,缩放动画:push_in.xml

 

Xml代码  

android 仿微信裁图 android仿微信源码_xml

1. <?xml version="1.0" encoding="utf-8"?>  
2. <scale   xmlns:android="http:///apk/res/android"  
3. android:interpolator="@android:anim/accelerate_decelerate_interpolator"    
4. android:fromXScale="1.0"     
5. android:toXScale="1.0"     
6. android:fromYScale="0"     
7. android:toYScale="1.0"     
8. android:pivotX="0"    
9. android:pivotY="10%"    
10. android:duration="200" />

           关闭动画,也是缩放动画:push_out.xml

 

 

Xml代码  

android 仿微信裁图 android仿微信源码_xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2.   
    3. <scale   xmlns:android="http:///apk/res/android"  
    4. android:interpolator="@android:anim/accelerate_decelerate_interpolator"    
    5. android:fromXScale="1.0"     
    6. android:toXScale="1.0"     
    7. android:fromYScale="1.0"     
    8. android:toYScale="0"     
    9. android:pivotX="0"    
    10. android:pivotY="10%"    
    11. android:duration="200" />   
            style样式定义:

     

     

    Xml代码  

    android 仿微信裁图 android仿微信源码_xml

    1. <style name="AnimTools" parent="@android:style/Animation">  
    2. <item name="android:windowEnterAnimation">@anim/push_in</item>  
    3. <item name="android:windowExitAnimation">@anim/push_out</item>  
    4. </style>

         到此为止我们的自定义窗口已经定义好了。接下来看使用。

     

    Java代码  

    android 仿微信裁图 android仿微信源码_xml

    1. if (popWinShare == null) {  
    2. //自定义的单击事件  
    3. new OnClickLintener();  
    4. new PopWinShare(TranslateDataContentActivity.this, paramOnClickListener, DisplayUtil.dip2px(context, 160), DisplayUtil.dip2px(context, 160));  
    5. //监听窗口的焦点事件,点击窗口外面则取消显示  
    6. new View.OnFocusChangeListener() {  
    7.           
    8. @Override  
    9. public void onFocusChange(View v, boolean hasFocus) {  
    10. if (!hasFocus) {  
    11.                 popWinShare.dismiss();  
    12.             }  
    13.         }  
    14.     });  
    15. }  
    16. //设置默认获取焦点  
    17. popWinShare.setFocusable(true);  
    18. //以某个控件的x和y的偏移量位置开始显示窗口  
    19. popWinShare.showAsDropDown(btnTools, 0, 0);  
    20. //如果窗口存在,则更新  
    21. popWinShare.update();

          每个子菜单的单击事件自定义内部类,在里面就可以写每个子菜单的单击事件啦,

    Java代码  

    android 仿微信裁图 android仿微信源码_xml

    1. class OnClickLintener implements OnClickListener{  
    2.   
    3. @Override  
    4. public void onClick(View v) {  
    5. switch (v.getId()) {  
    6. case .layout_share:  
    7.                   
    8. break;  
    9. case .layout_copy:  
    10.                   
    11. break;  
    12.                     
    13. default:  
    14. break;  
    15.             }  
    16.               
    17.         }  
    18.           
    19.     }

         效果预览:

         



    android 仿微信裁图 android仿微信源码_自定义_08


     

     

       

        部分图像资源