private View mMenuView;
private Button scene_camera_btn, scene_photo_btns, cancel_btns;
@SuppressLint("InflateParams")
public Popupwindow(Context context, OnClickListener onClickListener) {
super(context);
//加载布局文件
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Button mCamera = (Button) mMenuView.findViewById(R.id.btn_take_photo);
Button mPhoto = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
Button mCancle = (Button) mMenuView.findViewById(R.id.btn_cancel);
//设置点击事件
mCamera.setOnClickListener(onClickListener);
mCancle.setOnClickListener(this);mPhoto.setOnClickListener(onClickListener);
this.setContentView(mMenuView);
this.setWidth(LayoutParams.MATCH_PARENT);
this.setHeight(LayoutParams.MATCH_PARENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置popupwindow 动画效果
this.setAnimationStyle(R.style.popwin_anim_style);ColorDrawable dw = new ColorDrawable(0xb0000000);
this.setBackgroundDrawable(dw);
mMenuView.setOnTouchListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_cancel:
dismiss();
break;
}
}
//点击空白处消失
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
dismiss();
}
return true;
}
}
Popupwindow 使用方法
首先初始化 Popupwindow popupwindow = new Popupwindow(getActivity(), new mOnClickListener());
创建mOnClickListener监听事件
public class mOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.driver_header:
// if (!isLogin())
// return;
// popupwindow 出来的位置
popupwindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
break;
case R.id.btn_take_photo:// 拍照
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
photoUri = getActivity().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
photoUri);
startActivityForResult(intent, 2);
popupwindow.dismiss();
} else {
ToastUtil.show(getActivity(), "请确认已经插入SD卡", 0);
}
break;
case R.id.btn_pick_photo:// 相册
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent, 1);
popupwindow.dismiss();
break;
}
}
}
// popupwindowv布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="#FFFFFF"
>
<Button
android:id="@+id/btn_take_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:background="@drawable/button_norma"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_pick_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="从相册选择"
android:background="@drawable/button_norma"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
//popupwindow 动画style
<style name="popwin_anim_style">
<item name="android:windowEnterAnimation">@anim/popupwindow_show</item>
<item name="android:windowExitAnimation">@anim/popupwindow_out</item>
</style>
//popupwindow 出现和消失动画
//popupwindow_show
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="120"
android:toYDelta="0"
android:duration="500" />
</set>
//popupwindow_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="120"
android:duration="500" />
</set>