MainActivity.java
package com.example.mybottompopupwindowdemo; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v4.view.ViewPager.LayoutParams; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.ScaleAnimation; import android.view.animation.Animation.AnimationListener; import android.widget.Button; import android.widget.PopupWindow; import android.widget.RelativeLayout; public class MainActivity extends Activity implements OnClickListener { private Button button; private PopupWindow mcontactsBottompopup; private View contactBottomPopulayout; private RelativeLayout ll_contact_root; private RelativeLayout rl_contact_copy; private RelativeLayout rl_contact_send; private RelativeLayout rl_contact_cancle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); ll_contact_root = (RelativeLayout) findViewById(R.id.ll_contact_root); initContactsBottmoPopu(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mcontactsBottompopup.isShowing()) { mcontactsBottompopup.dismiss();// 关闭 } else { mcontactsBottompopup.showAtLocation(MainActivity.this .findViewById(R.id.ll_contact_root), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 动画效果 WindowManager.LayoutParams params = getWindow() .getAttributes(); params.alpha = 0.7f; getWindow().setAttributes(params); ScaleAnimation animation = new ScaleAnimation(1f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f); animation.setDuration(150); animation.setFillAfter(true); contactBottomPopulayout.startAnimation(animation); } } }); mcontactsBottompopup .setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams params = getWindow() .getAttributes(); params.alpha = 1.0f; getWindow().setAttributes(params); ScaleAnimation animation = new ScaleAnimation(1f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f); animation.setDuration(150); animation.setFillAfter(true); contactBottomPopulayout.startAnimation(animation); } }); } private void initContactsBottmoPopu() { contactBottomPopulayout = View.inflate(this, R.layout.contacts_bottom_popup, null); mcontactsBottompopup = new PopupWindow(contactBottomPopulayout); mcontactsBottompopup.setContentView(contactBottomPopulayout); // 加上这个popupwindow中的ListView才可以接收点击事件 mcontactsBottompopup.setWidth(LayoutParams.FILL_PARENT); mcontactsBottompopup.setHeight(LayoutParams.WRAP_CONTENT); mcontactsBottompopup.setFocusable(true); // ColorDrawable dw = new ColorDrawable(0xb0000000); mcontactsBottompopup.setBackgroundDrawable(new BitmapDrawable()); mcontactsBottompopup .setAnimationStyle(R.style.contactPopuAnimationExit); mcontactsBottompopup.setOutsideTouchable(true); rl_contact_copy = (RelativeLayout) contactBottomPopulayout .findViewById(R.id.rl_contact_copy); rl_contact_send = (RelativeLayout) contactBottomPopulayout .findViewById(R.id.rl_contact_send); rl_contact_cancle = (RelativeLayout) contactBottomPopulayout .findViewById(R.id.rl_contact_cancle); rl_contact_copy.setOnClickListener(this); rl_contact_send.setOnClickListener(this); rl_contact_cancle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.rl_contact_copy: contactBottomPopuExit(); break; case R.id.rl_contact_send: contactBottomPopuExit(); break; case R.id.rl_contact_cancle: contactBottomPopuExit(); break; default: break; } } private void contactBottomPopuExit() { ScaleAnimation animation = new ScaleAnimation(1f, 1f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f); animation.setDuration(150); animation.setFillAfter(true); contactBottomPopulayout.startAnimation(animation); WindowManager.LayoutParams params = getWindow().getAttributes(); params.alpha = 1.0f; getWindow().setAttributes(params); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mcontactsBottompopup.dismiss();// 关闭 } }); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll_contact_root" tools:context="com.example.mybottompopupwindowdemo.MainActivity" > <Button android:id="@+id/button" android:text="点击我,从底部滑出popupwindow" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
contacts_bottom_popup.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#ebebe9" android:orientation="vertical" > <RelativeLayout android:background="@drawable/bottom_popu_bg_selector" android:id="@+id/rl_contact_copy" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="8dp" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/iv_contact_copy" android:text="复制号码" android:textColor="#111111" android:textSize="16dp" /> <ImageView android:id="@+id/iv_contact_copy" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="18dp" android:background="@drawable/iv_contact_copy" /> </RelativeLayout> <RelativeLayout android:background="@drawable/bottom_popu_bg_selector" android:id="@+id/rl_contact_send" android:layout_width="match_parent" android:layout_height="48dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/iv_contact_send" android:text="发送号码" android:textColor="#111111" android:textSize="16dp" /> <ImageView android:id="@+id/iv_contact_send" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="17dp" android:background="@drawable/iv_contact_send" /> </RelativeLayout> <RelativeLayout android:background="@drawable/bottom_popu_bg_selector" android:id="@+id/rl_contact_cancle" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginBottom="8dp" > <ImageView android:id="@+id/iv_contact_cancle" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="17dp" android:background="@drawable/iv_contact_cancle" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/iv_contact_cancle" android:text="取消" android:textColor="#111111" android:textSize="16dp" /> </RelativeLayout> </LinearLayout>
bottom_popu_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/more_popu_pressed"/> <item android:drawable="@drawable/contact_bottom_popu_normal" /> </selector>
contactpopu.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 位移效果 fromXDelta="0%p" 指的x轴是相对于父控件从父控件的x=0出开始位移 fromYDelta="0%p" 指的y轴是相对于父控件从父控件的y=0出开始位移 toXDelta="100%p" 指的x轴是相对于父控件到达父控件的x=100的位置即x轴在屏幕的终点 toYDelta="100%p" 指的y轴是相对于父控件到达父控件的y=100的位置即x轴在屏幕的终点 --> <scale android:duration="250" android:pivotX="50%" android:pivotY="100%" android:fromXScale="1.0" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="0.0" /> </set>
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="more_popu_pressed">#d9d9d9</drawable> <drawable name="contact_bottom_popu_normal">#ebebe9</drawable> </resources>
styles.xml
<style name="contactPopuAnimationExit"> <item name="android:windowExitAnimation">@anim/contactpopu</item> </style>
时间: 2024-11-08 19:05:03