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

先看效果图:



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

  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. }  
package com.yuan.keyanhelper.view;

import io.vov.vitamio.activity.InitActivity;

import java.util.List;

import com.example.keyanhelper.R;
import com.example.keyanhelper.R.layout;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.AdapterView.OnItemClickListener;

public class PhotoPopupWindows extends PopupWindow {
	private View mMenuView; // PopupWindow 菜单布局
	private Context context; // 上下文参数
	private OnClickListener myOnClick; // PopupWindow 菜单 空间单击事件

	public PhotoPopupWindows(Activity context, OnClickListener myOnClick) {
		super(context);
		this.context = context;
		this.myOnClick = myOnClick;
		Init();
	}

	private void Init() {
		// TODO Auto-generated method stub
		// PopupWindow 导入
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.popitem_alter_icon, null);
		Button btn_camera = (Button) mMenuView
				.findViewById(R.id.btn_alter_pic_camera);
		Button btn_photo = (Button) mMenuView
				.findViewById(R.id.btn_alter_pic_photo);
		Button btn_cancel = (Button) mMenuView
				.findViewById(R.id.btn_alter_exit);

		btn_camera.setOnClickListener(myOnClick);
		btn_cancel.setOnClickListener(myOnClick);
		btn_photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});

		// 导入布局
		this.setContentView(mMenuView);
		// 设置动画效果
		this.setAnimationStyle(R.style.AnimationFade);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置可触
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x0000000);
		this.setBackgroundDrawable(dw);
		// 单击弹出窗以外处 关闭弹出窗
		mMenuView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int height = mMenuView.findViewById(R.id.ll_pop).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}
}

单击弹出菜单:

  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.     }  
private void showPopMenu() {
		// TODO Auto-generated method stub
		popMenus = new PhotoPopupWindows(this, myMenusOnClick);
		popMenus.showAtLocation(this.findViewById(R.id.ll_person_info),
				Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	}



动画效果:

fade_in:

  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" />  
  <translate
        android:duration="200"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

    <alpha
        android:duration="200"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

fade_out:

  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" />  
 <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />

    <alpha
        android:duration="200"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />




   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值