Android: Custom scrolling Edge (edgeeffect) effect

Source: Internet
Author: User

Android scrollable controls (GridView, ListView, ScrollView, etc.) have an edge feedback effect when the user scrolls to the head, and the default is the Holo blue effect on 4.0. If your app customizes theme colors, such as the orange color of Google Play music. It may look less coordinated to scroll the content controls or to use the default Holo blue edge effect. At this point, you need to customize the edge effect.

The edge effect is implemented by the Edgeeffect class in the Android system by using two system pictures in the constructor of the class to draw the edge effect:

Final Resources res = context.getresources ();        Medge = res.getdrawable (R.drawable.overscroll_edge);        Mglow = res.getdrawable (R.drawable.overscroll_glow);

4.0 The default two images are as follows (click the link to view the image):

    • Overscroll_edge:overscroll_edge.png
    • Overscroll_glow:overscroll_glow.png

So to achieve a custom edge effect, you only need the hack system to use the images provided by your app when getting these two images.

Android apps use the Contextwrapper class to get the resources class, and then use the resource class to get a variety of assets. So by customizing these two classes and applying custom classes to these scrolling controls.

First, customize the resources class to return a custom picture (Resourcesedgeeffect.java) If you decide that you need to get the two edge slices above:

PublicClassResourcesedgeeffectExtendsResources {  Privateint Overscroll_edge = Getplatformdrawableid ("Overscroll_edge");  Privateint overscroll_glow = Getplatformdrawableid ("Overscroll_glow");  Public Resourcesedgeeffect (Assetmanager assets, displaymetrics Metrics, Configuration config) {   Super (assets, Metrics, config); }  Privateint Getplatformdrawableid (String name) {   try {    int i = ((Integer) Class.forName ("Com.android.internal.r$drawable"). GetField (name). Get (NULL). Intvalue ();    return i;  }catch (ClassNotFoundException e) {   LOG.E ("[Contextwrapperedgeeffect].getplatformdrawableid ()","Cannot find internal resource class");    Return0;  }catch (Nosuchfieldexception E1) {   LOG.E ("[Contextwrapperedgeeffect].getplatformdrawableid ()","Internal resource ID does not exist:" + name);    Return0;  }catch (illegalargumentexception E2) {   LOG.E ("[Contextwrapperedgeeffect].getplatformdrawableid ()","Cannot access internal resource ID:" + name);    Return0;  }catch (Illegalaccessexception E3) {   LOG.E ("[Contextwrapperedgeeffect].getplatformdrawableid ()","Cannot access internal resource ID:" + name);  }   Return0; }  Public drawable getdrawable (int resId)Throws Resources.notfoundexception {   if (resId = =This.overscroll_edge)    return Contextwrapperedgeeffect. this.getbasecontext (). Getresources (). getdrawable (R.drawable.overscroll_edge);    if (resId = this.overscroll_glow)     return contextwrapperedgeeffect. This.getbasecontext (). Getresources (). getdrawable (R.drawable.overscroll_glow);    return  Super.getdrawable (RESID);  }}        /span>                

Then customize a Contextwrapper Class (Contextwrapperedgeeffect.java):

PublicClassContextwrapperedgeeffectExtendsContextwrapper { PrivateStatic Resourcesedgeeffect Res_edge_effect; public contextwrapperedgeeffect (Context context) {  super (context);  Resources resources = Context.getresources ();   if (res_edge_effect = null )    Res_edge_effect = new Resourcesedgeeffect (Resources.getassets (), Resources.getdisplaymetrics (), resources.getconfiguration ());  //return custom Resources public Resources getresources () {  return res_edge_effect; }              /span>        

Finally, customize the scroll controls used in the app, Replace the context object with the previously customized Contextwrapperedgeeffect class (This is the example of the GridView):

public class gridview extends android. Widgets. gridview { public GridView (context context, AttributeSet attrs) {  super ( new contextwrapperedgeeffect (context), attrs); } public GridView (context context, AttributeSet Attrs, int defstyle) {  super (new contextwrapperedgeeffect (context), attrs, Defstyle); }              /span>         

Then have your UE classmate follow the two graphs of the holo Blue edge effect to provide a custom two-image.

If you feel that these steps are more cumbersome, you can also download edgeeffectoverride this project, the project has implemented the ListView, GridView, Scrollveiw, Expandablelistview and Viewpager class, download this project only need to replace two pictures.

is a custom red effect:

Android: Custom scrolling Edge (edgeeffect) 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.