With ScrollView sliding in Android, the TitleBar state changes

Source: Internet
Author: User

Today, the project has a demand, class is a function within the QQ space, so the study of a bit, well, say so much, there may be people who do not know the point is that, directly on. Forgive me for not making a dynamic diagram:


Yes, that is the effect, I studied, the idea is as follows: 1. Monitor the slide of the ScrollView 2. By judging the sliding distance, and then setting the corresponding color gradient to titlebar   but then I found that I could not find the ScrollView listener, that is to say, ScrollView does not provide the corresponding monitoring interface, do not provide even if it, I myself provided the following code:  package com.example.myscrollview; import  android.content.Context;import android.util.AttributeSet;import android.view.View;import  android.widget.scrollview; public class myscrollview extends scrollview {     private ScrollViewListener scrollViewListener = null;      public myscrollview (Context context)  {         super (context);     }     public myscrollview (context)  context, attributeset attrs)  {        super ( Context, attrs);    }      @Override      Protected void onscRollchanged (Int x, int y, int oldx, int oldy)  {         // x is the horizontal axis of the current slider, y represents the ordinate of the current slider, OLDX is the horizontal axis of the previous slide, and the oldy represents the vertical axis of the previous slide          super.onscrollchanged (X, y, oldx, oldy);         if  (scrollviewlistener != null)  {             //  to expose the method here              scrollviewlistener.onscrollchanged (This, x, y, oldx, oldy);         }    }     //   Do you want its elastic sliding      @Override     protected boolean  Overscrollby (int deltax, int deltay, int scrollx,             int scrolly, int scrollrangex, int scrollrangey,             int maxOverScrollX, int maxOverScrollY, boolean  istouchevent)  {         //  Elastic sliding key is maxoverscrollx,   and maxoverscrolly,        //  The general default value is 0, when elasticity is required, change its value          //  that is, zero is not elastic, not 0 (>0, negative number not tested) will slide to its value position          return super.overscrollby (deltax, deltay, scrollx, scrolly,                 scrollrangex,  scrollrangey, 0, 0, istouchevent);    }        Interfaces     public interface ScrollViewListener {          vOid onscrollchanged (View scrollview, int x, int y, int oldx, int  oldy);     }     public void  Setscrollviewlistener (Scrollviewlistener listener)  {         Once the scrollviewlistener = listener;    }}   customization is complete, it can be obtained in the mainactivity:  package com.example.myscrollview; import android.app.Activity;import  android.os.bundle;import android.view.view;import android.widget.relativelayout; import  com.example.myscrollview.myscrollview.scrollviewlistener; public class mainactivity  extends activity {    myscrollview scroll;     relativelayout title;      @Override     protected void  oncreate (bundle savedinstancestate) &NBSP;{&NBSP;&NBsp;      super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);          scroll =  (Myscrollview)  findviewbyid (r.id.scroll);         title =  (relativelayout)  findviewbyid (r.id.title);          //  implementing a listener in a custom control          Scroll.setscrollviewlistener (New scrollviewlistener ()  {               @Override              public void onscrollchanged (View scrollview, int x, int y,                      int oldx, int oldy)  {                // TODO  auto-generated method stub                 titleanima (y);             }         });    }      /**     *  Gradient Effects      */     Public void titleanima (int y)  {        int  Scrollheight = scroll.getchildat (0). GetHeight ()                  - scroll.getheight ();         float scrollPercent =  (float)  y / scrollHeight;          Title.getbackground (). Setalpha ((int)   (255 * scrollpercent));          //  If you have text, you can also set the color gradient of the text         //  Int color = toptext.gettextcolors (). Getdefaultcolor ();         // int r = color.red (Color);         //  int g = color.green (Color);         // int  b = color.blue (Color);        // int  Changetocolor = color.argb ((int)   (255 *  (1 - scrollpercent)), r,  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;G,&NBSP;B);         / / toptext.settextcolor (changetocolor);    } }   My layout file activity_ Main.xml is like this: &NBSP;&LT;RELATIVELAYOUT&NBsp;xmlns:android= "Http://schemas.android.com/apk/res/android"     android:layout_width= " Match_parent "    android:layout_height=" Match_parent " >      <relativelayout        android:id= "@+id/title"          android:layout_width= "Match_parent"          android:layout_height= "48DP"         android:layout_ Alignparenttop= "true"         android:background= "@android: Color/holo_ Red_dark " >         <TextView             android:layout_width= "Match_parent"              android:layout_height= "Wrap_content"               android:gravity= "Center_horizontal"              android:text= "title"  />    </RelativeLayout>      <com.example.myscrollview.myscrollview        android: Id= "@+id/scroll"         android:layout_below= "@id/title"          android:layout_width= "Match_parent"          android:layout_height= "Match_parent"  >          <relativelayout            android:layout _width= "Match_parent"             android:layout_ height= "Match_parent"  >              <linearlayout  &nBsp;             android:id= "@+id/lin"                  android:layout_width = "Match_parent"                  android:layout_height= "Wrap_content"                  android:orientation= "Vertical"  >                  <TextView                     android:layout_width = "Match_parent"                      android:layout_height= "Wrap_content"              &Nbsp;       android:background= "#fff"                      android:padding= "16DP"                      android:text= " HH Adi and France Hius big Hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah speed up Kazakhstan, not to mention the Jiangsu work remember the calligrapher to speed up the struggle to fight Kazakhstan Rebel center Register card hh "                      android:textsize= "40SP"  />                  <TextView                     android:layout_width= "Match_parent"                       Android: layout_height= "Wrap_content"                      android:background= "#fff"                      android:padding= "16DP"                       android:text= " HH Adi and France Hius big Hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah speed up Kazakhstan, not to mention the Jiangsu work remember the calligrapher to speed up the struggle to fight Kazakhstan Rebel center Register card hh "                      android:textsize= "40SP"  />                  <TextView                     android:layout_width= "Match_parent"      &Nbsp;               android:layout_ height= "Wrap_content"                      android:background= "#fff"                      android:padding= "16DP"                       Android:text= " HH Adi and France Hius big Hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah speed up Kazakhstan, not to mention the Jiangsu work remember the calligrapher to speed up the struggle to fight Kazakhstan Rebel center Register card hh "                      android:textsize= "40SP"  />                  <TextView                     android:layout_width= "Match_parent"                      android:layout_ height= "Wrap_content"                      android:background= "#fff"                      android:padding= "16DP"                       Android:text= " HH Adi and France Hius big Hiugfhsdiuhgudhsiuahiudshfuihdsiuhhfsdkjhuiagfhsduiah speed up Kazakhstan, not to mention the Jiangsu work remember the calligrapher to speed up the struggle to fight Kazakhstan Rebel center Register card hh "                      android:textsize= "40SP"  />             </linearlayout>        </relativelayout>    </ com.example.myscrollview.myscrollview> </relativelayout>   is not very simple, do not thank, please call me Lei Feng.  

The titlebar state changes with ScrollView sliding in Android

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.