版权声明:本文为博主原创文章,未经博主允许不得转载。
先看效果图:
代码:自定义一个类 继承PopupWindows
- 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;
- }
- });
- }
- }
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;
}
});
}
}
单击弹出菜单:
- 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);
- }
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:
- <translate
- android:duration="200"
- android:fromYDelta="100%p"
- android:toYDelta="0" />
- <alpha
- android:duration="200"
- android:fromAlpha="0.0"
- 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:
- <translate
- android:duration="200"
- android:fromYDelta="0"
- android:toYDelta="50%p" />
- <alpha
- android:duration="200"
- android:fromAlpha="1.0"
- 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" />