我们写项目的时候又是要自定义弹出美观的悬浮操作窗口,这里我们可以通过popUpWindow来时现:以下是点击按钮弹出popUpWindow用于选择本地上传图片的选择操作:
LayoutInflater mLayoutInflater = (LayoutInflater) activity
.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
View headwindow = mLayoutInflater.inflate(你要显示几界面的ID(R.id.picchoose), null);
PopupWindow mPopupWindow = new PopupWindow(headwindow,
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
mPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
headalbum = (Button) headwindow.findViewById(R.id.button_headalbum);
headcamera = (Button) headwindow.findViewById(R.id.button_headcamera);
headquit = (Button) headwindow.findViewById(R.id.button_headquit);
headalbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 选择相册的图片
Intent mIntent1 = new Intent(Intent.ACTION_GET_CONTENT);
mIntent1.addCategory(Intent.CATEGORY_OPENABLE);
mIntent1.setType("image/*");
activity.startActivityForResult(mIntent1, ApplicationConstant.FROM_GALLERY);
mPopupWindow.dismiss();
}
});
headcamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 选择照相图片
Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
// 获取这个图片的URI
File photofile = new File(photopath);
Uri originalUri = Uri.fromFile(photofile);// 这是个实例变量,方便下面获取图片的时候用
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
activity.startActivityForResult(mIntent, ApplicationConstant.FROM_CAMERA);
mPopupWindow.dismiss();
}
});
headquit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mPopupWindow.dismiss();
}
});
xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#a0000000"
android:gravity="center_horizontal"
android:paddingTop="15dip" android:paddingBottom = "25dip"
>
<Button android:id="@+id/button_headcamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_headcamera"/>
<Button android:id="@+id/button_headalbum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_headalbum"
android:layout_marginTop = "5px"/>
<Button android:id="@+id/button_headquit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_headquit"
android:layout_marginTop = "5px"/>
</LinearLayout>
当然界面你也可以定义成其他的方式这个靠你自由的发挥了。
调用本地的图库后可以在activity中重新写onActivityResult这个方法来获取你选择的图片