Android: sliding effect on the home page of Baidu mobile Assistant

Source: Internet
Author: User
Tags gety

Android: sliding effect on the home page of Baidu mobile Assistant

Today, we can see that the sliding effect on the Baidu mobile assistant homepage is very nice. The main functions are as follows:

1. When a finger is drawn, the top search bar scales out to hide as the finger moves. After hiding, the content can still be moved.
2. When the finger slides down, when the display content reaches the first one, the top search bar gradually increases.

Knowledge Used by yourself:

1. android event transfer mechanism: After the finger movement event is captured, the size of the function bar is modified based on the moving direction and the height of the function bar. Because the height of the listview and feature columns must be moved at the same time, the dispatchTouchEvent method must be rewritten to directly call this. onTouchEvent (ev); to capture and analyze all events.
2. Custom viewgroup: measure the view height

Additional features:

The feature Bar Height is automatically hidden when it is <= 1/2
When the function Bar Height is greater than 1/2, it is automatically changed to the maximum value.
Code implementation in MotionEvent. ACTION_UP

The effect is displayed. The video tool is too choppy. Let's take a look at it:
Vc/fingerprint + fingerprint/J0tTX1Ly61Nm0zrfi17CjrLvy1d/fingerprint + DQoJPHA + fingerprint + 687Hku6 + fingerprint + bWrrHku6 + fingerprint = "brush: java;">Package com. example. materialtest. widget; import android. animation. objectAnimator; import android. animation. valueAnimator; import android. animation. valueAnimator. animatorUpdateListener; import android. content. context; import android. graphics. pointF; import android. graphics. rectF; import android. support. v4.view. viewCompat; import android. util. attributeSet; import android. util. log; import android. view. motionEve Nt; import android. view. view; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. linearLayout; import com. example. materialtest. r;/*** sliding hidden control ***/public class ScrollHideLayout extends LinearLayout implements OnScrollListener {private static final String TAG = ScrollHideLayout. class. getSimpleName (); private int scrollViewId = R. id. scrollView; Private int changeViewId = R. id. changeView; private int changeViewMaxHeight; private PointF touchPoint = new PointF (); private View changeView; private AbsListView scrollView; private RectF scrollViewRect = new RectF (); public ScrollHideLayout (Context context Context, attributeSet attrs) {this (context, attrs, 0);} public ScrollHideLayout (Context context) {this (context, null);} public ScrollHideLayout (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr) ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); int len = getChildCount (); if (null = changeView | null = scrollView) {for (int I = 0; I <len; I ++) {View child = getChildAt (I); // if (child. getId () = scro LlViewId & child instanceof AbsListView) {scrollView = (AbsListView) child; setScrollViewRect ();} if (child. getId () = changeViewId) {changeView = child; changeView. setMinimumHeight (0); changeViewMaxHeight = changeView. getMeasuredHeight () ;}} else {// recalculate the position of the scroll control setScrollViewRect ();} Log. I (TAG, find scrollview and changeView: + scrollViewId +, + changeViewId); Log. I (TAG, scrollview rec T: + changeView. getLayoutParams (). getClass (). getCanonicalName (); if (null = changeView | null = scrollView) {throw new IllegalArgumentException (cocould not foud changeView or scrollView);} private void setScrollViewRect () {// obtain the range of the scroll control float left = ViewCompat. getX (scrollView); float top = ViewCompat. getY (scrollView); float right = left + scrollView. getMeasuredWidth (); float bottom = top + s CrollView. getMeasuredHeight (); scrollViewRect. left = left; scrollViewRect. top = top; scrollViewRect. right = right; scrollViewRect. bottom = bottom ;}@ Override public boolean onTouchEvent (MotionEvent ev) {if (! IsScrollViewTouch (ev) {return false;} final android. view. viewGroup. layoutParams params = changeView. getLayoutParams (); switch (ev. getAction () {case MotionEvent. ACTION_DOWN: touchPoint. x = ev. getX (); touchPoint. y = ev. getY (); break; case MotionEvent. ACTION_MOVE: int height = params. height; // float distance = ev. getY ()-touchPoint. y; // maximum height. You cannot drag down if (height> = changeViewMaxHeight && Distance> 0) {touchPoint. y = ev. getY (); break;} // if (height <= 0 & distance <0) {touchPoint. y = ev. getY (); break;} // you can drag if (distance> 0 & scrollView down to the top of the listview. getFirstVisiblePosition ()! = 0) {touchPoint. y = ev. getY (); break;} height = Math. round (height + distance); if (height> changeViewMaxHeight) {height = changeViewMaxHeight;} if (height <= 0 & distance <0) {height = 0; // TODO onhide} params. height = height; changeView. requestLayout (); touchPoint. x = ev. getX (); touchPoint. y = ev. getY (); break; case MotionEvent. ACTION_CANCEL: case MotionEvent. ACTION_UP: // if the height exceeds half, the I will be automatically hidden. Nt [] values = null; // slide up, less than half of the remaining position if (params. height <= changeViewMaxHeight/2) {values = new int [] {params. height, 0 };} else {values = new int [] {params. height, changeViewMaxHeight};} if (null! = Values) {ValueAnimator anim = ObjectAnimator. ofInt (changeView, translationY, values); anim. addUpdateListener (new AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {int value = (int) animation. getAnimatedValue (); params. height = value; changeView. requestLayout () ;}}); anim. setDuration (1, 250); anim. setTarget (changeView); anim. start ();} break;} return true;} @ Override public boolean dispatchTouchEvent (MotionEvent ev) {// directly intercept the event this. onTouchEvent (ev); return super. dispatchTouchEvent (ev);} private boolean isScrollViewTouch (MotionEvent ev) {float x = ev. getX (); float y = ev. getY (); return (x> = scrollViewRect. left & x <= scrollViewRect. right) & (y> = scrollViewRect. top & y <= scrollViewRect. bottom);}/*** @ return Whether it is possible for the child view of this layout to * scroll up. override this if the child view is a custom view. */public boolean canChildScrollUp () {if (android. OS. build. VERSION. SDK_INT <14) {if (scrollView instanceof AbsListView) {final AbsListView absListView = (AbsListView) scrollView; return absListView. getChildCount ()> 0 & (absListView. getFirstVisiblePosition ()> 0 | absListView. getChildAt (0 ). getTop () <absListView. getPaddingTop ();} else {return ViewCompat. canScrollVertically (scrollView,-1) | scrollView. getScrollY ()> 0 ;}} else {return ViewCompat. canScrollVertically (scrollView,-1) ;}@ Override public void onScrollStateChanged (AbsListView view, int scrollState) {// TODO Auto-generated method stub} @ Override public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {// TODO Auto-generated method stub }}

 

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.