Dynamically set the width of the picture above the ListView according to the Finger Movement event of the ListView, and when the finger is released
Animating a picture (animated values over time) Valueanimator.ofint ()
ImportAndroid.animation.ValueAnimator;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.ViewGroup;ImportAndroid.view.animation.OvershootInterpolator;ImportAndroid.widget.ImageView;ImportAndroid.widget.ListView;/*** Created by Lxj on 2017/1/5.*/ Public classParallaxlistviewextendsListView { PublicParallaxlistview (Context context) {Super(context); } PublicParallaxlistview (Context context, AttributeSet attrs) {Super(context, attrs); } PublicParallaxlistview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } intOriginalHeight;//The initial height is 120dp.ImageView image; intMaxHeight;//The maximum height of a child is set to the height of the image itself. Public voidsetparallaximage (ImageView image) { This. Image =image; //reads the value of Image_height from the Dimens file and automatically transitions to pixelsOriginalHeight =getresources (). Getdimensionpixelsize (R.dimen.image_height); MaxHeight= This. Image.getdrawable (). Getintrinsicheight (); } /*** This method is executed when the ListView is sliding to the head and can get the distance to the finger movement in the method *@paramDeltaY The distance the finger moves, the top end is a negative value, and the bottom end is a positive value *@paramscrolly scrollto Scrolling coordinates *@parammaxoverscrolly The maximum distance that the ListView can continue to slide after sliding to the head *@paramIstouchevent Whether the finger is dragged to the head, true: Yes, false: Indicates that the head is sliding by inertia *@return */@Overrideprotected BooleanOverscrollby (intDeltaX,intDeltaY,intSCROLLX,intscrolly,intScrollrangex,intScrollrangey,intMAXOVERSCROLLX,intmaxoverscrolly,Booleanistouchevent) { //if the finger is dragged to the head, and is the top of the head, the other ignores not handling if(Istouchevent && deltay<0){ //let the height of the imageview increase with the finger movement intNewheight = image.getheight () + math.abs (DeltaY)/3; //Limit Newheight if(newheight>maxheight) {Newheight=MaxHeight; } viewgroup.layoutparams params=Image.getlayoutparams (); Params.height=Newheight; Image.setlayoutparams (params); } return Super. Overscrollby (DeltaX, DeltaY, Scrollx, scrolly, Scrollrangex,
Scrollrangey, Maxoverscrollx, maxoverscrolly, istouchevent); } @Override Public Booleanontouchevent (motionevent ev) {if(ev.getaction () = =motionevent.action_up) { //slowly restores the imageview height to the initial 120 .Valueanimator animator =Valueanimator.ofint (Image.getheight (), originalheight); //listen to changes in animation values to implement your own animation logicAnimator.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override Public voidonanimationupdate (valueanimator animation) {intValue = (int) Animation.getanimatedvalue (); //we need to set the value to the heightViewgroup.layoutparams params =Image.getlayoutparams (); Params.height=value; Image.setlayoutparams (params); } }); Animator.setinterpolator (Newovershootinterpolator ()); Animator.setduration (500); Animator.start (); } return Super. ontouchevent (EV); }}
Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AbsListView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ImageView; Public classMainactivityextendsappcompatactivity {PrivateParallaxlistview ListView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView=(Parallaxlistview) Findviewbyid (R.id.listview); //Remove the blue shadow of the edge when the ListView slides to the headListview.setoverscrollmode (Abslistview.over_scroll_never); //Add HeaderView Header = View.inflate ( This, R.layout.header,NULL); ImageView Image=(ImageView) Header.findviewbyid (r.id.image); Listview.addheaderview (header); //Set ImageViewlistview.setparallaximage (image); Listview.setadapter (NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1, Constant.names)); }}
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/activity_main " Android:layout_width= "Match_parent" android:layout_height= "match_parent" tools:context= " Com.zh.parallax97.MainActivity "> <com.itheima.parallax97.ParallaxListView android:id=" @ +id/listview " android:layout_width=" Match_parent " android:layout_height=" Match_ Parent "/></relativelayout>
Artificial QQ Space Parallax Effect, Listview.setheader ()