仿照微信自定义PopupWindow

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvzhongdi/article/details/53206702
我又来了,今天给大家介绍下自定义的PopupWindow,刚开始学习android的时候我就知道微信使用的就是这个,但是刚开始的时候搞不清楚和Dialog的区别,所以今天想通过简单的介绍,让大家知道他们的区别,然后老规矩,奉献上自己的代码。哈哈
**一.概述:**
Android的对话框有两种:PopupWindow AlertDialog.他们的不同点在于AlertDialog的位置固定,而PopupWindow的位置可以随意设置自己的位置。
是不是很简单呢,哈哈,来吧,还是直接看代码吧;
众所周知android提供给我们的控件是有限的,所以想要实现自己的控件,只能通过自定义,继承PopupWindow。
先看布局
popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/title_tools_bg"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/copy_pressed" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="编辑文字"
            android:textColor="#fff"
            android:textSize="18sp"
            android:textStyle="bold" />

    </LinearLayout>

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#fff" />

    <LinearLayout
        android:id="@+id/ll_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/share" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="分享内容"
            android:textColor="#fff"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#fff" />

    <LinearLayout
        android:id="@+id/ll_magic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/title_btn_function" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="魔法图标"
            android:textColor="#fff"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

MyPopupWindow类

package com.lv.activity;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

/**
 * Created by mac on 16/11/14.
 */

public class MyPopupWindow extends PopupWindow implements View.OnClickListener {


    private View view;
    private LinearLayout ll_magic, ll_share, ll_edittext;
    private Context context;

    public MyPopupWindow(Context context) {
        super(context);
        this.context = context;
        //加载布局
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.popuwindow, null);
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        int width = wm.getDefaultDisplay().getWidth();
        //把MyPopupWindow 添加到布局中去
        this.setContentView(view);
        // 设置MyPopupWindow弹出窗体的宽
        this.setWidth(width / 2);
        // 设置MyPopupWindow弹出窗体的高度
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置MyPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview);//在stytle中配置下
        //初始化控件
        ll_edittext = (LinearLayout) view.findViewById(R.id.ll_edittext);
        ll_share = (LinearLayout) view.findViewById(R.id.ll_share);
        ll_magic = (LinearLayout) view.findViewById(R.id.ll_magic);
        //设置点击事件
        ll_edittext.setOnClickListener(this);
        ll_share.setOnClickListener(this);
        ll_magic.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_edittext:
                Toast.makeText(context, "编辑文字", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_share:
                Toast.makeText(context, "分享文字", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_magic:
                Toast.makeText(context, "魔法图标", Toast.LENGTH_SHORT).show();
                break;
        }
        MyPopupWindow.this.dismiss();
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
        } else {
            this.dismiss();
        }
    }
}

这些都清楚后了,下面就是简单的调用了,很简单
PopupwindowActivity类

public class PopuwindowActivity extends Activity {
    private Button mBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popuwindow);
        mBtn = (Button) findViewById(R.id.search_close_btn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyPopupWindow popupWindow = new MyPopupWindow(PopuwindowActivity.this);
                popupWindow.showPopupWindow(mBtn);
            }
        });
    }

}

这里面为了不让我们的程序看起来单调,所以加入了切换的动画。代码也粘贴出来吧。
R.style.AnimationPreview
自定义style

  <style name="AnimationPreview">
        <item name="android:windowEnterAnimation">@anim/fade_in</item>
        <item name="android:windowExitAnimation">@anim/fade_out</item>
    </style>

fade_in.xml和fade_out.xml 点击和消失的动画

<scale   xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="0.001"
    android:toXScale="1.0"
    android:fromYScale="0.001"
    android:toYScale="1.0"
    android:pivotX="100%"
    android:pivotY="10%"
    android:duration="200" />

<scale   xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1.0"
    android:toXScale="0.001"
    android:fromYScale="1.0"
    android:toYScale="0.001"
    android:pivotX="100%"
    android:pivotY="10%"
    android:duration="200" />

大家看下吧,很简单,代码只不过是辅助大家学习的,真正想要学习的话,大家还得通过自己不断的努力。自定义的路还是漫长的,以后会奉献出更多的自定义控件和讲解,不明白的地方欢迎留言交流。

猜你喜欢

转载自blog.csdn.net/lvzhongdi/article/details/53206702