Android -- 自定义popWindow工具类

一. 前言

popwindow是常用的一种自定义布局的弹窗,只需要加入不同的布局就可以显示不同的样子,灵活性较大。

二. popwindow工具类

import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import com.jmg.cameraphone.R;

public class ShowPopupWindow {

    static PopupWindow popupWindow = null;

    public static void showPopWindow(View view, View layout, int width, int height) {

        popupWindow = new PopupWindow(layout, width, height, true);

        popupWindow.setTouchable(true);
//        popupWindow.setBackgroundDrawable(null);
//        popupWindow.setFocusable(false);

        popupWindow.setAnimationStyle(R.style.showPopupAnimation);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("hotels", "popupWindow");
                return false;
                // 这里如果返回true的话,touch事件将被拦截
                // 拦截后 PopupWindowonTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });
        // 设置好参数之后再show
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    public static void dismissPopWindow() {
        popupWindow.dismiss();
    }
}

二. 动画

alpha_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 持续时间 -->
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

alpha_exit.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>

styles.xml : 

<style name="showPopupAnimation" parent="android:Animation">
    <item name="android:windowExitAnimation">@anim/alpha_exit</item>
    <item name="android:windowEnterAnimation">@anim/alpha_in</item>
</style>

三. 自定义圆角Button

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- 填充的颜色 -->
    <solid android:color="@color/colorWrite" />

    <!-- 设置按钮的四个角为弧形 -->
    <!-- android:radius 弧形的半径 -->
    <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    <!-- paddingButton里面的文字与Button边界的间隔 -->
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

注:想要哪个角圆就在corners中设置

四. popwindow显示的布局

 dialog_select.xml : 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="20dp"
        android:background="#01000000"
        android:orientation="vertical">


        <Button
            android:id="@+id/btnPhone"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/button_pop_style"
            android:gravity="center"
            android:text="相册" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.2dp" />

        <Button
            android:id="@+id/btnCamera"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/button_pop_style2"
            android:gravity="center"
            android:text="拍照" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/button_pop_style3"
            android:text="取消" />

    </LinearLayout>
</RelativeLayout>

五. MainActivity.class调用

在main_activity.xml中的按钮事件中调用以下代码:

View layout = LayoutInflater.from(MainActivity.this)
        .inflate(R.layout.dialog_select, null, false);    //设置popwindow的布局
int width = getResources().getDisplayMetrics().widthPixels;    //获取popwindow展示的宽
int height = getResources().getDisplayMetrics().heightPixels;    //获取popwindow展示的高
ShowPopupWindow.showPopWindow(view, layout, width, height);    //设置布局
Button btnPhone = layout.findViewById(R.id.btnPhone);        //popwindow内布局的控件
Button btnCamera = layout.findViewById(R.id.btnCamera);
Button btnCancel = layout.findViewById(R.id.btnCancel);

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnPhone:    //相册
                Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                //打开文件
                intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent1, 1);
                break;

            case R.id.btnCamera:    //拍照
                Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent2.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg")));
                startActivityForResult(intent2, 2);// 采用ForResult打开
                break;

            case R.id.btnCancel:    //取消
                ShowPopupWindow.dismissPopWindow();    //关闭popwindow
                break;
            default:
                break;
        }
    }
};

btnPhone.setOnClickListener(listener);    //设置popwindow布局控件的点击事件
btnCamera.setOnClickListener(listener);
btnCancel.setOnClickListener(listener);

The End!

每天进步一点点!




猜你喜欢

转载自blog.csdn.net/qq_26446715/article/details/80309547