截图:
动画效果介绍:
1.点击ActionBar上“+”按钮,菜单从上方弹出(带反弹效果);
2.再次点击“+”、点击空白区域或者点击返回键,菜单向上方收起;
3.点击弹出框上的按钮时,该按钮放大,其它按钮缩小,菜单整体渐变退出。
主体代码:
1.Activity.
代码如下
/**
* 仿易信动画弹出框
*/
publicclassMainActivityextendsActionBarActivity {
//用于标记页面顶端位置
privateView topView;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topView = findViewById(R.id.main_top);
}
privatePopupWindow popupWindow;
privateintline1DeltaY, line2DeltaY;
//仿易信更多弹出框
privatevoidshowPopup() {
if(popupWindow ==null) {
View contentView = LayoutInflater.from(this).inflate(R.layout.yixin_pop_layout,null);
//点击空白区域关闭
View blankView = contentView.findViewById(R.id.yixin_more_blank);
View blankView2 = contentView.findViewById(R.id.yixin_more_blank2);
initItems(contentView);
//测量高度
intline2Height = ViewUtils.getViewMeasuredHeight(itemViews[0]);
line1DeltaY = -getActionBarHeight() -40;
line2DeltaY = line1DeltaY - line2Height;
blankView.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View v) {
dismissPopup();
}
});
blankView2.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View v) {
dismissPopup();
}
});
popupWindow =newPopupWindow(contentView, ScreenUtils.getScreenW(this), ScreenUtils.getScreenH(this));
//随便设置一个drawable作为背景
popupWindow.setBackgroundDrawable(newColorDrawable());
}
if(!popupWindow.isShowing()) {
popupWindow.showAsDropDown(topView,0,0);
for(inti =0; i < itemViews.length; i++) {
if(i<3) {
//第一行
itemViews[i].startAnimation(AnimationHelper.createPopupAnimIn(this, line1DeltaY));
}else{
//第二行
itemViews[i].startAnimation(AnimationHelper.createPopupAnimIn(this, line2DeltaY));
}
}
popupWindow.getContentView().startAnimation(AnimationHelper.createPopupBgFadeInAnim());
}
}
privatevoiddismissPopup() {
if(popupWindow ==null|| !popupWindow.isShowing()) {
return;
}
ViewGroup contentView = (ViewGroup) popupWindow.getContentView();
contentView.startAnimation(AnimationHelper.createPopupBgFadeOutAnim(AnimationHelper.TIME_OUT));
for(inti =0; i < itemViews.length; i++) {
if(i<3) {
//第一行
itemViews[i].startAnimation(AnimationHelper.createPopupAnimOut(this, line1DeltaY));
}else{
//第二行
itemViews[i].startAnimation(AnimationHelper.createPopupAnimOut(this, line2DeltaY));
}
}
//动画结束时隐藏popupWindow
contentView.postDelayed(newRunnable() {
@Override
publicvoidrun() {
popupWindow.dismiss();
}
}, AnimationHelper.TIME_OUT +10);
}
privateView[] itemViews;
//初始化popupWindow上的按钮
privatevoidinitItems(View parent) {
int[] viewIds =newint[]{R.id.yixin_more_item1, R.id.yixin_more_item2, R.id.yixin_more_item3,
R.id.yixin_more_item4, R.id.yixin_more_item5, R.id.yixin_more_item6};
itemViews =newView[viewIds.length];
intitemWidth = ScreenUtils.getScreenW(this) /3;
OnClickImpl l =newOnClickImpl();
for(inti =0; i < viewIds.length; i++) {
intid = viewIds[i];
itemViews[i] = parent.findViewById(id);
GridLayout.LayoutParams p = (GridLayout.LayoutParams) itemViews[i].getLayoutParams();
p.width = itemWidth;
itemViews[i].setLayoutParams(p);
itemViews[i].setOnClickListener(l);
}
}
privateclassOnClickImplimplementsView.OnClickListener {
@Override
publicvoidonClick(View v) {
finalintviewId = v.getId();
//背景动画
popupWindow.getContentView().startAnimation(AnimationHelper.createPopupBgFadeOutAnim(AnimationHelper.TIME_OUT_CLICK));
//动画结束时隐藏popupWindow
v.postDelayed(newRunnable() {
@Override
publicvoidrun() {
popupWindow.dismiss();
//动画结束时响应点击事件
handleEvent(viewId);
}
}, AnimationHelper.TIME_OUT_CLICK +10);
//按钮动画
for(View item : itemViews) {
if(item.getId() == v.getId()) {
//点击的按钮,放大
item.startAnimation(AnimationHelper.createPopupItemBiggerAnim(MainActivity.this));
}else{
//其它按钮,缩小
item.startAnimation(AnimationHelper.createPopupItemSmallerAnim(MainActivity.this));
}
}
}
}
//popupWindow上按钮的点击事件
privatevoidhandleEvent(intviewId) {
Toast.makeText(this,"点击了按钮:"+ viewId, Toast.LENGTH_SHORT).show();
}
privateintgetActionBarHeight() {
returngetSupportActionBar().getHeight();
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
returntrue;
}
@Override
publicbooleanonOptionsItemSelected(MenuItem item) {
intid = item.getItemId();
if(id == R.id.action_more) {
if(popupWindow ==null|| !popupWindow.isShowing()) {
showPopup();
}else{
dismissPopup();
}
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
//点击返回键时,如果popupWindow是显示状态,则关闭它
@Override
publicvoidonBackPressed() {
if(popupWindow !=null&& popupWindow.isShowing()) {
dismissPopup();
return;
}
super.onBackPressed();
}
}
2.动画工具类。
/**
* AnimationHelper
*/
publicclassAnimationHelper {
/**
* 进入动画的时间
*/
publicstaticfinalintTIME_IN =300;
/**
* 进入动画之后的反弹动画时间
*/
publicstaticfinalintTIME_IN_BACK =100;
/**
* 退出动画的时间
*/
publicstaticfinalintTIME_OUT =300;
/**
* 点击PopupWindow上菜单后退出动画的时间
*/
publicstaticfinalintTIME_OUT_CLICK =500;
/**
* PopupWindow上菜单进入动画
*/
publicstaticAnimation createPopupAnimIn(Context context,intfromYDelta) {
AnimationSet animationSet =newAnimationSet(context,null);
// animationSet.setInterpolator(new BounceInterpolator()); //结束时弹跳
animationSet.setFillAfter(true);
//移动
TranslateAnimation translateAnim =newTranslateAnimation(0,0, fromYDelta,20);
translateAnim.setDuration(TIME_IN);
animationSet.addAnimation(translateAnim);
//回弹效果
TranslateAnimation translateAnim2 =newTranslateAnimation(0,0,0, -20);
translateAnim2.setStartOffset(TIME_IN);
translateAnim2.setDuration(TIME_IN_BACK);
animationSet.addAnimation(translateAnim2);
returnanimationSet;
}
/**
* PopupWindow上菜单离开动画
*/
publicstaticAnimation createPopupAnimOut(Context context,inttoYDelta) {
AnimationSet animationSet =newAnimationSet(context,null);
animationSet.setFillAfter(true);
TranslateAnimation translateAnim =newTranslateAnimation(0,0,0, toYDelta);
translateAnim.setDuration(TIME_OUT);
animationSet.addAnimation(translateAnim);
returnanimationSet;
}
/**
* PopupWindow背景进入动画(透明度渐变)
*/
publicstaticAnimation createPopupBgFadeInAnim() {
AlphaAnimation anim =newAlphaAnimation(0,1.0f);
anim.setDuration(TIME_IN);
anim.setFillAfter(true);
returnanim;
}
/**
* PopupWindow背景离开动画(透明度渐变)
*/
publicstaticAnimation createPopupBgFadeOutAnim(intduration) {
AlphaAnimation anim =newAlphaAnimation(1.0f,0);
anim.setDuration(duration);
anim.setFillAfter(true);
returnanim;
}
/**
* PopupWindow按钮点击动画
*/
publicstaticAnimation createPopupItemBiggerAnim(Context context) {
AnimationSet animationSet =newAnimationSet(context,null);
animationSet.setFillAfter(true);
//放大(设置缩放的中心点为自己的中心)
ScaleAnimation scaleAnim =newScaleAnimation(1.0f,2.0f,1.0f,2.0f,
Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
scaleAnim.setDuration(TIME_OUT_CLICK);
animationSet.addAnimation(scaleAnim);
//渐变
AlphaAnimation alphaAnim =newAlphaAnimation(1.0f,0);
alphaAnim.setInterpolator(newAccelerateInterpolator());
alphaAnim.setDuration(TIME_OUT_CLICK);
animationSet.addAnimation(alphaAnim);
returnanimationSet;
}
/**
* PopupWindow按钮点击时其它按钮的动画
*/
publicstaticAnimation createPopupItemSmallerAnim(Context context) {
//放大(设置缩放的中心点为自己的中心)
ScaleAnimation scaleAnim =newScaleAnimation(1.0f,0,1.0f,0,
Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
scaleAnim.setDuration(TIME_OUT_CLICK);
scaleAnim.setFillAfter(true);
returnscaleAnim;
}
}
[从零开始学android编程]Android编程实现仿易信精美弹出框效果
http://m.bbyears.com/flash/122546.html
推荐访问: