popuwidow 本文通过一个弹出拍照和选择图片的popuwidow 来解释如何自定义popuwidow
首先我们需要了解一些参数的意义:
popupWindow.showAtLocation(mActivity.findViewById(R.id.pop_parent), Gravity.BOTTOM, 0, 0);在父组件的底部
popupWindow显示提供了两种形式:
showAtLocation()显示在指定位置,有两个方法重载:
public void showAtLocation(View parent, int gravity, int x, int y) public void showAtLocation(IBinder token, int gravity, int x, int y)
showAsDropDown()显示在一个参照物View的周围,有三个方法重载:
public void showAsDropDown(View anchor) public void showAsDropDown(View anchor, int xoff, int yoff) public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
自定义的popuwidow
第一步:创建
<img src="https://img-blog.youkuaiyun.com/20160106103807527?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
/**
* Created by hasee on 2015/11/28.
*/
public class PopupWindowChange implements View.OnClickListener {
@Bind(R.id.take_photo)
TextView takePhoto;
@Bind(R.id.from_album)
TextView fromAlbum;
@Bind(R.id.cancel_change)
TextView cancel;
static Activity mActivity;
static PopupWindow pop;
static Uri imageUri;
public static void showPopupWindow(Activity activity,Uri imgUri){
mActivity = activity;
imageUri =imgUri;
new PopupWindowChange();
}
private PopupWindowChange(){
View contentView = mActivity.getLayoutInflater().inflate(R.layout.popupwindow_change,null);
ButterKnife.bind(this, contentView);
initEvent();
PopupWindow popupWindow = new PopupWindow(contentView);
popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setAnimationStyle(R.style.anim_popwin_share);
popupWindow.showAtLocation(mActivity.findViewById(R.id.pop_parent), Gravity.BOTTOM, 0, 0);
WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
lp.alpha = 0.5f; //0.0-1.0
mActivity.getWindow().setAttributes(lp);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
lp.alpha = 1f; //0.0-1.0
mActivity.getWindow().setAttributes(lp);
}
});
pop = popupWindow;
}
private void initEvent(){
takePhoto.setOnClickListener(this);
fromAlbum.setOnClickListener(this);
cancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.take_photo:
pop.dismiss();
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
mActivity.startActivityForResult(takePhoto, MyInformationActivity.TAKE_PHOTO);
break;
case R.id.from_album:
pop.dismiss();
Intent fromAlbum = new Intent(Intent.ACTION_PICK,null);
fromAlbum.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
mActivity.startActivityForResult(fromAlbum,MyInformationActivity.FROM_ALBUM);
break;
case R.id.cancel_change:
pop.dismiss();
break;
}
}
}
第二步:绘制window的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/grid_thing_bg">
<TextView
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@drawable/button_text_color"
android:padding="10dp"
android:textSize="@dimen/content_size_big"
android:text="@string/take_photo"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/divide_line"/>
<TextView
android:id="@+id/from_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@drawable/button_text_color"
android:padding="10dp"
android:textSize="@dimen/content_size_big"
android:text="@string/from_album"/>
</LinearLayout>
<TextView
android:id="@+id/cancel_change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="@drawable/grid_thing_bg"
android:padding="10dp"
android:textSize="@dimen/content_size_big"
android:textColor="@color/orange"
android:text="@string/cancel"/>
</LinearLayout>
</LinearLayout>
其中用到了自定义形状的TextView和linearlayout gri_thing_bg.xml<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ffffff" /> <corners android:radius="5dp"/> </shape>
动画风格设定anim_popupwindow_share.xml<style name="anim_popwin_share"> <item name="android:windowEnterAnimation">@anim/anim_popupwindow_share</item> </style>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%" android:toYDelta="0%" android:duration="300" android:interpolator="@android:interpolator/linear"/> </set>
最后是使用 在需要用到的ACTIVITY中
protected static File file = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg"); Uri imageUri; public static final int CHANGE_NAME = 0; public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; public static final int FROM_ALBUM =3;
private void initFile(){ try { if(file.exists()){ file.delete(); } file.createNewFile(); imageUri = Uri.fromFile(file); } catch (IOException e) { e.printStackTrace(); } }
<span style="font-family: 宋体; font-size: 9pt;"> PopupWindowChange.showPopupWindow(this,imageUri);</span>
需要注意的是mActivity.startActivityForResult(fromAlbum,MyInformationActivity.FROM_ALBUM);这句代码是用来交互popupwindow 和activity 的MyInformationActivity.FROM_ALBUM 是一个定义好的INT类型的数值。返回给ACTIVITY
在acitivity中回调
protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case CHANGE_NAME: if(resultCode == RESULT_OK){ if(null == data){ return; } else { userName.setText(data.getStringExtra(ChangeNameActivity.USER_NAME)); } break; } else { showCustomToast("修改失败,请稍后再试"); break; } case TAKE_PHOTO: if(resultCode == RESULT_OK){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", true); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); } break; case FROM_ALBUM: Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(data.getData(), "image/*"); intent.putExtra("scale", true); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); break; case CROP_PHOTO: if(resultCode == RESULT_OK){ Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); headImg.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } finally { file.delete(); } break; } } }