popwindow設(shè)置了動(dòng)畫(huà)?
封裝:
/**
* 封裝了顯示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);//移動(dòng)狀態(tài)欄的高度 tvTips.setAnimationEnd(this);//設(shè)置動(dòng)畫(huà)結(jié)束監(jiān)聽(tīng)函數(shù) tvTips.showTips();//顯示提示RelativeLayout,移動(dòng)動(dòng)畫(huà). } 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();//動(dòng)畫(huà)結(jié)束,隱藏popupwindow }}自定義RelativeLayout 顯示提示信息,顯示時(shí)有動(dòng)畫(huà)效果(從上面彈出,然后改變透明度慢慢隱藏public class TipRelativeLayout extends RelativeLayout{ private static final int START_TIME=400;//動(dòng)畫(huà)顯示時(shí)間 private static final int END_TIME=400;//動(dòng)畫(huà)移出時(shí)間 private static final int SHOW_TIME=1000;//動(dòng)畫(huà)顯示時(shí)間 private AnimationEndCallback animationEnd; private int titleHeight=100;//標(biāo)題欄默認(rèn)的高度設(shè)置成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); //向下移動(dòng)動(dòng)畫(huà) TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,0,titleHeight); downTranslateAnimation.setDuration(START_TIME); downTranslateAnimation.setFillAfter(true); startAnimation(downTranslateAnimation); //動(dòng)畫(huà)監(jiān)聽(tīng) downTranslateAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationEnd(Animation animation){//向下移動(dòng)動(dòng)畫(huà)結(jié)束 topTranslateAnimation(); } @Override public void onAnimationRepeat(Animation animation) {} }); } private void topTranslateAnimation(){ new Handler().postDelayed(new Runnable() {//延時(shí)1秒之后再向上移動(dòng) @Override public void run(){ //向上移動(dòng)動(dòng)畫(huà) 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); //兩個(gè)動(dòng)畫(huà)添加到動(dòng)畫(huà)集合中 AnimationSet animationSet=new AnimationSet(true); animationSet.addAnimation(topTranslateAnimation); animationSet.addAnimation(alphaAnimation); startAnimation(animationSet);//開(kāi)啟動(dòng)畫(huà) animationSet.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation){//動(dòng)畫(huà)結(jié)束隱藏提示的TextView setVisibility(View.GONE); if(animationEnd!=null){ animationEnd.onAnimationEnd(); } } }); } },SHOW_TIME); } /** * 設(shè)置標(biāo)題欄高度 * @param titleHeight */ public void setTitleHeight(int titleHeight) { this.titleHeight = titleHeight; } public void setAnimationEnd(AnimationEndCallback animationEnd) { this.animationEnd = animationEnd; } /** * 動(dòng)畫(huà)結(jié)束監(jiān)聽(tīng)函數(shù) * @author apple */ public interface AnimationEndCallback{ public void onAnimationEnd(); }可以借鑒參考下。