In Android Development, iOS apps and Android apps are often required to be consistent, so many controls need to be defined by the developer when it comes to Android, so let's share an elastic sliding scrollview that imitates apples.
Bouncescrollview Source:
PackageCom.joke.widget;ImportAndroid.content.Context;ImportAndroid.graphics.Rect;ImportAndroid.os.Build;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.view.View;Importandroid.view.animation.TranslateAnimation;ImportAndroid.widget.ScrollView;/*** Implementation of ScrollView rebound effect*/ Public classBouncescrollviewextendsScrollView {PrivateView inner;//Kids View Private floatY//y-coordinate when clicked PrivateRect normal =NewRect ();//the rectangle (here is just a form, just used to determine if an animation is needed.) Private BooleanIscount =false;//whether to start the calculation PublicBouncescrollview (Context context, AttributeSet attrs) {Super(context, attrs); //cancels the yellow or blue shading of the edge when sliding to the top or bottom if(Build.VERSION.SDK_INT >= 9) { This. Setoverscrollmode (View.over_scroll_never); } } /*** * Based on XML generation view work done. The function is called at the end of the build view, after all the child views have been added. Even if the subclass overrides the Onfinishinflate * method, the method of the parent class should be called to enable the method to execute. */@Overrideprotected voidonfinishinflate () {if(Getchildcount () > 0) {Inner= Getchildat (0); } } /*** * Monitor Touch*/@Override Public Booleanontouchevent (motionevent ev) {if(Inner! =NULL) {commontouchevent (EV); } return Super. ontouchevent (EV); } /*** * Touch Event * *@paramEV*/ Public voidcommontouchevent (motionevent ev) {intAction =ev.getaction (); Switch(action) { CaseMotionevent.action_down: Break; Casemotionevent.action_up://loosen your fingers. if(Isneedanimation ()) {animation (); Iscount=false; } Break; /*** excludes the first move calculation, because the y-coordinate is not known for the first time and is not available in Motionevent.action_down, * because this is the Myscrollview touch event passed to the child in the ListView Child item above. So start with the second calculation. * However, we also want to initialize, that is, the first time to move the sliding distance to 0. Then the records are accurate and executed normally. */ CaseMotionevent.action_move:Final floatPreY = y;//y-coordinate when pressed floatNowy = Ev.gety ();//always y-coordinate intDeltaY = (int) (Prey-nowy);//Sliding distance if(!Iscount) {DeltaY= 0;//It's going to be 0 here.} y=Nowy; //when scrolling to the top or bottom, no more scrolling, then move the layout if(Isneedmove ()) {//Initialize the head rectangle if(Normal.isempty ()) {//save a normal layout locationNormal.set (Inner.getleft (), Inner.gettop (), Inner.getright (), Inner.getbottom ()); } //log.e ("JJ", "Rectangle:" + inner.getleft () + "," + inner.gettop ()//+ "," + inner.getright () + "," + Inner.getbottom ()); //Mobile LayoutsInner.layout (Inner.getleft (), Inner.gettop ()-DELTAY/2, Inner.getright (), Inner.getbottom ()-DELTAY/2); } Iscount=true; Break; default: Break; } } /*** * Retraction animation*/ Public voidAnimation () {//turn on mobile animationTranslateanimation TA =NewTranslateanimation (0, 0, Inner.gettop (), normal.top); Ta.setduration (200); Inner.startanimation (TA); //set back to normal layout positioninner.layout (Normal.left, Normal.top, Normal.right, Normal.bottom); //log.e ("JJ", "return:" + Normal.left + "," + Normal.top + "," +//Normal.right//+ "," + normal.bottom);Normal.setempty (); } //do you want to turn on animation Public Booleanisneedanimation () {return!Normal.isempty (); } /*** * If you need to move the layout inner.getmeasuredheight (): Gets the total height of the control * * getheight (): Gets the height of the screen * *@return */ Public BooleanIsneedmove () {intoffset = inner.getmeasuredheight ()-getheight (); intscrolly =getscrolly (); //log.e ("JJ", "scrolly=" + scrolly); //0 is the top, the back one is the bottom. if(scrolly = = 0 | | scrolly = =offset) { return true; } return false; }}
Instructions for use:
1, put the Bouncescrollview directly into the *.widget bag
2. Use Bouncescrollview to wrap other layouts directly in the layout file
<Com.joke.widget.BounceScrollViewxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:fadescrollbars= "false"Android:fadingedge= "None"Android:fadingedgelength= "0dip"Tools:context=". Mainactivity " > <!--android:background= "@drawable/coversation_bg" - <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#FF0000"android:orientation= "vertical" > ... </LinearLayout></Com.joke.widget.BounceView>
It is noteworthy that: Bouncescrollview is directly inherited from the ScrollView, then Bouncescrollview also need to follow the scrollview of the Convention, it only allows a child view inside, So other layouts need to be wrapped using a viewgroup.
In addition: can also give Bouncescrollview background, use: android:background= "@drawable/coversation_bg"
For example: Coversation_bg.xml can also achieve the image overlay effect
<?XML version= "1.0" encoding= "Utf-8"?><layer-listxmlns:android= "Http://schemas.android.com/apk/res/android" > <Item> <Bitmapandroid:src= "@drawable/cover_default_bg"Android:tilemode= "Repeat" /> </Item> <ItemAndroid:top= "10.0dip"> <Bitmapandroid:gravity= "Top|center"android:src= "@drawable/conversation_bg_logo" /> </Item></layer-list>
Imitation apple Elastic layout