[Android UI] Actionbar drag up and down with scorllview and transparency gradient effect

Source: Internet
Author: User

I see more and more applications using such effects, such as the main interface of QQ Space 5.0, really good looking! Probably on the search for the relevant implementation methods, found that there are long-time related solutions:

Imitation QQ space rolling Actionbar Transparency Change Demo

And the way I see it on GitHub, this is the main core of this blog post:

For details, please see: Https://github.com/AChep/Header2ActionBar

The effect is as follows:


This is the demo structure:


1.fadingactionbarhelper.java This class is the core class that handles Actionbar and handles the processing of actionbar background color alpha for scroll events.

public class Fadingactionbarhelper {private static final String TAG = "Fadingactionbarhelper";    private int malpha = 255;    Private drawable mdrawable;    Private Boolean isalphalocked;    Private final ActionBar Mactionbar;    Public Fadingactionbarhelper (Final ActionBar ActionBar) {mactionbar = ActionBar;        } public Fadingactionbarhelper (final ActionBar ActionBar, final drawable drawable) {mactionbar = ActionBar;    Setactionbarbackgrounddrawable (drawable); } public void Setactionbarbackgrounddrawable (drawable drawable) {setactionbarbackgrounddrawable (Drawable, True)    ; } @TargetApi (Build.version_codes. KITKAT) public void setactionbarbackgrounddrawable (Drawable drawable, Boolean mutate) {mdrawable = mutate? drawabl        E.mutate (): drawable;        Mactionbar.setbackgrounddrawable (mdrawable); if (Malpha = = 255) {if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) Malpha = Mdrawable.getalpha ();        } else {Setactionbaralpha (malpha);     }}/** * An {@link android.app.ActionBar} background drawable.     * * @see #setActionBarBackgroundDrawable (android.graphics.drawable.Drawable) * @see #setActionBarAlpha (int) */    Public drawable getactionbarbackgrounddrawable () {return mdrawable;     }/** * Use the This method for global changes only! * This is helpful if you need to provide something like * Navigation drawer:lock ActionBar and set * {@link and     Roid.graphics.drawable.drawable#setalpha (int)} * to {@link #getActionBarBackgroundDrawable ()} directly.     * * @param alpha A value from 0 to 255 * @see #getActionBarBackgroundDrawable () * @see #getActionBarAlpha () */public void Setactionbaralpha (int alpha) {if (mdrawable = = null) {LOG.W (TAG, "Set Action Bar B            Ackground before setting the Alpha level! ");        Return } if (!isalphalocked) {mdrawabLe.setalpha (Alpha); View view = Mactionbar.getcustomview (), if (view!=null) {//This is the process of customizing the actionbar background, and I'm going to have a sloppy one here. if (alpha>=55) { View.findviewbyid (R.id.search_button). Setbackgroundresource (R.drawable.search); View.findviewbyid (R.id.refresh_ button). Setbackgroundresource (R.drawable.refresh);} Else{view.findviewbyid (R.id.search_button). Setbackgroundresource (R.drawable.skin_nav_icon_l_search_rev); View.findviewbyid (R.id.refresh_button). Setbackgroundresource (R.drawable.skin_nav_icon_r_refresh_rev);}        LOG.I (TAG, "search_button.alpha=>" +alpha);}}    Malpha = Alpha;    } public int Getactionbaralpha () {return malpha; }/** * When ActionBar's alpha is locked {@link #setActionBarAlpha (int.)} * won ' t change Drawable\ 's alpha (but Would change {@link #getActionBarAlpha ()} level) * * @param lock */public void setactionbaralphalocked (bool EAN lock) {//Update alpha level on unlock if (isalphalocked! = (isalphalocked = lock) &&!isalphal ocked){Setactionbaralpha (Malpha);    }} public Boolean isactionbaralphalocked () {return isalphalocked; }}

2. Other component classes I am not copy, interested friends to download the project on GitHub on their own O (∩_∩) O.

public class Notifyingscrollview extends ScrollView {//edge-effects don ' t mix well with the translucent action bar in    Android 2.X Private Boolean mdisableedgeeffects = true; /** * @author Cyril mottier * * Public interface Onscrollchangedlistener {void onscrollchanged (Scrollvie    W who, int l, int t, int oldl, int oldt);    } private Onscrollchangedlistener Monscrollchangedlistener;    Public Notifyingscrollview (Context context) {super (context);    } public Notifyingscrollview (context context, AttributeSet Attrs) {Super (context, attrs); } public Notifyingscrollview (context context, AttributeSet attrs, int defstyle) {Super (context, Attrs, Defstyle    ); } @Override protected void onscrollchanged (int l, int t, int oldl, int Oldt) {super.onscrollchanged (L, T, OL        DL, Oldt);        if (Monscrollchangedlistener! = null) {monscrollchangedlistener.onscrollchanged (this, L, T, OLDL, Oldt); }} PublIC void Setonscrollchangedlistener (Onscrollchangedlistener listener) {Monscrollchangedlistener = listener;        } @Override protected Float gettopfadingedgestrength () {//http://stackoverflow.com/a/6894270/244576 if (mdisableedgeeffects && Build.VERSION.SDK_INT < Build.version_codes.        Honeycomb) {return 0.0f;    } return Super.gettopfadingedgestrength ();        } @Override protected Float getbottomfadingedgestrength () {//http://stackoverflow.com/a/6894270/244576 if (mdisableedgeeffects && Build.VERSION.SDK_INT < Build.version_codes.        Honeycomb) {return 0.0f;    } return Super.getbottomfadingedgestrength (); }}


3. For ordinary Java programmers, you can not know how a principle, but you must know how to use it, how to apply to our actual project and to transform it, this is the main interface of the demo, use up a few lines of code, is not very diao?

public class Mainactivity extends Activity {private Fadingactionbarhelper mfadingactionbarhelper;    Private ActionBar Mactionbar;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Mactionbar = Getactionbar ();        Use the custom layout of the ActionBar mactionbar.setdisplayoptions (Actionbar.display_show_custom);        Mactionbar.setcustomview (R.layout.my_actionbar); Defines the background color of white for actionbar mfadingactionbarhelper = new Fadingactionbarhelper (Getactionbar (), getresources        (). Getdrawable (R.DRAWABLE.ACTIONBAR_BG)); if (savedinstancestate = = null) {Getfragmentmanager (). BeginTransaction (). Add (R.id.containe        R, New Listviewfragment ()). commit ();  }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds items To the action Bar if it is presEnt.        The menu page is hidden//Getmenuinflater () by the effect. Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.    return super.onoptionsitemselected (item);    }//This code can not forget the public fadingactionbarhelper Getfadingactionbarhelper () {return mfadingactionbarhelper; }}

Paste the main code, in fact, the implementation of the way is a bit complicated, I think there should be a more concise way, some words please leave a message to share! Sharing code is an effective way to make progress in the technology circle Oh!


----------------------------------------------------------

Finally is the download link of the demo, interested please download it yourself, open Ruyi door


[Android UI] Actionbar drag up and down with scorllview and transparency gradient effect

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.