当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义popupwindow实例代码

Android自定义popupwindow实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

海王祭无弹窗,成都数字电视,忻州师范学院论坛

先来看看效果图:

一、布局 

<?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() {

  }


}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网