Android Custom Popupwindow Instance code _android

Source: Internet
Author: User
Tags touch

Let's take a look at the effect chart:

First, the layout

<?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:o" rientation= "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= "OK" />

  <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= "Cancel"/>

</LinearLayout>

2, custom Mypopupwindow inheritance Popupwindow

public class Mypopupwindow extends Popupwindow {

3, rewrite the construction method and animation style
in Styles.xml custom style, animation

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

  <!--translation
  <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=/>

  <!--
Fromxscale
fromyscale
at the start of x,y coordinates,

pivotx
Pivoty

Animation starting position, relative to the percentage of the screen, two are 50% for the animation from the center of the screen

Toxscale
toyscale
Animation final scaling multiples, 1.0 for normal size, Greater than 1.0 amplification

duration
animation duration


 -->

  <!--transparency-->
  <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 :p ivotx= "50%"
    android:pivoty= "50%"
    android:toxscale= "0"
    android:toyscale= "0"
    android: duration= "/>



  <alpha
    android:duration=" android:fromalpha= "1.0"

    android: Toalpha= "0.0"/>

</set>

4, rewrite the construction method and set the click outside can disappear listening

 Super (context);
    This.mcontext=context;


    Pump minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);

    Pep Mcontentview = minflater.inflate (r.layout.layout_dialog,null);


    Set View Setcontentview (Mcontentview);

    Set wide and high setwidth (WindowManager.LayoutParams.MATCH_PARENT);


    SetHeight (WindowManager.LayoutParams.WRAP_CONTENT);


    /** * Set in and out of animation/Setanimationstyle (R.style.mypopupwindow);


    /** * Setting background only this can be clicked outside and back disappears * * setbackgrounddrawable (new colordrawable ());

    /** * settings can get the set point * * Setfocusable (TRUE);

    /** * Set Click on the outside can disappear * * setoutsidetouchable (TRUE);


    /** * settings can touch/settouchable (TRUE); /** * Settings Click External can disappear * * Settouchinterceptor (new View.ontouchlistener () {@Override public boolean O Ntouch (View V, motionevent event) {/** * judgment is not clicked on the external */if (event.getaction () ==motIonevent.action_outside) {return true;
      ///Not click External return false;

 }
    });

5, display and set the window darken and lighten

public void Displaydialog (view view) {

    Mypopupwindow Mypopupwindow = new Mypopupwindow (this);

    Mypopupwindow.showasdropdown (mbtndispaly,0,0);
    Lightoff ();


    /**
     * When the screen becomes bright
    /Mypopupwindow.setondismisslistener (new Popupwindow.ondismisslistener () {
      @ Override public
      void Ondismiss () {
        Windowmanager.layoutparams layoutparams = GetWindow (). GetAttributes ();

        layoutparams.alpha=1.0f;

        GetWindow (). SetAttributes (Layoutparams);}}
    );

  /**
   * Display when the screen dims
   /
  private void Lightoff () {

    Windowmanager.layoutparams layoutparams = GetWindow (). GetAttributes ();

    layoutparams.alpha=0.3f;

    GetWindow (). SetAttributes (layoutparams);

  

6, complete

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; /** * Learning Popupwindow * Created by Liu Nan on 2016/8/1 0001.17:42/public class Mypopupwindow extends Popupwindow {conte
  XT Mcontext;
  Private Layoutinflater Minflater;


  Private View Mcontentview;

    Public Mypopupwindow {Super (context);
    This.mcontext=context;


    Pump minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);

    Pep Mcontentview = minflater.inflate (r.layout.layout_dialog,null);


    Set View Setcontentview (Mcontentview);

    Set wide and high setwidth (WindowManager.LayoutParams.MATCH_PARENT);


    SetHeight (WindowManager.LayoutParams.WRAP_CONTENT); /** * Set in/Out animation/Setanimationstyle(R.style.mypopupwindow);


    /** * Setting background only this can be clicked outside and back disappears * * setbackgrounddrawable (new colordrawable ());

    /** * settings can get the set point * * Setfocusable (TRUE);

    /** * Set Click on the outside can disappear * * setoutsidetouchable (TRUE);


    /** * settings can touch/settouchable (TRUE); /** * Settings Click External can disappear * * Settouchinterceptor (new View.ontouchlistener () {@Override public boolean O Ntouch (View V, motionevent event) {/** * judgment is not clicked on the external */if (event.getaction () ==motioneve Nt.
        Action_outside) {return true;
      ///Not click External return false;


    }
    });

    /** * Initialize view with listener/Initview ();
  Initlistener ();
 private void Initview () {} private void Initlistener () {}}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.