Android-自定义popupwindow详解(包含动画简单讲解)

我们知道在很多需求的时候都要求有对话框的,我们也知道dialog和popupwindow都可以实现。但是,popupwindow是弹框的万能之法,当然dialog的集成度,所继承的相关方法也给予一定便利。关于dialog的自定义上篇文章有详细介绍,本篇文章将详解popupwindow的自定义(糅合动画)。


先上图,就是做成这个带有弹出的动画的popupwindow在这里插入图片描述


下面将进行代码的详解

大体思路:就是继承popupwindow,获得构造器,接入自定义布局,设置弹框属性

1.popupwindow的java文件:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.android.administrator.childrensittingposture.R;
import com.android.administrator.childrensittingposture.activity.HistoryActivity;
import com.android.administrator.childrensittingposture.activity.SettingActivity;

/**
 * Created by 符柱成 on 2016/7/24.
 */
public class MyPopWindow extends PopupWindow {
    private View conentView;
    private Activity context;

    public MyPopWindow(final Activity context) {
        super(context);
        this.context = context;
        this.initPopupWindow();

    }

    private void initPopupWindow() {
    //使用view来引入布局
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.popuo_dialog, null);
        //获取popupwindow的高度与宽度
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w / 2 + 50);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果,设置动画,一会会讲解
        this.setAnimationStyle(R.style.AnimationPreview);
        //布局控件初始化与监听设置
        LinearLayout llayout_remind = (LinearLayout) conentView
                .findViewById(R.id.llayout_remind);
        LinearLayout llayout_history = (LinearLayout) conentView
                .findViewById(R.id.llayout_history);
        llayout_remind.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
     
            }
        });

        llayout_history.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
               
            }
        });
    }

    /**
     * 显示popupWindow的方式设置,当然可以有别的方式。
     *一会会列出其他方法
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
        } else {
            this.dismiss();
        }
    }
}

列出其他的popupwindow显示方法:都是封装好了的方法,尽情使用吧

     /**
     * 第一种
     * 显示在控件的下右方
     *
     * @param parent parent
     */
    public void showAtDropDownRight(View parent) {
        if (parent.getVisibility() == View.GONE) {
            this.showAtLocation(parent, 0, 0, 0);
        } else {
            // x y
            int[] location = new int[2];
            //获取在整个屏幕内的绝对坐标
            parent.getLocationOnScreen(location);
            this.showAtLocation(parent, 0, location[0] + parent.getWidth() - this.width, location[1] + parent.getHeight());
        }
    }


    /**  第二种
     * 显示在控件的下中方
     *
     * @param parent parent
     */
    public void showAtDropDownCenter(View parent) {
        if (parent.getVisibility() == View.GONE) {
            this.showAtLocation(parent, 0, 0, 0);
        } else {
            // x y
            int[] location = new int[2];
            //获取在整个屏幕内的绝对坐标
            parent.getLocationOnScreen(location);
            this.showAtLocation(parent, 0, location[0] / 2 + parent.getWidth() / 2 - this.width / 6, location[1] + parent.getHeight());
        }
    }



 /**   第三种
     * 显示在控件的下左方
     *
     * @param parent parent
     */
    public void showAtDropDownLeft(View parent) {
        if (parent.getVisibility() == View.GONE) {
            this.showAtLocation(parent, 0, 0, 0);
        } else {
            // x y
            int[] location = new int[2];
            //获取在整个屏幕内的绝对坐标
            parent.getLocationOnScreen(location);
            this.showAtLocation(parent, 0, location[0], location[1] + parent.getHeight());
        }
    }

popupwindow的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@color/blue">

    <LinearLayout
        android:id="@+id/pop_layout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@color/white"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/llayout_remind"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="8dp" >

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:scaleType="fitCenter"
                android:src="@drawable/remind" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_marginLeft="10dp"
                android:gravity="center"
                android:text="自动提醒设置"
                android:textColor="@color/white"
                android:textSize="15dip" />
        </LinearLayout>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0.2dp"
            android:background="@color/white" />

        <LinearLayout
            android:id="@+id/llayout_history"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="8dp" >

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:scaleType="fitCenter"
                android:src="@drawable/history" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_marginLeft="10dp"
                android:gravity="center"
                android:text="历史记录统计"
                android:textColor="@color/white"
                android:textSize="15dip" />
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

此外我们不要忘了,我们是给这个popupwindow设置了动画的喔:style文件:这里作为资源来自定义样式

<style name="AnimationPreview">
        <item name="android:windowEnterAnimation">@anim/in</item>
        <item name="android:windowExitAnimation">@anim/out</item>
    </style>

保存动画的文件在另一个地方

用图说明在这里插入图片描述

下面就给出动画文件的代码啦:

<?xml version="1.0" encoding="utf-8"?>
<!-- 对话框出来时的动画-->
<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" />

<!--fromXScale与toXScale是指一开始的时候在x方向上的缩放比例和在结束时候的比例,1.0就是原本的样子。同理fromYScale和 toYScale-->
<!--pivotX和pivotY是指动画开始的位置 起始动画的x和y位置-->

<?xml version="1.0" encoding="utf-8"?>
<!-- 对话框消失时的动画-->
<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" />

好了,popupwindow的自定义思路详解完成,并且在期间列出一些扩展性的可能和讲解了这个动画的实现。

本文参考了:老妖的博客http://blog.csdn.net/wwj_748/article/details/25653409
欢迎在下方指出错误,共同学习!
转载请注明:【JackFrost的博客】

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您需要更高级的弹窗样式和交互时,可以使用 PopupWindow 来创建自定义布局的弹窗。下面是使用 PopupWindow 创建自定义布局的步骤: 1. 创建自定义布局文件:首先,创建一个 XML 文件来定义您的自定义布局。例如,您可以创建一个名为 `custom_popup.xml` 的文件,并在其中定义您希望显示的布局。 2. 实例化 PopupWindow:在您的 Activity 或 Fragment 中,实例化 PopupWindow 对象。 ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View customView = inflater.inflate(R.layout.custom_popup, null); PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ``` 3. 设置 PopupWindow 属性:根据需要,设置 PopupWindow 的属性,例如背景、动画效果、焦点等。 ```java popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置动画效果 popupWindow.setAnimationStyle(R.style.PopupAnimation); ``` 4. 设置布局中的控件和事件:通过 `customView` 获取布局中的控件,并设置相应的事件监听器。 ```java Button button = customView.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 } }); ``` 5. 显示弹窗:使用 `showAtLocation()` 或 `showAsDropDown()` 方法显示弹窗。`showAtLocation()` 方法可以显示在指定的位置,而 `showAsDropDown()` 方法则可以显示在某个视图的下方。 ```java View anchorView = findViewById(R.id.anchor_view); // 锚点视图 popupWindow.showAsDropDown(anchorView); // 或者使用 showAtLocation() 方法 ``` 这样,您就可以使用 PopupWindow 创建自定义布局的弹窗。希望对您有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值