Android Development: Parallax effect of Scrollerview, adapted from Parallaxlistview

Source: Internet
Author: User

Recently in the project, useful to a follow the path of the parallax effect, hard to search, on GitHub, there is a similar effect, but the ListView, plus a top of the headerview, to achieve the effect, But what I need is scrollerview, so the code is modified to achieve the scrollerview below the parallax effect, refer to the following:



Before reading the code below, you can check the source above GitHub


I have a lot of limitations on the original code, only to achieve the effect I need, it seems easy to understand, the most afraid of the kind of around the code, look at the core implementation code:

public class Parallaxscollview extends ScrollView implements Onscrolllistener {public final static double no_zoom = 1;    Public final static Double zoom_x2 = 2;    Private ImageView Mimageview;    private int mimageviewheight =-1;    private int mdefaultimageviewheight = 0;    private int originimageviewheight;    private int mmaxheight;                                    Private interface Onoverscrollbylistener {public boolean overscrollby (int deltax, int deltay, int scrollx, int scrolly, int scrollrangex, int scrollrangey, int maxove    RSCROLLX, int maxoverscrolly, Boolean istouchevent);    } Private interface Ontoucheventlistener {public void ontouchevent (motionevent ev);        } public Parallaxscollview (context context, AttributeSet attrs, int defstyle) {        Super (context, attrs, Defstyle);    Init (context);     } public Parallaxscollview (context context, AttributeSet Attrs) {   Super (context, attrs);    Init (context);        } public Parallaxscollview (context context) {super (context);    Init (context); } public void init (context context) {mdefaultimageviewheight = Context.getresources (). Getdimensionpixelsize (R.D        Imen.size_default_height);    Originimageviewheight = Context.getresources (). Getdimensionpixelsize (R.dimen.size_default_height); } @Override public void onscrollstatechanged (Abslistview view, int scrollstate) {} @Override protected Boo Lean Overscrollby (int deltax, int deltay, int scrollx, int scrolly, int scrollrangex, in T scrollrangey, int maxoverscrollx, int maxoverscrolly, Boolean istouchevent) {b                Oolean iscollapseanimation = false; Iscollapseanimation = Scrollbylistener.overscrollby (DeltaX, DeltaY, Scrollx, scrolly, Scrollrangex, ScrollR Angey, MAXOVERSCROLLX, maxoverscrolly, IStouchevent) | |        Iscollapseanimation; /*return iscollapseanimation?                True:super.overScrollBy (DeltaX, DeltaY, Scrollx, scrolly, Scrollrangex, Scrollrangey, MAXOVERSCROLLX,    0, istouchevent); */return false; } @Override public void onscroll (Abslistview view, int firstvisibleitem, int Visibleitemcoun    T, int totalitemcount) {} @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {    Super.onmeasure (Widthmeasurespec, Heightmeasurespec); } @Override protected void onscrollchanged (int l, int t, int oldl, int Oldt) {super.onscrollchanged (L, T, OL        DL, Oldt);        View Firstview = (view) mimageview.getparent (); Firstview.gettop < getpaddingtop means mimageview would be covered by top padding//So we can layout it to M Ake it shorter if (Firstview.gettop () < Getpaddingtop () && mimageview.getheight () > MimageViewheight) {mimageview.getlayoutparams (). Height = Math.max (mimageview.getheight ()-(Getpaddingtop ()-FirstV            Iew.gettop ()), mimageviewheight); The to set of the Firstview.mtop to 0,///maybe Use view.settop () are more easy, but it just support from Android 3.            0 (API one) firstview.layout (Firstview.getleft (), 0, Firstview.getright (), Firstview.getheight ());        Mimageview.requestlayout ();        }} @Override public boolean ontouchevent (motionevent ev) {touchlistener.ontouchevent (EV);    return super.ontouchevent (EV);        } public void Setparallaximageview (ImageView iv) {Mimageview = IV;    Mimageview.setscaletype (ImageView.ScaleType.CENTER_CROP); } public void Setviewsbounds (double zoomratio) {if (mimageviewheight = =-1) {mimageviewheight = MIm            Ageview.getheight ();            if (mimageviewheight <= 0) {mimageviewheight = Mdefaultimageviewheight;    }        Double ratio = (double) mimageview.getdrawable (). Getintrinsicwidth ())/((double) mimageview.getwidth ());  }} private Onoverscrollbylistener Scrollbylistener = new Onoverscrollbylistener () {@Override public Boolean Overscrollby (int deltax, int deltay, int scrollx, int scrolly, int scrollrange            X, int scrollrangey, int maxoverscrollx, int maxoverscrolly, Boolean istouchevent) { if (istouchevent) {if (true) {Mimageview.getlayoutparams (). Height = Mimag                        Eview.getheight ()-DELTAY/2;                Mimageview.requestlayout ();        }} return false;    }    }; Private Ontoucheventlistener Touchlistener = new Ontoucheventlistener () {@Override public void ontouchevent (Motionevent ev) {if (ev.getaction () = = motionevent.action_up) {if (mimageviewheight -1 < Mimageview.getheight ()) {resetanimimation animation = new Resetanimimation (                    Mimageview, Mimageviewheight);                    Animation.setduration (300);                Mimageview.startanimation (animation);    }            }        }    };        public class Resetanimimation extends Animation {int targetheight;        int originalheight;        int extraheight;        View MView;            Protected resetanimimation (view view, int targetheight) {This.mview = view;            This.targetheight = Targetheight;            OriginalHeight = View.getheight ();            Extraheight = This.originalheight-originimageviewheight;        LOG.I ("Debug", "Target heitht" + targetheight + "original height" + originalheight + "Extraheight" + extraheight);            } @Override protected void applytransformation (float interpolatedtime, transformation t) {            int newheight; NeWheight = (int) (Originimageviewheight + extraheight * (1-interpolatedtime));            Mview.getlayoutparams (). height = newheight;        Mview.requestlayout (); }    }}


Second cloth: In XML, refer to the Parallaxscollview:

<com.gnod.parallaxlistview.parallaxscollview xmlns:android= "Http://schemas.android.com/apk/res/android" Android:id= "@+id/parallax_scroll_view" android:layout_width= "match_parent" android:layout_height= "Match_parent" a ndroid:orientation= "vertical" android:overscrollmode= "never" > <linearlayout android:layout_width= "MATC            H_parent "android:layout_height=" match_parent "android:orientation=" vertical "> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:orientati on= "Horizontal" > <imageview android:id= "@+id/headview" Android:layout_widt                H= "Match_parent" android:layout_height= "120DP" android:scaletype= "Centercrop" android:src= "@drawable/img_header"/> </LinearLayout> <textview android:id= "@+id/tex TView1 "Android:layout_wiDth= "Wrap_content" android:layout_height= "wrap_content" android:layout_margin= "15DP" Android            oid:text= "Large text" android:textappearance= "? Android:attr/textappearancelarge"/> <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_margi        N= "15DP" android:text= "Large text" android:textappearance= "? Android:attr/textappearancelarge"/>            <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_margin= "15DP" android:text= "Large text" android:textappearance= "? Android:attr/tex Tappearancelarge "/> <textview android:layout_width=" Wrap_content "Android:layout_heigh t= "Wrap_content" android:layout_margin= "15DP" android:text= "Large text" Android:textappea Rance= "? Android:attr/texTappearancelarge "/> <textview android:layout_width=" Wrap_content "Android:layout_heigh t= "Wrap_content" android:layout_margin= "15DP" android:text= "Large text" Android:textappea            Rance= "Android:attr/textappearancelarge"/> <textview android:layout_width= "Wrap_content"            android:layout_height= "Wrap_content" android:layout_margin= "15DP" android:text= "Large text" Android:textappearance= "? Android:attr/textappearancelarge"/> <textview Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" android:layout_margin= "15DP" Androi            d:text= "Large text" android:textappearance= "? Android:attr/textappearancelarge"/> <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margin= "15DP" Android:text= "Large text" android:textappearance= "? Android:attr/textappearancelarge"/> <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margin=        "15DP" android:text= "Large text" android:textappearance= "Android:attr/textappearancelarge"/>            <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_margin= "15DP" android:text= "Large text" android:textappearance= "? Android:attr/texta Ppearancelarge "/> <textview android:layout_width=" Wrap_content "android:layout_height= "Wrap_content" android:layout_margin= "15DP" android:text= "Large text" Android:textappeara Nce= "? Android:attr/textappearancelarge"/> </linearlayout></ Com.gnod.parallaxlistview.parallaxscollview>

The last step, in activity, refers to Parallaxscrollerview and sets ImageView:


Mimageview = (ImageView) Findviewbyid (R.id.headview);        ScrollView = (Parallaxscollview) Findviewbyid (R.id.parallax_scroll_view);        Scrollview.setparallaximageview (Mimageview);


You can also achieve cool parallax effect on Scrollerview.


Source Portal









Android Development: Parallax effect of Scrollerview, adapted from Parallaxlistview

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.