android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

时间:2023-11-25 13:02:20

需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求。之前在通知栏显示的提示需要在标题栏上面弹出提示框,然后用动画去显示隐藏,并且在大部分Activity上都要显示。

问题1:用上次那个TextView隐藏在布局文件中肯定不行了,不然每个activity都要修改,于是决定用PopupWindow,只要显示在activity的根布局上就行.

问题2:需要把显示PopupWindow的方法抽出来作为一个公共方法,我这边是放到一个工具类里面,传入当前activity.

问题3:从顶部移动多少距离呢?因为我们这个提示框跟标题栏的高度不一样的,于是我就计算提示框图片的高度,作为移动的高度.

效果图如下:

android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

PopupWindowTest.java  程序的入口,调用显示popupwindow的工具类

/**
* 显示PopupWindow的activity
* @author ansen
* @create time 2015-10-27
*/
public class PopupWindowTest extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popupwindow_test); findViewById(R.id.btn_show_tip).setOnClickListener(onClickListener);
} private OnClickListener onClickListener=new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_show_tip://显示提示.
Util util=new Util();
util.showTips(PopupWindowTest.this);
break;
}
}
};
}

Util.java   封装了显示popupwindow的方法,并且增加了动画结束回调接口,动画结束隐藏popupwindow。

/**
* 封装了显示Popupwindow的方法.
* @author ansen
* @create time 2015-10-27
*/
public class Util implements AnimationEndCallback{
private PopupWindow reportVideoPopwindow; public void showTips(Activity activity){
int translateHeight=(int) dip2px(activity,52);
View parent = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
View popView = LayoutInflater.from(activity).inflate(R.layout.activity_popupwindow_tips, null);
int statusBar=getStatusBarHeight(activity);
reportVideoPopwindow = new PopupWindow(popView,LayoutParams.MATCH_PARENT,translateHeight*2);
reportVideoPopwindow.showAtLocation(parent,Gravity.TOP, 0, 0);
TipRelativeLayout tvTips=(TipRelativeLayout) popView.findViewById(R.id.rl_tips);
tvTips.setTitleHeight(statusBar);//移动状态栏的高度
tvTips.setAnimationEnd(this);//设置动画结束监听函数
tvTips.showTips();//显示提示RelativeLayout,移动动画.
} public int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
} private float dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
float result = dpValue * scale + 0.5f;
return result;
} @Override
public void onAnimationEnd() {
reportVideoPopwindow.dismiss();//动画结束,隐藏popupwindow
}
}

TipRelativeLayout.java   这个类也没啥好说的,就在原来的TipTextView基础上增加了一个动画结束回调接口。

/**
* 自定义RelativeLayout 显示提示信息,显示时有动画效果(从上面弹出,然后改变透明度慢慢隐藏)
* @author ansen
* @create time 2015-10-20
*/
public class TipRelativeLayout extends RelativeLayout{
private static final int START_TIME=400;//动画显示时间
private static final int END_TIME=400;//动画移出时间
private static final int SHOW_TIME=1000;//动画显示时间 private AnimationEndCallback animationEnd;
private int titleHeight=100;//标题栏默认的高度设置成100 public TipRelativeLayout(Context context) {
super(context);
} public TipRelativeLayout(Context context, AttributeSet paramAttributeSet) {
super(context, paramAttributeSet);
} public TipRelativeLayout(Context context, AttributeSet paramAttributeSet,int paramInt) {
super(context, paramAttributeSet, paramInt);
} public void showTips(){
setVisibility(View.VISIBLE); //向下移动动画
TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,0,titleHeight);
downTranslateAnimation.setDuration(START_TIME);
downTranslateAnimation.setFillAfter(true); startAnimation(downTranslateAnimation); //动画监听
downTranslateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation){//向下移动动画结束
topTranslateAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
} private void topTranslateAnimation(){
new Handler().postDelayed(new Runnable() {//延时1秒之后再向上移动
@Override
public void run(){ //向上移动动画
TranslateAnimation topTranslateAnimation=new TranslateAnimation(0,0,titleHeight,0);
topTranslateAnimation.setDuration(END_TIME);
topTranslateAnimation.setFillAfter(true); //改变透明度
AlphaAnimation alphaAnimation=new AlphaAnimation(1,0);
alphaAnimation.setDuration(END_TIME); //两个动画添加到动画集合中
AnimationSet animationSet=new AnimationSet(true);
animationSet.addAnimation(topTranslateAnimation);
animationSet.addAnimation(alphaAnimation); startAnimation(animationSet);//开启动画 animationSet.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation){//动画结束隐藏提示的TextView
setVisibility(View.GONE);
if(animationEnd!=null){
animationEnd.onAnimationEnd();
}
}
});
}
},SHOW_TIME);
} /**
* 设置标题栏高度
* @param titleHeight
*/
public void setTitleHeight(int titleHeight) {
this.titleHeight = titleHeight;
} public void setAnimationEnd(AnimationEndCallback animationEnd) {
this.animationEnd = animationEnd;
} /**
* 动画结束监听函数
* @author apple
*/
public interface AnimationEndCallback{
public void onAnimationEnd();
}
}

其他的布局文件什么的我就不贴出来了,有需要的自己可以去下载源码.推荐下自己创建的android QQ群:202928390  
欢迎大家的加入.

点击下载源码

下载第二版   修正了某些手机状态栏的高度不对出现的bug.