Example of using thread to implement view smoothing scrolling in Android development tutorial

Source: Internet
Author: User

Recently always want to do the effect of a pull-up refresh, pondering a long time, only to go through the Ontouch method to pull the entire view down the steps, the next is to pull down, loosen the hand to be able to slide back ah. On the internet for a long time, did not find detailed example of the drop-down refresh, only their own slowly pondering. Yesterday and today, the study of two days, pull back after the return of the effect is finally done today! Happy. Now let's share my implementation and some experience.
I looked at an example of a great God on the Web and found that using the view's Scrollto (int, int) method in Ontouch to scroll the entire view down, I tried to roll back the views using Settranslationy (), and the first time was no problem. But after a few more scrolling, the entire view is actually very "high", pulling a long distance to see the content. So rollback must also be done using the Scrollto (int, int) method.
But scrollto (int, int) execution is instantaneous, the method name is scrolling to, in fact, "teleport to" a position, so it takes a step-by-step to teleport it, so it looks like it's rolling past ...

Because wait to go to run, there is also no a lot of points need to explain, I directly on the code for everyone to see, comments are written, the main points will be singled out, more detailed explanation and experience will be waiting for me to the next day to refresh the implementation of it.

Copy CodeThe code is as follows:
/**
* @desc Smooth Scrolling
* @param v need to manipulate the view
* @param fromY start y-coordinate
* @param toY terminating y-coordinate
* @param fps frame rate
* @param durtion Animation completion time (MS)
* */
private void Smoothscroll (View v, int fromY, int toY, int fps, long durtion) {
Smoothscrollthread = new Smoothscrollthread (V, FromY, ToY, durtion, fps);
Smoothscrollthread.run ();
}

/**
* @desc Smooth scrolling thread, used to recursively call itself to implement a smooth scrolling of a view
* */
Class Smoothscrollthread implements Runnable {
Views that need to be manipulated
Private View v = null;
Original y-coordinate
private int fromY = 0;
Target Y coordinate
private int toY = 0;
Animation execution Time (MS)
Private long durtion = 0;
Frame rate
private int fps = 60;
Interval time (milliseconds), interval = 1000/frame rate
private int interval = 0;
Start time, 1 indicates not yet started
Private Long startTime =-1;
/Deceleration Interpolator
Private Decelerateinterpolator decelerateinterpolator = null;

/**
* @desc construction method, do the first time configuration
* */
Public Smoothscrollthread (View v, int fromY, int toY, long durtion, int fps) {
THIS.V = v;
this.fromy = FromY;
This.toy = ToY;
This.durtion = durtion;
This.fps = fps;
This.interval = 1000/this.fps;
Decelerateinterpolator = new Decelerateinterpolator ();
}
@Override
public void Run () {
First to determine whether it is the first start, is the first start to record the start of the timestamp, this value is only assigned this time
if (StartTime = =-1) {
StartTime = System.currenttimemillis ();
}
Get the time stamp of the current moment
Long currenttime = System.currenttimemillis ();
magnification, in order to enlarge the floating point precision of the Division calculation
int enlargement = 1000;
Figure out how much of the current moment runs to the entire animation time
Float rate = (currenttime-starttime) * enlargement/durtion;
This ratio is unlikely to be between 0-1 and 0-1000 after amplification.
Rate = Math.min (rate, 1000);
The rate at which the animation progresses through the interpolator, multiplied by the starting and target coordinates to derive the current moment, the distance the view should scroll.
int changedistance = (int) ((fromy-toy) * Decelerateinterpolator.getinterpolation (rate/enlargement));
int currenty = fromy-changedistance;
V.scrollto (0, CurrentY);
if (currenty! = ToY) {
Postdelayed (this, this.interval);
} else {
Return
}
}
public void Stop () {
Removecallbacks (this);
}
}

Some points:

1. The purpose of using a thread is to recursively call itself, in each run () method only scroll a little bit, this a little bit according to the frame rate and the interpolator to decide.

2. The interpolator is actually a function (mathematical function), enter the floating point between 0-1, the output of the floating number between 0-1, the output of the curve is what, see what is the Interpolator, decelerate is the deceleration interpolator, in the plane Cartesian coordinate system, the X-value uniform change, The y-axis changes more and more slowly.

3. Magnification (that is, multiplied by 1000) is to improve accuracy, as the result of a practice finding that the elapsed number of milliseconds divided by the entire animation cycle results in a total of 1.0 1.0, 0.0, 1.0, 0.0, 0.0, 0.0 Gt 1.0, 1.0, 2.0, 2.0, 2.0, although it is floating point number, but the precision is inexplicable in the single digit, multiplied by 1000, there will be 0-1000 of the uniform change, this time to divide by 1000, you can get 0.000- The number of evenly varying between 1.000.

4. It is also very rare that the value of motionevent.gety () and the value of Scrollto (Int,int) do not appear to be in the same coordinate system. This has yet to be further analyzed and researched.

Example of using thread to implement view smoothing scrolling in Android development tutorial

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.