仿微信 个人头像修改 popPopupWindow实现Menus从底部弹出

来源:互联网 发布:php .net哪个好 编辑:程序博客网 时间:2024/06/05 08:25

先看效果图:



代码:自定义一个类 继承PopupWindows

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.yuan.keyanhelper.view;  
  2.   
  3. import io.vov.vitamio.activity.InitActivity;  
  4.   
  5. import java.util.List;  
  6.   
  7. import com.example.keyanhelper.R;  
  8. import com.example.keyanhelper.R.layout;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.Context;  
  12. import android.graphics.drawable.ColorDrawable;  
  13. import android.view.LayoutInflater;  
  14. import android.view.MotionEvent;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.view.View.OnTouchListener;  
  18. import android.view.ViewGroup.LayoutParams;  
  19. import android.widget.Button;  
  20. import android.widget.PopupWindow;  
  21. import android.widget.AdapterView.OnItemClickListener;  
  22.   
  23. public class PhotoPopupWindows extends PopupWindow {  
  24.     private View mMenuView; // PopupWindow 菜单布局  
  25.     private Context context; // 上下文参数  
  26.     private OnClickListener myOnClick; // PopupWindow 菜单 空间单击事件  
  27.   
  28.     public PhotoPopupWindows(Activity context, OnClickListener myOnClick) {  
  29.         super(context);  
  30.         this.context = context;  
  31.         this.myOnClick = myOnClick;  
  32.         Init();  
  33.     }  
  34.   
  35.     private void Init() {  
  36.         // TODO Auto-generated method stub  
  37.         // PopupWindow 导入  
  38.         LayoutInflater inflater = (LayoutInflater) context  
  39.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  40.         mMenuView = inflater.inflate(R.layout.popitem_alter_icon, null);  
  41.         Button btn_camera = (Button) mMenuView  
  42.                 .findViewById(R.id.btn_alter_pic_camera);  
  43.         Button btn_photo = (Button) mMenuView  
  44.                 .findViewById(R.id.btn_alter_pic_photo);  
  45.         Button btn_cancel = (Button) mMenuView  
  46.                 .findViewById(R.id.btn_alter_exit);  
  47.   
  48.         btn_camera.setOnClickListener(myOnClick);  
  49.         btn_cancel.setOnClickListener(myOnClick);  
  50.         btn_photo.setOnClickListener(new OnClickListener() {  
  51.   
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 // TODO Auto-generated method stub  
  55.                 dismiss();  
  56.             }  
  57.         });  
  58.   
  59.         // 导入布局  
  60.         this.setContentView(mMenuView);  
  61.         // 设置动画效果  
  62.         this.setAnimationStyle(R.style.AnimationFade);  
  63.         this.setWidth(LayoutParams.FILL_PARENT);  
  64.         this.setHeight(LayoutParams.WRAP_CONTENT);  
  65.         // 设置可触  
  66.         this.setFocusable(true);  
  67.         ColorDrawable dw = new ColorDrawable(0x0000000);  
  68.         this.setBackgroundDrawable(dw);  
  69.         // 单击弹出窗以外处 关闭弹出窗  
  70.         mMenuView.setOnTouchListener(new OnTouchListener() {  
  71.   
  72.             @Override  
  73.             public boolean onTouch(View v, MotionEvent event) {  
  74.                 // TODO Auto-generated method stub  
  75.                 int height = mMenuView.findViewById(R.id.ll_pop).getTop();  
  76.                 int y = (int) event.getY();  
  77.                 if (event.getAction() == MotionEvent.ACTION_UP) {  
  78.                     if (y < height) {  
  79.                         dismiss();  
  80.                     }  
  81.                 }  
  82.                 return true;  
  83.             }  
  84.         });  
  85.     }  
  86. }  

单击弹出菜单:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. private void showPopMenu() {  
  2.         // TODO Auto-generated method stub  
  3.         popMenus = new PhotoPopupWindows(this, myMenusOnClick);  
  4.         popMenus.showAtLocation(this.findViewById(R.id.ll_person_info),  
  5.                 Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 00);  
  6.     }  



动画效果:

fade_in:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <translate  
  2.       android:duration="200"  
  3.       android:fromYDelta="100%p"  
  4.       android:toYDelta="0" />  
  5.   
  6.   <alpha  
  7.       android:duration="200"  
  8.       android:fromAlpha="0.0"  
  9.       android:toAlpha="1.0" />  

fade_out:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <translate  
  2.        android:duration="200"  
  3.        android:fromYDelta="0"  
  4.        android:toYDelta="50%p" />  
  5.   
  6.    <alpha  
  7.        android:duration="200"  
  8.        android:fromAlpha="1.0"  
  9.        android:toAlpha="0.0" />  




   
0 1