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"?>
xmlns:android="http://schemas.android.com/apk/res/android">
    
            android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

alpha_exit.xml:

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

styles.xml : 

三. 自定义圆角Button

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

    
    android:color="@color/colorWrite" />

    
    
            android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    
            android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

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

四. popwindow显示的布局

 dialog_select.xml : 

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

            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">


        

五. 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!

每天进步一点点!




你可能感兴趣的:(android基础)