Android自定义popupwindow实例代码

所属分类: 软件编程 / Android 阅读数: 25
收藏 0 赞 0 分享

先来看看效果图:

一、布局 

<?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"
  android:background="#ffffff"
  android:padding="20dp" >

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:gravity="center"
    android:textColor="@android:color/holo_orange_dark"
    android:text="确定" />

  <TextView
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:clickable="true"
    android:gravity="center"
    android:text="取消" />

</LinearLayout>

2、自定义MypopupWindow继承PopupWindow

public class MyPopupWindow extends PopupWindow {  

3、重写构造方法与动画样式
在styles.xml自定义样式,动画

<style name="MyPopupWindow">

    <item name="android:windowEnterAnimation">@anim/pop_in</item>
    <item name="android:windowExitAnimation">@anim/pop_out</item>
  </style>

 pop_in

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

  <!-- 平移
  <translate
     android:duration="5000"
     android:fromXDelta="100%"

     android:toXDelta="0"/>
     -->

  <scale
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.8"
    android:toYScale="0.5"
    android:duration="200"/>

  <!--
fromXScale
fromYScale
起始时X,Y座标,

pivotX
pivotY

动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始

toXScale
toYScale
动画最终缩放的倍数, 1.0为正常大小,大于1.0放大

duration
动画持续时间


 -->

  <!--透明度-->
  <alpha
    android:duration="200"
    android:fromAlpha="0.0"

    android:toAlpha="1.0"/>


</set>

pop_out

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

  <!-- <translate
    android:duration="5000"
    android:fromXDelta="0"

    android:toXDelta="100%"/>-->

  <scale
    android:fromXScale="0.8"
    android:fromYScale="0.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0"
    android:toYScale="0"
    android:duration="200"/>



  <alpha
    android:duration="200"
    android:fromAlpha="1.0"

    android:toAlpha="0.0"/>

</set>

4、重写构造方法并设置点击外部可以消失监听

 super(context);

    this.mContext=context;
    //打气筒
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    //打气

    mContentView = mInflater.inflate(R.layout.layout_dialog,null);

    //设置View
    setContentView(mContentView);


    //设置宽与高
    setWidth(WindowManager.LayoutParams.MATCH_PARENT);

    setHeight(WindowManager.LayoutParams.WRAP_CONTENT);


    /**
     * 设置进出动画
     */
    setAnimationStyle(R.style.MyPopupWindow);


    /**
     * 设置背景只有设置了这个才可以点击外边和BACK消失
     */
    setBackgroundDrawable(new ColorDrawable());


    /**
     * 设置可以获取集点
     */
    setFocusable(true);

    /**
     * 设置点击外边可以消失
     */
    setOutsideTouchable(true);

    /**
     *设置可以触摸
     */
    setTouchable(true);


    /**
     * 设置点击外部可以消失
     */

    setTouchInterceptor(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

        /**
         * 判断是不是点击了外部
         */
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
          return true;
        }
        //不是点击外部
        return false;
      }
    });

5、显示及设置窗口变暗与变亮

public void displayDialog(View view){

    MyPopupWindow myPopupWindow = new MyPopupWindow(this);

    myPopupWindow.showAsDropDown(mBtnDispaly,0,0);
    lightOff();


    /**
     * 消失时屏幕变亮
     */
    myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();

        layoutParams.alpha=1.0f;

        getWindow().setAttributes(layoutParams);
      }
    });
  }

  /**
   * 显示时屏幕变暗
   */
  private void lightOff() {

    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();

    layoutParams.alpha=0.3f;

    getWindow().setAttributes(layoutParams);

  }

6、完整

package liu.basedemo.view;

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

import liu.basedemo.R;


/**
 * 学习PopupWindow
 * Created by 刘楠 on 2016/8/1 0001.17:42
 */
public class MyPopupWindow extends PopupWindow {

  Context mContext;
  private LayoutInflater mInflater;
  private View mContentView;


  public MyPopupWindow(Context context) {
    super(context);

    this.mContext=context;
    //打气筒
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    //打气

    mContentView = mInflater.inflate(R.layout.layout_dialog,null);

    //设置View
    setContentView(mContentView);


    //设置宽与高
    setWidth(WindowManager.LayoutParams.MATCH_PARENT);

    setHeight(WindowManager.LayoutParams.WRAP_CONTENT);


    /**
     * 设置进出动画
     */
    setAnimationStyle(R.style.MyPopupWindow);


    /**
     * 设置背景只有设置了这个才可以点击外边和BACK消失
     */
    setBackgroundDrawable(new ColorDrawable());


    /**
     * 设置可以获取集点
     */
    setFocusable(true);

    /**
     * 设置点击外边可以消失
     */
    setOutsideTouchable(true);

    /**
     *设置可以触摸
     */
    setTouchable(true);


    /**
     * 设置点击外部可以消失
     */

    setTouchInterceptor(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

        /**
         * 判断是不是点击了外部
         */
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
          return true;
        }
        //不是点击外部
        return false;
      }
    });


    /**
     * 初始化View与监听器
     */
    initView();

    initListener();
  }




  private void initView() {

  }

  private void initListener() {

  }


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多