引言
上一期我们使用PopupWindow实现了简单的封装使用,本期就其封装类进一步添加渐变阴影效果。实现点击呼出弹出框,除弹出框外,整个activity的布局逐渐变暗,关闭弹出框,布局逐渐恢复原色。
介绍
PopupWindow用来做弹出框并不是独一份,一般系统使用的是另一种,我们很多软件的消息通知其实都是用AlertDialog,但PopupWindow好就好在它足够灵活,可以适应很多自定义的弹出框环境。
传送门
第一篇:简单封装使用PopupWindow
第二篇:封装使用PopupWindow渐变阴影效果
本期效果预览
PopupWindow第二期.jpg
用法
第一步:布局文件
(1)主布局:activity_case22.xml(有改动)
说明:我简单添加了一个居中的按钮点击事件来处理弹出popupwindow。
改动:对控件背景色进行了调整。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/case22_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".show.Case22"
android:background="@color/blue"
tools:ignore="MissingConstraints">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="@color/white"
android:background="@color/green"
app:title="PopupWindow使用" />
<Button
android:id="@+id/show_popupWindow"
android:layout_width="match_parent"
android:textAllCaps="false"
android:layout_height="wrap_content"
android:text="弹出PopupWindow"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
(2)子布局:popup_select_layout.xml(有改动)
说明:这里是比较常用的弹出框,拍照+相册+取消,View用作分割线。
改动:对控件背景和分割线颜色进行了调整。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent">
<Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/gray"
android:gravity="center"
android:text="拍照"
android:textColor="@color/black"
android:textSize="20sp" />
<View
android:id="@+id/divide1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/blue"
app:layout_constraintTop_toBottomOf="@id/take_photo" />
<Button
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/gray"
android:gravity="center"
android:text="相册"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/divide1" />
<View
android:id="@+id/divide2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/blue"
app:layout_constraintTop_toBottomOf="@id/picture" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/gray"
android:gravity="center"
android:text="取消"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/divide2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
第二步:创建TestPopupWindow类(有改动)
继承自PopupWindow类,并且重写其方法。
改动:对比前期增加了弹出框后增加渐变阴影的效果。呼出渐变出现阴影效果,点击消失渐变消失阴影效果。
/**
* @data on 2020/9/14 9:18 AM
* @auther
* @describe
*/
public class TestPopupWindow extends PopupWindow {
TestDataListener mTestDataListener;
Context mContext;
int type = 0;
public TestPopupWindow(Context context) {
this.mContext = context;
View contentView = LayoutInflater.from(context).inflate(R.layout.popup_select_layout, null);
//封装成CommonPopupWindow类
setContentView(contentView);
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setOutsideTouchable(true);
// 收起时渐进恢复背景,从半透明-
setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
ValueAnimator animator = ValueAnimator.ofFloat(0.5f, 1f).setDuration(500);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
setBackgroundAlpha((Float) animation.getAnimatedValue());
}
});
animator.start();
}
});
show(contentView);
//初始化控件
TextView takePhoto = (TextView)contentView.findViewById(R.id.take_photo);
TextView picture = (TextView)contentView.findViewById(R.id.picture);
TextView cancel = (TextView)contentView.findViewById(R.id.cancel);
takePhoto.setOnClickListener((View)->{
Log.i("TestPopupWindow: ","拍照");
type = 0;
if(mTestDataListener != null){
mTestDataListener.getTestData(type);
}
dismiss();
});
picture.setOnClickListener((View)->{
Log.i("TestPopupWindow: ","相册");
type = 1;
if(mTestDataListener != null){
mTestDataListener.getTestData(type);
}
dismiss();
});
cancel.setOnClickListener((View)->{
Log.i("TestPopupWindow: ","取消");
type = 2;
if(mTestDataListener != null){
mTestDataListener.getTestData(type);
}
dismiss();
});
}
//回调事件接口
public interface TestDataListener {
void getTestData(int type);
}
public void setTestClickaListener(TestDataListener testDataListener) {
mTestDataListener = testDataListener;
}
/**
* 设置PopupWindow下面的内容的透明度
*/
private void setBackgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
lp.alpha = bgAlpha;
((Activity) mContext).getWindow().setAttributes(lp);
}
/**
* 显示弹窗,过程中伴随背景逐渐变暗。
* 不要改变MaskLayout的背景色,否则会是灰色背景的框框整体向上升起的效果。
*/
public void show(View parent) {
ValueAnimator animator = ValueAnimator.ofFloat(1, 0.5f).setDuration(500);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
setBackgroundAlpha((Float) animation.getAnimatedValue());
}
});
animator.start();
}
}
第三步:在Activity中使用
public class Case22 extends AppCompatActivity {
TestPopupWindow popupWindow;
Button btnShowPopupWindow;
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_case22);
ConstraintLayout case22Layout = findViewById(R.id.case22_layout); //主布局设置弹出窗口定位使用
btnShowPopupWindow = findViewById(R.id.show_popupWindow);
showPopupWindow(case22Layout);
}
//展示弹出窗口
private void showPopupWindow(View parent){
btnShowPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow = new TestPopupWindow(getApplicationContext());
popupWindow.showAtLocation(parent, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);//在activity的底部展示。
// popupWindow.showAsDropDown(binding.showPopupWindow); //在某个控件下方弹出Popupwindow
popupWindow.setTestClickaListener(new TestPopupWindow.TestDataListener() {
@Override
public void getTestData(int type) {
Log.d("test: ", String.valueOf(type)); //测试
switch (type){
case 0: //拍照
Toast.makeText(getBaseContext(),"拍照",Toast.LENGTH_SHORT).show();
break;
case 1: //相册
Toast.makeText(getBaseContext(),"相册",Toast.LENGTH_SHORT).show();
break;
case 2: //取消
Toast.makeText(getBaseContext(),"取消",Toast.LENGTH_SHORT).show();
break;
}
}
});
}
});
}
}
大功告成!
千夜零一:“之前总是看各种博客学习东西,现在我想用博客记录下我的学习脚步,好东西也需要分享,索取和给予是相互的。以后会尽量日更的!目标完成1001篇博客哈哈。”
如果觉得对你有所帮助,请不要吝啬你的点赞,有问题也可以在下方评论区留言哦,关注我一起学习吧~