Android安卓用Dialog对话框简单并且完美实现popupWindow底部弹出效果,有动画效果

1背景

项目中经常用到popupwindow从底部弹出的样式,而且存在有动画,最初实现这种效果用是的popupwindow实现起来十分复杂(指的是动画要实现,效果要好),代码基类书写的代码量大。后来发现对话框可以实现从底部弹出效果,实验后发现,代码量奇迹般变少了,特分享一下思路和代码

2看效果

3 要点

主要用到了自定义对话框的进出动画,对话框样式,设置对话框的屏幕宽度和位置等相关的方法

4实现过程

A 在attr.xml文件里面定义对话框的样式

   <!-- 对话框风格 -->
    <style name="dialogThemeBase" parent="@android:style/Theme.Dialog">
        <!-- 窗口边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- 窗口是否浮动在Activity上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">false</item>
        <!-- 没有标题栏 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 背景 -->
        <item name="android:background">@null</item>
        <!-- 背景模糊 -->
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

B 在style.xml文件里面定义对话框菜单的进出样式


     <!-- 底部弹出popupwindow的动画样式 -->
    <style name="popupAnimBottomIn" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/slide_in_from_bottom</item>
        <item name="android:windowExitAnimation">@anim/slide_out_from_bottom</item>
    </style>

C 在anin目录下创建两个动画文件

slide_in_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXDelta="0.0%"
    android:fromYDelta="100.0%"
    android:toXDelta="0.0%"
    android:toYDelta="0.0%" />

slide_out_from_bottom.xml


<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXDelta="0.0%"
    android:fromYDelta="0.0%"
    android:toXDelta="0.0%"
    android:toYDelta="100.0%" />

D 创建普通对话框基类

BaseDialog.java

package com.example.mytest20170113;

import android.app.Dialog;
import android.content.Context;
import android.view.View;

/**
 * Created by liugd on 2016/4/29.
 */
public abstract class BaseDialog extends Dialog implements View.OnClickListener {

    protected Context context;

    public BaseDialog(Context context) {
        this(context, R.style.dialogThemeBase);
    }

    public BaseDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
        if (setWindowAnimation() != 0) {
            getWindow().setWindowAnimations(setWindowAnimation());
        }
        setContentView(setView());
        initListener();
        finView();
    }


    /***
     * 设置VIEW
     *
     * @return
     */
    protected abstract int setView();


    protected void finView() {
    }


    //动画样式
    protected int setWindowAnimation() {
        return 0;
    }


    protected int[] setClickIDs() {
        return null;
    }



    /***
     * 泛型查找ID,无需强制转换
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewByID(int id) {
        return (T) super.findViewById(id);
    }

    /**
     * 初始化点击事件
     */
    void initListener() {
        int ids[] = setClickIDs();// 设置点击ID
        if (ids != null && ids.length > 0) {
            for (int id : ids) {
                findViewById(id).setOnClickListener(this);
            }
        }
    }
}


E 底部弹出对话框基类
BaseBottomDialog.java

package com.example.mytest20170113;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;

/**
 * 从底部弹出的对话框,实现popwindow弹出的效�? Created by liugd on 2017/1/12.
 */

public abstract class BaseBottomDialog extends BaseDialog {
	public BaseBottomDialog(Context context) {
		super(context);
		configScreenSize(context);
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.width = mScreenWidth;
		getWindow().setAttributes(lp);
		getWindow().setGravity(Gravity.BOTTOM);// 底部展示
	}

	// 动画样式
	protected int setWindowAnimation() {
		return R.style.popupAnimBottomIn;
	}

	public static void configScreenSize(Context context) {
		if (mScreenWidth == 0) {
			DisplayMetrics display = new DisplayMetrics();
			WindowManager manager = (WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE);
			manager.getDefaultDisplay().getMetrics(display);
			mScreenHeight = Math.max(display.heightPixels, display.widthPixels);
			mScreenWidth = Math.min(display.heightPixels, display.widthPixels);
		}
	}

	public static int mScreenHeight;
	public static int mScreenWidth;
}

F 底部对话框的实现类

MyTestBottomDialog.java

package com.example.mytest20170113;

import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MyTestBottomDialog extends BaseBottomDialog {

	public MyTestBottomDialog(Context context) {
		super(context);
	}

	@Override
	public void onClick(View arg0) {
		Toast.makeText(context, ((TextView) arg0).getText().toString(), 0)
				.show();
		switch (arg0.getId()) {
		case R.id.button1:

			break;
		case R.id.button2:

			break;
		case R.id.button3:

			break;
		}
		dismiss();

	}

	@Override
	protected int[] setClickIDs() {
		return new int[] { R.id.button1, R.id.button2, R.id.button3 };
	}

	@Override
	protected int setView() {
		return R.layout.dialog_bottom_test;
	}

}

G  测试对话框的布局文件

dialog_bottom_test.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:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选项1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选项2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消" />

</LinearLayout>


H 在Activity里面测试

		findViewById(R.id.textView1).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						MyTestBottomDialog dialog = new MyTestBottomDialog(MainActivity.this);
						dialog.show();
					}
				});



2017-1-13 12:16

附CSDN下载地址

http://download.csdn.NET/detail/u012990509/9736702



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值