android弹窗demo,Android编程实现仿易信精美弹出框效果
【附demo源码下载】
本⽂实例讲述了Android编程实现仿易信精美弹出框效果。分享给⼤家供⼤家参考,具体如下:
截图:
动画效果介绍:
android编程入门指南 pdf
1.点击ActionBar上“+”按钮,菜单从上⽅弹出(带反弹效果);
2.再次点击“+”、点击空⽩区域或者点击返回键,菜单向上⽅收起;
3.点击弹出框上的按钮时,该按钮放⼤,其它按钮缩⼩,菜单整体渐变退出。
主体代码:
1.Activity.
/**
* 仿易信动画弹出框
*/
public class MainActivity extends ActionBarActivity {
//⽤于标记页⾯顶端位置
private View topView;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
topView = findViewById(R.id.main_top);
}
private PopupWindow popupWindow;
private int line1DeltaY, line2DeltaY;
//仿易信更多弹出框
private void showPopup() {
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);
//测量⾼度
int line2Height = ViewMeasuredHeight(itemViews[0]);
line1DeltaY = -getActionBarHeight() - 40;
line2DeltaY = line1DeltaY - line2Height;
blankView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismissPopup();
}
});
blankView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismissPopup();
}
});
popupWindow = new PopupWindow(contentView, ScreenW(this), ScreenH(this)); //随便设置⼀个drawable作为背景
popupWindow.setBackgroundDrawable(new ColorDrawable());
}
if (!popupWindow.isShowing()) {
popupWindow.showAsDropDown(topView, 0, 0);
for (int i = 0; i < itemViews.length; i++) {
if (i < 3) {
//第⼀⾏
itemViews[i].atePopupAnimIn(this, line1DeltaY));
} else {
//第⼆⾏
itemViews[i].atePopupAnimIn(this, line2DeltaY));
}
}
}
}
private void dismissPopup() {
if (popupWindow == null || !popupWindow.isShowing()) {
return;
}
ViewGroup contentView = (ViewGroup) ContentView();
contentView.atePopupBgFadeOutAnim(AnimationHelper.TIME_OUT)); for (int i = 0; i < itemViews.length; i++) {
if (i < 3) {
//第⼀⾏
itemViews[i].atePopupAnimOut(this, line1DeltaY));
} else {
//第⼆⾏
itemViews[i].atePopupAnimOut(this, line2DeltaY));
}
}
//动画结束时隐藏popupWindow
contentView.postDelayed(new Runnable() {
@Override
public void run() {
popupWindow.dismiss();
}
}, AnimationHelper.TIME_OUT + 10);
}
private View[] itemViews;
//初始化popupWindow上的按钮
private void initItems(View parent) {
int[] viewIds = new int[]{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 = new View[viewIds.length];
int itemWidth = ScreenW(this) / 3;
OnClickImpl l = new OnClickImpl();
for (int i = 0; i < viewIds.length; i++) {
int id = 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);
}
}
private class OnClickImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
final int viewId = v.getId();
//背景动画
//动画结束时隐藏popupWindow
v.postDelayed(new Runnable() {
@Override
public void run() {
popupWindow.dismiss();
//动画结束时响应点击事件
handleEvent(viewId);
}
}, AnimationHelper.TIME_OUT_CLICK + 10);
//按钮动画
for (View item : itemViews) {
if (Id() == v.getId()) {
//点击的按钮,放⼤
item.atePopupItemBiggerAnim(MainActivity.this)); } else {
/
/其它按钮,缩⼩
item.atePopupItemSmallerAnim(MainActivity.this)); }
}
}
}
//popupWindow上按钮的点击事件
private void handleEvent(int viewId) {
Toast.makeText(this, "点击了按钮:" + viewId, Toast.LENGTH_SHORT).show();
}
private int getActionBarHeight() {
return getSupportActionBar().getHeight();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().u.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = ItemId();
if (id == R.id.action_more) {
if (popupWindow == null || !popupWindow.isShowing()) {
showPopup();
} else {
dismissPopup();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。