47. Android custom PopupWindow skills

Source: Internet
Author: User

47. Android custom PopupWindow skills
47. Android custom PopupWindow skills

Android custom PopupWindow skills preface PopupWindow wide and high PopupWindow position Animation

Preface

In fact, the customization process of PopupWindow is very simple, and the only headache is:Locate the problem displayed in PopupWindow.

It is especially disgusting to locate the problem:Sometimes it involves the wide and high issue of PopupWindow.. We all know that the width and height cannot be obtained before the show operation. The width and height must be determined during the show operation, which is so contradictory. Below,All give answers.

The second problem is:Design of the display and disappear animation of PopupWindow.

The following three simple locating methods are also provided in my custom PopupWindow:

Display in the lower left position of the control

Position displayed under the control

Display in the lower right position of the control

PopupWindow width and height

When PopupWindow is initialized,Force draw Layout, and get the width and height of PopupWindow.

// Used to save the width private int width of PopupWindow; // used to save the height private int height of PopupWindow; public CustomPopupWindow (Activity activity) {super (activity); this. activity = activity; this. initPopupWindow ();} private void initPopupWindow () {LayoutInflater inflater = (LayoutInflater) activity. getSystemService (Context. LAYOUT_INFLATER_SERVICE); this. contentView = inflater. inflate (R. layout. popupwindow_custom, null); this. setContentView (contentView); // you can specify the width of the pop-up window. setWidth (ViewGroup. layoutParams. WRAP_CONTENT); // set the height of the pop-up form this. setHeight (ViewGroup. layoutParams. WRAP_CONTENT); // you can click this to set the pop-up form. setTouchable (true); this. setFocusable (true); // sets whether the click disappears. this. setOutsideTouchable (true); // sets the animation effect of the pop-up form. this. setAnimationStyle (R. style. popupAnimation); // instantiate a ColorDrawable color as translucent ColorDrawable background = new ColorDrawable (0x4f000000); // set the background of the pop-up form this. setBackgroundDrawable (background); // draw this. mandatoryDraw ();}/*** force draw popupWindowView and initialize the size of popupWindowView */private void mandatoryDraw () {this. contentView. measure (View. measureSpec. UNSPECIFIED, View. measureSpec. UNSPECIFIED);/*** obtain the width and height of the PopupWindow after force refreshing */this. width = this. contentView. getMeasuredWidth (); this. height = this. contentView. getMeasuredHeight ();}
Position PopupWindow on the lower left
/*** Display in the lower left of the control ** @ param 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]; // obtain the absolute coordinate parent of the entire screen. getLocationOnScreen (location); this. showAtLocation (parent, 0, location [0], location [1] + parent. getHeight ());}}
Position PopupWindow in the center
/*** Display in the control. ** @ param 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]; // obtain the absolute coordinate parent of the entire screen. getLocationOnScreen (location); this. showAtLocation (parent, 0, location [0]/2 + parent. getWidth ()/2-this. width/6, location [1] + parent. getHeight ());}}
Position PopupWindow at the bottom right
/*** Display in the lower right of the control ** @ param 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]; // obtain the absolute coordinate parent of the entire screen. getLocationOnScreen (location); this. showAtLocation (parent, 0, location [0] + parent. getWidth ()-this. width, location [1] + parent. getHeight ());}}
PopupWindow Animation

Design an animation that is gradually displayed in the upper right corner at the beginning of PopWindow, and then gradually collapsed in the upper right corner at the end:

Styles. xml

Popwindow_in.xml

<code class="language-xml hljs "><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator">    <!--{cke_protected}{C}%3C!%2D%2D%0A%20%20%20%20%20%20%20%20%E6%97%B6%E9%97%B4%200.2%E7%A7%92%0A%20%20%20%20%20%20%20%20%E5%BC%80%E5%A7%8B%E7%9A%84%E6%97%B6%E5%80%99%20x%20y%20%E5%85%A8%E6%98%AF0%20%E6%B2%A1%E6%9C%89%E5%A4%A7%E5%B0%8F%0A%20%20%20%20%20%20%20%20%E7%BB%93%E6%9D%9F%E7%9A%84%E6%97%B6%E5%80%99%20x%20y%20%E5%85%A8%E6%98%AF1%20%E5%AE%9E%E9%99%85%E5%A4%A7%E5%B0%8F%0A%20%20%20%20%20%20%20%20pivotX%20100%25%20%E8%A1%A8%E7%A4%BA%E6%9C%80%E5%8F%B3%E8%BE%B9%0A%20%20%20%20%20%20%20%20pivotY%200%25%20%E8%A1%A8%E7%A4%BA%E6%9C%80%E9%A1%B6%E8%BE%B9%0A%20%20%20%20%20%20%20%20%E4%BB%A5%E4%B8%8A%E5%AE%9A%E4%BD%8D%E5%8F%B3%E4%B8%8A%E8%A7%92%20%E7%BC%A9%E6%94%BE%E6%97%B6%E4%B8%8D%E5%8F%98%E4%BD%8D%E7%BD%AE%0A%20%20%20%20%2D%2D%3E-->    <scale android:duration="200" android:fromxscale="0.0" android:fromyscale="0.0" android:pivotx="100%" android:pivoty="0%" android:toxscale="1.0" android:toyscale="1.0">    <!--{cke_protected}{C}%3C!%2D%2D%0A%20%20%20%20%20%20%20%20%E6%97%B6%E9%97%B4%200.2%E7%A7%92%0A%20%20%20%20%20%20%20%20%E5%BC%80%E5%A7%8B%E5%85%A8%E9%80%8F%E6%98%8E%0A%20%20%20%20%20%20%20%20%E7%BB%93%E6%9D%9F%E4%B8%80%E7%82%B9%E9%83%BD%E4%B8%8D%E9%80%8F%E6%98%8E%0A%20%20%20%20%2D%2D%3E-->    <alpha android:duration="200" android:fromalpha="0.0" android:toalpha="1.0"></alpha></scale></set></code>

Popwindow_out.xml

<code class="language-xml hljs "><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator">    <!--{cke_protected}{C}%3C!%2D%2D%0A%20%20%20%20%20%20%20%20%E6%97%B6%E9%97%B4%200.2%E7%A7%92%0A%20%20%20%20%20%20%20%20%E5%BC%80%E5%A7%8B%E7%9A%84%E6%97%B6%E5%80%99%20x%20y%20%E5%85%A8%E6%98%AF1%20%E5%AE%9E%E9%99%85%E5%A4%A7%E5%B0%8F%0A%20%20%20%20%20%20%20%20%E7%BB%93%E6%9D%9F%E7%9A%84%E6%97%B6%E5%80%99%20x%20y%20%E5%85%A8%E6%98%AF0%20%E6%B2%A1%E6%9C%89%E5%A4%A7%E5%B0%8F%0A%20%20%20%20%20%20%20%20pivotX%20100%25%20%E8%A1%A8%E7%A4%BA%E6%9C%80%E5%8F%B3%E8%BE%B9%0A%20%20%20%20%20%20%20%20pivotY%200%25%20%E8%A1%A8%E7%A4%BA%E6%9C%80%E9%A1%B6%E8%BE%B9%0A%20%20%20%20%20%20%20%20%E4%BB%A5%E4%B8%8A%E5%AE%9A%E4%BD%8D%E5%8F%B3%E4%B8%8A%E8%A7%92%20%E7%BC%A9%E6%94%BE%E6%97%B6%E4%B8%8D%E5%8F%98%E4%BD%8D%E7%BD%AE%0A%20%20%20%20%2D%2D%3E-->    <scale android:duration="200" android:fromxscale="1.0" android:fromyscale="1.0" android:pivotx="100%" android:pivoty="0%" android:toxscale="0.0" android:toyscale="0.0">    <!--{cke_protected}{C}%3C!%2D%2D%0A%20%20%20%20%20%20%20%20%E6%97%B6%E9%97%B4%200.2%E7%A7%92%0A%20%20%20%20%20%20%20%20%E5%BC%80%E5%A7%8B%E4%B8%80%E7%82%B9%E9%83%BD%E4%B8%8D%E9%80%8F%E6%98%8E%0A%20%20%20%20%20%20%20%20%E7%BB%93%E6%9D%9F%E5%85%A8%E9%80%8F%E6%98%8E%0A%20%20%20%20%2D%2D%3E-->    <alpha android:duration="200" android:fromalpha="1.0" android:toalpha="0.0"></alpha></scale></set></code>
Custom PopupWindow
Public class CustomPopupWindow extends android. widget. popupWindow {private Activity activity; private View contentView; // used to save the width of PopupWindow private int width; // used to save the height of PopupWindow private int height; public CustomPopupWindow (Activity activity) {super (activity); this. activity = activity; this. initPopupWindow ();} private void initPopupWindow () {LayoutInflater inflater = (LayoutInflater) activi Ty. getSystemService (Context. LAYOUT_INFLATER_SERVICE); this. contentView = inflater. inflate (R. layout. popupwindow_custom, null); this. setContentView (contentView); // you can specify the width of the pop-up window. setWidth (ViewGroup. layoutParams. WRAP_CONTENT); // set the height of the pop-up form this. setHeight (ViewGroup. layoutParams. WRAP_CONTENT); // you can click this to set the pop-up form. setTouchable (true); this. setFocusable (true); // sets whether the click disappears. this. setOutsideTouchable (true); // set Set the form animation effect this. setAnimationStyle (R. style. popupAnimation); // instantiate a ColorDrawable color as translucent ColorDrawable background = new ColorDrawable (0x4f000000); // set the background of the pop-up form this. setBackgroundDrawable (background); // draw this. mandatoryDraw ();}/*** force draw popupWindowView and initialize the size of popupWindowView */private void mandatoryDraw () {this. contentView. measure (View. measureSpec. UNSPECIFIED, View. measureSpec. UNSPECIFIED );/*** Forcibly refresh and get the width and height of PopupWindow */this. width = this. contentView. getMeasuredWidth (); this. height = this. contentView. getMeasuredHeight ();}/*** display in the lower right of the control ** @ param 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]; // obtain the absolute coordinate parent of the entire screen. getLocationOnScreen (location ); This. showAtLocation (parent, 0, location [0] + parent. getWidth ()-this. width, location [1] + parent. getHeight () ;}}/*** display in the lower left corner of the control ** @ param 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]; // obtain the absolute coordinate parent of the entire screen. getLocationOnScreen (location); this. showAtLo Cation (parent, 0, location [0], location [1] + parent. getHeight () ;}}/*** is displayed under the control. ** @ param 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]; // obtain the absolute coordinate parent of the entire screen. getLocationOnScreen (location); this. showAtLocation (parent, 0, location [0]/2 + parent. ge TWidth ()/2-this. width/6, location [1] + parent. getHeight () ;}} public static class PopupWindowBuilder {private static String activityHashCode; private static CustomPopupWindow popupWindow; public static PopupWindowBuilder ourInstance; public static PopupWindowBuilder getInstance (Activity activity) {if (ourInstance = null) ourInstance = new PopupWindowBuilder (); String hashCode = String. v AlueOf (activity. hashCode ();/*** different Activity */if (! HashCode. equals (String. valueOf (activityHashCode) {activityHashCode = hashCode; popupWindow = new CustomPopupWindow (activity);} return ourInstance;} public PopupWindowBuilder setTouchable (boolean touchable) {popupWindow. setTouchable (touchable); return this;} public PopupWindowBuilder setAnimationStyle (int animationStyle) {popupWindow. setAnimationStyle (animationStyle); return this;} public PopupWindowBuilder setBackgroundDrawable (Drawable background) {popupWindow. setBackgroundDrawable (background); return this;} public CustomPopupWindow getPopupWindow () {popupWindow. update (); return popupWindow ;}}}

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.