自定义PupopWindow(带动画效果)

来源:互联网 发布:打赏危害网络直播 编辑:程序博客网 时间:2024/06/09 17:34

二话不说直接上代码

  只用到了三个地方,MainActivity 和 MainActivity的布局    ,填充pupopwindow 的布局


MainActivity  代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private RelativeLayout rl;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        rl = (RelativeLayout) findViewById(R.id.rl);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.tv:                ShowPopWindowShareToOther();                break;        }    }    ImageView point_out_cha;    CheckBox user_protocol_cb;    TextView point_out_share_to_others;    PopupWindow share_to_others_tips_pop;//分享提醒    private void ShowPopWindowShareToOther() {        if (share_to_others_tips_pop == null) {            View view = LayoutInflater.from(this).inflate(R.layout.layout_share_to_others, null);            point_out_cha = (ImageView) view.findViewById(R.id.point_out_cha);//叉            user_protocol_cb = (CheckBox) view.findViewById(R.id.user_protocol_cb);//不再提示的选择框            point_out_share_to_others = (TextView) view.findViewById(R.id.point_out_share_to_others);//去分享            // 创建一个PopuWidow对象            share_to_others_tips_pop = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);        }        // 使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法        share_to_others_tips_pop.setFocusable(true);        // 设置允许在外点击消失        share_to_others_tips_pop.setOutsideTouchable(false);        // 设置背景,这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景        share_to_others_tips_pop.setBackgroundDrawable(new BitmapDrawable());        //软键盘不会挡着popupwindow        share_to_others_tips_pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//设置pop的动画share_to_others_tips_pop.setAnimationStyle(R.style.showPopAnimation);// showPopAnimation在styles里
//设置菜单显示的位置 share_to_others_tips_pop.showAtLocation(rl, Gravity.BOTTOM, 0, 0); point_out_share_to_others.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "点击了去分享", Toast.LENGTH_SHORT).show(); //前往去分享 share_to_others_tips_pop.dismiss(); } }); point_out_cha.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { share_to_others_tips_pop.dismiss(); } }); }}
MainActivity的布局
<?xml version="1.0" encoding="utf-8"?><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/rl"    tools:context="com.example.popupwindow.MainActivity">    <TextView        android:id="@+id/tv"        android:onClick="onClick"        android:textSize="20sp"        android:padding="10dp"        android:gravity="center_horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="按钮"/></RelativeLayout>
最后一个就是填充PupopWindow的布局
<?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="match_parent"    android:orientation="vertical">    <RelativeLayout        android:id="@+id/rl_point_out"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#3000"        android:onClick="onClick">        <LinearLayout            android:background="#3000"            android:layout_width="268dp"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:orientation="vertical">            <RelativeLayout                android:layout_width="match_parent"                android:layout_height="150dp">                <ImageView                    android:id="@+id/point_out_cha"                    android:layout_width="36dp"                    android:layout_height="40dp"                    android:layout_alignParentRight="true"                    android:layout_marginRight="12dp"                    android:src="@mipmap/ic_launcher" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerInParent="true"                    android:paddingLeft="10dp"                    android:paddingRight="10dp"                    android:text="您需要将这条圈动态分享到:微信、QQ才能领取该红包,请知晓!"                    android:textSize="16sp" />                <LinearLayout                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_alignParentBottom="true"                    android:layout_marginBottom="5dp"                    android:layout_marginLeft="10dp"                    android:layout_marginTop="10dp"                    android:gravity="center_vertical"                    android:orientation="horizontal">                    <CheckBox                        android:id="@+id/user_protocol_cb"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:onClick="onClick" />                    <TextView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="6dp"                        android:text="不再提醒"                        android:textSize="12sp" />                </LinearLayout>            </RelativeLayout>            <View                android:layout_width="match_parent"                android:layout_height="1dp" />            <LinearLayout                android:layout_width="match_parent"                android:layout_height="44dp"                android:layout_alignParentBottom="true"                android:orientation="horizontal">                <TextView                    android:id="@+id/point_out_share_to_others"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="去分享"                    android:textSize="16sp" />            </LinearLayout>        </LinearLayout>    </RelativeLayout></LinearLayout>
styles 的代码:
 
<style name="showPopAnimation" parent="android:Animation">  <!--name为调用这个style动画的名字,自己设置的-->    <item name="android:windowEnterAnimation">@anim/slide_in_bottom</item><!--- 进入动画-->    <item name="android:windowExitAnimation">@anim/slide_out_bottom</item><!-- 退出动画--></style>
slide_in_bottom.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="false">  <translate      android:interpolator="@android:anim/decelerate_interpolator"      android:duration="500"      android:fromXDelta="0%"      android:toXDelta="0%"      android:fromYDelta="100%"      android:toYDelta="0%"/></set>
slide_out_bottom.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="false">  <translate      android:interpolator="@android:anim/decelerate_interpolator"      android:duration="500"      android:fromXDelta="0%"      android:toXDelta="0%"      android:fromYDelta="0%"      android:toYDelta="100%"/></set>