Control _android for the realization of the jelly sliding effect by Android

Source: Internet
Author: User
Tags gety

Objective

In the micro-letter is the processing method is to allow users to slide, but eventually rolled back to the original place, the effect is very vivid (after all, success depends on the details). So what are we going to do with Android? Here is a brief introduction to Jellyscrollview, a jelly sliding control that I inherited from scrollview with a damp effect.

The following words do not say more, first to see the effect of the picture

(in the virtual machine or the real machine running is very fluent, may be recorded video into GIF when a little cotton.) )

Implementation principle

In fact, you just need to rewrite the logic of its interception method, ScrollView blocking method Onintercepttouchevent generally returns false by default, indicating that the event is not blocked. All we need to do is to intercept and move the layout when the user slides the interface, and restore the layout when the user releases the hand. Very simple

The first step: integration ScrollView rewrite several construction methods;

Step two: Rewrite the Onfinishinflate method to get the first child view;

Step three: In the interception method to deal with the above mentioned logic;

public class Jellyscrollview extends ScrollView {Private View inner;//view private float y;//Click Y-coordinate private
 
 Rect normal = new Rect ();//Rectangle (This is just a form, just to determine whether an animation is needed.)
 
 Private Boolean iscount = false;//whether to start computing private Boolean ismoving = false;//whether to start moving.
 
 private int top;//Drag from time to time height.
 private int mtouchslop;//system minimum Sliding distance public jellyscrollview (context) {super (context);
 Public Jellyscrollview (context, AttributeSet attrs) {Super (context, attrs); Jellyscrollview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr)
  ;
 Mtouchslop = Viewconfiguration.get (context). Getscaledtouchslop (); /*** * Work is done based on the XML build view. The function is called at the end of the build view, after all child views have been added.
 Even if the subclass overrides the Onfinishinflate * method, the method of the parent class should be called so that the method can be executed.
  * * @Override protected void onfinishinflate () {if (Getchildcount () > 0) {inner = getchildat (0); }/** Intercept Event/@Override public boolean onintercepttouchevent (motionevent ev) {if inneR!= null) {int action = ev.getaction ();
     Switch (action) {Case motionevent.action_down:y = ev.gety ();
     top = 0;
 
    Break
     Case MOTIONEVENT.ACTION_UP://Fingers loose.
     Ismoving = false;
     if (Isneedanimation ()) {animation ();
    } break; /*** * Excludes the first move calculation because it is not possible to get the y-coordinate in motionevent.action_down for the first time, * because this is the ScrollView touch event passed to the ListView's child item. So from paragraph
    Two times the calculation begins. * However, we also have to initialize, that is, the first time we move the sliding distance to 0.
    After the record is accurate, the normal execution. * * Case MotionEvent.ACTION_MOVE:final float prey = y;//y-coordinate when pressed Nowy = Ev.gety (); y-coordinate int d per hour
     Eltay = (int) (Nowy-prey),//sliding distance if (!iscount) {deltay = 0;//here is 0.
 
     } if (Math.Abs (DeltaY) < mtouchslop && Top <= 0) return true;
 
     When scrolling to the top or bottom will not scroll, then move the layout isneedmove (); if (ismoving) {//Initialize header rectangle if (Normal.isempty ()) {//Save normal layout position Normal.set (Inner.getleft (), Inne R.gettop (), Inner.getright (), Inner.getboTtom ()); }//Mobile layout inner.layout (Inner.getleft (), inner.gettop () + DELTAY/3, Inner.getright (), Inner.getbottom () + D
 
      ELTAY/3);
     Top + = (DELTAY/6);
     } Iscount = true;
     y = Nowy;
   Break
 } return super.onintercepttouchevent (EV); /*** * Back-shrink animation/public void animation () {//Open mobile animation translateanimation ta = new translateanimation (0, 0, Inne
  R.gettop (), normal.top);
  Ta.setduration (200);
 
  Inner.startanimation (TA);
  Set back to normal layout position inner.layout (Normal.left, Normal.top, Normal.right, Normal.bottom);
 
  Normal.setempty ();
  Loosen your fingers to 0.
  Iscount = false;
 y = 0;
 //Whether you need to turn on animation public boolean isneedanimation () {return!normal.isempty (); /*** * Need to move layout * Inner.getmeasuredheight (): Gets the total height of the control * getheight (): Gets the height of the screen * * @return/public void Isne
  Edmove () {int offset = inner.getmeasuredheight ()-getheight ();
  int scrolly = getscrolly (); scrolly = 0 is the top//scrolly = = Offset is the bottom if (scrolly= = 0 | |
 scrolly = = offset) {ismoving = true;} }
}

And then use our jellyscrollview in the outermost layer of the layout.

(For the sake of presentation, I just probably wrote part of the layout code)

<?xml version= "1.0" encoding= "Utf-8" ><cn.ichengxi.fang.view.jellyscrollview xmlns:android= "http://" Schemas.android.com/apk/res/android "
 android:layout_width=" match_parent "
 android:layout_height=" Match_ Parent "
 android:background=" @color/bg "
 android:scrollbars=" None ">
 
 <linearlayout  android: Layout_width= "Match_parent"
  android:layout_height= "wrap_content"
  android:orientation= "vertical" >
  <textview   android:id= "@+id/personal_setting_txt"
   android:layout_width= "Match_parent"
   android:layout_height= "Wrap_content"
   android:alpha= "0.8"
   android:gravity= "center_vertical
   " android:text= "Setting"
   android:textcolor= "@android: Color/black"/>
 
 </linearlayout></ Cn.ichengxi.fang.view.jellyscrollview> 12345678910111213141516171819202122231234567891011121314151617181920212223

Summarize

OK, so you can be very elegant to achieve the effect of the jelly control. The above is the entire content of this article, I hope this article content for everyone can help, if there are questions you can message exchange.

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.