樣式
popwindow_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dip" android:color="#ffbbbbbb"/>
<corners android:topLeftRadius="15dip"
android:topRightRadius="15dip"
android:bottomLeftRadius="15dip"
android:bottomRightRadius="15dip" />
<solid android:color="@android:color/white"/>
</shape>
xml佈局:
<?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="match_parent"
android:padding="5dp"
android:orientation="vertical">
<LinearLayout
android:padding="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/popwindow_bg">
<TextView
android:id="@+id/tv_take_picture"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#919494"/>
<TextView
android:id="@+id/tv_take_video"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="錄像"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="@drawable/popwindow_bg">
<TextView
android:id="@+id/tv_cancel"
android:layout_marginTop="10dp"
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
動畫
res裏的anim文件夾裏新建bottom_popu_enter_anim、bottom_popu_out_anim.xml。
bottom_popu_enter_anim:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<translate
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="300"/>
</set>
bottom_popu_out_anim:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<translate
android:fromYDelta="0"
android:toYDelta="100%p"
android:duration="300"></translate>
</set>
style樣式BottomPopuAnim:
<!-- 底部彈框style -->
<style name="BottomPopuAnim">
<item name="android:windowEnterAnimation">@anim/bottom_popu_enter_anim</item>
<item name="android:windowExitAnimation">@anim/bottom_popu_out_anim</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
popwindow代碼:
public class ChooseFilePopwindow extends PopupWindow implements View.OnClickListener {
/** 上下文 */
private Activity mActivity;
/** 根佈局 */
private View mRootView;
private TextView mTvTakePicture;
private TextView mTvTakeVideo;
private TextView mTvCancel;
private ChooseFilePopViewClickListener mChooseFilePopViewClickListener;
public ChooseFilePopwindow(Activity activity) {
this.mActivity = activity;
initConfig(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
initView();
}
public ChooseFilePopwindow(Activity activity, int width, int height) {
this.mActivity = activity;
initConfig(width, height);
initView();
}
@SuppressLint("NewApi")
private void initConfig(int width, int height) {
this.setWidth(width);
this.setHeight(height);
this.setFocusable(true);
this.setTouchable(true);
this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
this.setOutsideTouchable(true);
this.setElevation(25f);
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//達到背景全部變暗的效果
// mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
// WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
this.setAnimationStyle(R.style.BottomPopuAnim);
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
mActivity.getWindow().setDimAmount(0f);
params.alpha = 1.0f;
mActivity.getWindow().setAttributes(params);
}
});
}
private void initView() {
mRootView = LayoutInflater.from(mActivity).inflate(R.layout.popwindow_choose_file, null, false);
mTvCancel = mRootView.findViewById(R.id.tv_cancel);
mTvTakePicture = mRootView.findViewById(R.id.tv_take_picture);
mTvTakeVideo = mRootView.findViewById(R.id.tv_take_video);
mTvCancel.setOnClickListener(this);
mTvTakePicture.setOnClickListener(this);
mTvTakeVideo.setOnClickListener(this);
setContentView(mRootView);
}
/**
* 顯示
* @param parent 關聯父佈局
*/
public void show(View parent) {
WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
params.alpha = 0.7f;
mActivity.getWindow().setDimAmount(0.7f);
mActivity.getWindow().setAttributes(params);
this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_take_picture:
dismiss();
if(mChooseFilePopViewClickListener != null){
mChooseFilePopViewClickListener.takePicture();
}
break;
case R.id.tv_take_video:
dismiss();
if(mChooseFilePopViewClickListener != null){
mChooseFilePopViewClickListener.takeVideo();
}
break;
case R.id.tv_cancel:
dismiss();
break;
}
}
public void setChooseFilePopViewClickListener(ChooseFilePopViewClickListener mChooseFilePopViewClickListener) {
this.mChooseFilePopViewClickListener = mChooseFilePopViewClickListener;
}
public interface ChooseFilePopViewClickListener{
public void takePicture();
public void takeVideo();
}
}
使用
private ChooseFilePopwindow mChooseFilePopwindow;
private void showChooseFilePopwindow(){
if(mChooseFilePopwindow == null){
mChooseFilePopwindow = new ChooseFilePopwindow(mActivity);
mChooseFilePopwindow.setChooseFilePopViewClickListener(new ChooseFilePopwindow.ChooseFilePopViewClickListener() {
@Override
public void takePicture() {
}
@Override
public void takeVideo() {
}
});
}
if(mChooseFilePopwindow.isShowing()){
mChooseFilePopwindow.dismiss();
}
mChooseFilePopwindow.show(mRootView);
}