PopupWindow简单动画弹窗
2018-11-08 本文已影响0人
周大侠侠侠侠侠侠侠侠侠侠侠侠侠
写在前面
主要感觉自己对于博客这个东西感觉很手生,现拿一些简单的东西来练练手,先练练等级,然后再去做一些分析类的文章,这样可能会好一点,感觉abcd的代码居然比中文的博客还好写,我也是服了我自己
内容
先来一个简单的PopupWindow弹出效果,主要是showAtLocation()和showAsDropDown()显示的动画效果不同,所以就需要进行统一一下。
主要思路:因为PopupWindow其实也是一个View,所以只要show()的时候对布局显示的时候开始动画,dimiss()的时候开始关闭动画就可以
321.gif
代码
动效 就是简单的上下偏移的动画 这里是可以自己设置
private Animation createVerticalAnimation(float fromY, float toY) {
Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
0f,
Animation.RELATIVE_TO_SELF,
0f,
Animation.RELATIVE_TO_SELF,
fromY,
Animation.RELATIVE_TO_SELF,
toY);
animation.setDuration(300);
animation.setInterpolator(new DecelerateInterpolator());
return animation;
}
自定义Show方法
public void showPopupWindow(View v) {
//获取控件的位置坐标
v.getLocationOnScreen(location);
//获取自身的长宽高
mRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//获取屏幕高度
int measuredHeight = mRootView.getMeasuredHeight();
int measuredWidth = mRootView.getMeasuredWidth();
int width = v.getMeasuredWidth();
//如果在一半的上方 显示在下面 在一半的下方显示在上面
if (location[1] > ScreenUtils.getScreenHeight() / 2) {
showAnim = createVerticalAnimation(1, 0);
hideAnim = createVerticalAnimation(0, 1);
mRootView.startAnimation(showAnim);
this.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] - measuredWidth + width), location[1] - measuredHeight-20);
} else {
showAnim = createVerticalAnimation(-1, 0);
hideAnim = createVerticalAnimation(0, -1);
mRootView.startAnimation(showAnim);
this.showAsDropDown(v, -measuredWidth + width, 20);
}
hideAnim.setAnimationListener(new AnimListener() {
@Override
public void onAnimationEnd(Animation animation) {
CirclePopupWindow.super.dismiss();
}
});
}
完整代码,布局就不贴了
/**
* title:点击更多弹窗效果
* tip:
*
* @author zhou
* @date 2018-10-29 上午10:15
*/
public class CirclePopupWindow extends PopupWindow {
private List<PopupEntity> mData;
private Context mContext;
private View mRootView;
private Animation showAnim, hideAnim;
public CirclePopupWindow(Context context, List<PopupEntity> list) {
super(context);
mContext = context;
mData = list;
initialize();
}
private void initialize() {
mRootView = LayoutInflater.from(mContext).inflate(R.layout.popupwindow, null);
RecyclerView mRvList = mRootView.findViewById(R.id.rv_list);
mRvList.setLayoutManager(new LinearLayoutManager(mContext));
RecyclerPopupAdapter adapter = new RecyclerPopupAdapter();
adapter.setNewData(mData);
mRvList.setAdapter(adapter);
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
if (mData != null) {
ToastUtils.showShort(mData.get(position).getContent());
}
}
});
this.setContentView(mRootView);
this.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(mContext, android.R.color.transparent)));
this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
this.setFocusable(true);
this.setOutsideTouchable(true);
this.update();
}
private int[] location = new int[2];
public void showPopupWindow(View v) {
//获取控件的位置坐标
v.getLocationOnScreen(location);
//获取自身的长宽高
mRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//获取屏幕高度
int measuredHeight = mRootView.getMeasuredHeight();
int measuredWidth = mRootView.getMeasuredWidth();
int width = v.getMeasuredWidth();
//如果在一半的上方 显示在下面 在一半的下方显示在
if (location[1] > ScreenUtils.getScreenHeight() / 2) {
showAnim = createVerticalAnimation(1, 0);
hideAnim = createVerticalAnimation(0, 1);
mRootView.startAnimation(showAnim);
this.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] - measuredWidth + width), location[1] - measuredHeight-20);
} else {
showAnim = createVerticalAnimation(-1, 0);
hideAnim = createVerticalAnimation(0, -1);
mRootView.startAnimation(showAnim);
this.showAsDropDown(v, -measuredWidth + width, 20);
}
hideAnim.setAnimationListener(new AnimListener() {
@Override
public void onAnimationEnd(Animation animation) {
CirclePopupWindow.super.dismiss();
}
});
}
@Override
public void dismiss() {
if (hideAnim != null) {
mRootView.startAnimation(hideAnim);
} else {
super.dismiss();
}
}
private Animation createVerticalAnimation(float fromY, float toY) {
Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
0f,
Animation.RELATIVE_TO_SELF,
0f,
Animation.RELATIVE_TO_SELF,
fromY,
Animation.RELATIVE_TO_SELF,
toY);
animation.setDuration(300);
animation.setInterpolator(new DecelerateInterpolator());
return animation;
}
public class AnimListener implements Animation.AnimationListener {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
}
}