Android: Inherit ScrollView implement custom up scrolling popup (translucent background)

Source: Internet
Author: User

Now the common effect: Click the button up pop-up box to display information, pop-up after the background becomes translucent, and support gestures to slide off the popup box.

As follows:






The following code:


1. Core class: Custom Up Popup box Verticalscrollview.java

Package App.popupbox.view;import Android.app.activity;import Android.content.context;import Android.graphics.Color ; Import Android.util.attributeset;import Android.util.displaymetrics;import Android.view.motionevent;import Android.view.view;import Android.view.viewgroup;import Android.view.windowmanager;import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.translateanimation;import android.widget.scrollview;/** * @author */public class        Verticalscrollview extends ScrollView {/** screen height */private int mscreenheight;        Private Boolean isOpen = false;        Private Boolean IsFirst = true;        Private View Mvwbackground;        Private ViewGroup Mviewgroup;        Private ViewGroup mvwcontent;    Public Verticalscrollview (Context context) {super (context);    } public Verticalscrollview (context context, AttributeSet Attrs) {Super (context, attrs); } public VerticalsCrollview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); }/* * (non-javadoc) * @see android.widget.scrollview#onmeasure (int, int) */@Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {if (IsFirst) {Mviewgroup = (viewgroup) GE            Tchildat (0);            Mvwbackground = Mviewgroup.getchildat (0);                        Mvwcontent = (viewgroup) mviewgroup.getchildat (1);            Mscreenheight = GETSCREEMH ();            Mvwbackground.getlayoutparams (). height = mscreenheight;            Mvwcontent.getlayoutparams (). Height = mscreenheight * 3/5;            SetVisible (FALSE);        IsFirst = false;    } super.onmeasure (Widthmeasurespec, Heightmeasurespec); }/* * (non-javadoc) * @see android.widget.scrollview#onlayout (boolean, int, int, int, int) */@Over Ride protected void OnLayout (Boolean changed, int l, int t, int r, int b){//TODO auto-generated Method stub super.onlayout (changed, L, T, R, b);        if (changed) {this.smoothscrollto (0, mscreenheight);        }} @Override public boolean ontouchevent (motionevent ev) {int action = ev.getaction ();                Switch (action) {case MotionEvent.ACTION_UP:int i = getscrolly (); If the sliding distance is greater than 2/3 of the screen, it pops up, where you can customize if (i > Mscreenheight * 2/3) {Smoothscrollto (0, Mscre                    Enheight);                IsOpen = true;                    } else {Traslatey (0, mscreenheight);                    Mviewgroup.setbackgroundcolor (color.transparent);                IsOpen = false;                        } return true;    } return super.ontouchevent (EV); }/** * Close * * @description * @date 2014-12-5 */public void closecontent () {if (IS          Open) {  Traslatey (0, mscreenheight);            Mviewgroup.setbackgroundcolor (color.transparent);        IsOpen = false;  }}/** * Open * * @description * @date 2014-12-5 */public void opencontent () {if (!isopen)            {setvisible (true);            Mviewgroup.setbackgroundcolor (Color.argb (66, 0, 0, 0));            Traslatey (mscreenheight, 0);        IsOpen = true;    }}/** * for sliding animation * * @description * @date 2014-12-5 * @param Oldt * @param t */ public void Traslatey (int oldt, int t) {translateanimation animation = new Translateanimation (0, 0, Oldt,        T);                Animation.setduration (600);        Animation.setfillafter (TRUE);        Mvwcontent.startanimation (animation); Animation.setanimationlistener (New Animationlistener () {@Override public void Onanimati OnStart (Animation Animation) {//TODO auto-generated Method stub} @Override public void Onanimationrepeat (Animat Ion animation) {//TODO auto-generated method stub} @Ov Erride public void Onanimationend (Animation Animation) {//sliding End If the close condition is met if (!iso                    pen) {setvisible (false);                ScrollTo (0, mscreenheight);    }                            }        }); /** * Call this method to operate the popup box * * @description * @date 2014-12-5 */public void Toggle () {if (IsOpen)        {closecontent ();        } else {opencontent ();    }}/** pop-up box pops up */public boolean isOpen () {return isOpen;  }/** * * @description * @date 2014-12-5 * @param visible */public void setvisible (Boolean Visible) {if (visible) {MVIEWGROUP.SETVISibility (VISIBLE);        This.setvisibility (VISIBLE);            } else {mviewgroup.setvisibility (GONE);        This.setvisibility (GONE); }}/** * Get screen Height * * @return int */@SuppressWarnings ("deprecation") private int        GETSCREEMH () {WindowManager wm = (Activity) getcontext ()). Getwindowmanager ();    Return Wm.getdefaultdisplay (). GetHeight (); }}

2. Main layout: Layout_pop_up_box_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Butt On android:id= "@+id/btn" android:layout_width= "match_parent" android:layout_height= "50DP" Android Oid:layout_alignparentbottom= "True" android:text= "click to Eject"/> <app.popupbox.view.verticalscrollview an        Droid:id= "@+id/verticalscrollview" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_above= "@id/btn" android:layout_alignparentbottom= "true" android:layout_marginbottom= "5 0DP "android:scrollbars=" None "> <linearlayout android:id=" @+id/dialog_view "and             Roid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:background= "#66000000" android:orientation= "Vertical" &GT                <view android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:background= "#00000000"/> <include layout= "@layout/dialog_view"/> </linearlayo Ut> </app.popupbox.view.VerticalScrollView></RelativeLayout>

3, pop-up box layout: dialog_view.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" 300DP "    android:background=" @ Android:color/holo_blue_dark "    android:orientation=" vertical ">    <textview        android:layout_width = "Wrap_content"        android:layout_height= "wrap_content"        android:layout_centerinparent= "true"        Android: text= "pop-up box"/></relativelayout>


4, Popupboxmainactivity.java

Package App.popupbox;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.button;import App.popupbox.view.verticalscrollview;public class Popupboxmainactivity extends Activity {private Verticalscrollvie        W Mverticalscrollview;        Private Button Mbutton;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);                Setcontentview (R.layout.layout_pop_up_box_main);    Initview (); }/** * Initialize control */private void Initview () {Mverticalscrollview = (Verticalscrollview) findviewbyi        D (R.id.verticalscrollview);                Mbutton = (Button) Findviewbyid (R.ID.BTN);  Mbutton.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {MverticalsCrollview.toggle ();            Updateview ();    }        }); }/** * Update interface */private void Updateview () {if (Mverticalscrollview.isopen ()) {Mbutto        N.settext ("click Close");        } else {Mbutton.settext ("Click to Eject"); }    }}


5. Engineering structure



Android: Inherit ScrollView implement custom up scrolling popup (translucent background)

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.