Android development tutorial using thread to implement view smooth scrolling sample _android

Source: Internet
Author: User
Tags current time

Recently have been trying to do the effect of the Drop-down refresh, pondering for a long time, only to go through the Ontouch method to pull down the entire view of the steps, the next is able to pull down, loosen the hand to be able to slip back ah. Read on the internet for a long time, did not find a detailed drop-down to refresh the example, only their own slowly pondering. Yesterday and today, studied for two days, after pulling back after the effect of the return to do today! Happy. Now to share my implementation and some experience.
I looked at an example of a great God on the Internet and found that it was in Ontouch to use view's Scrollto (int, int) method to scroll the entire view down, and I tried to roll back with Settranslationy (), the first time it was OK, But after a few more scrolling, the entire view is actually very "high" and it takes a long distance to see the content. Therefore, the rollback must also be operated using the Scrollto (int, int) method.
But scrollto (int, int) execution is instantaneous, the method name is to scroll to, in fact is "instantaneous move to" a position, so need to move it step by step, let it appear to be rolled over ...

Because I'm going to run for a while, there is also no a lot of points need to explain, I will directly on the code to everyone to see, notes are written, the main points will be a separate mention, more detailed explanation and experience will be waiting for me the day of the drop down to realize it.

Copy Code code as follows:

/**
* @desc Smooth Scrolling
* @param v need to manipulate the view
* @param fromY start y-coordinate
* @param toY termination y-coordinate
* @param fps frame rate
* @param durtion Animation finish 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 for recursively calling itself to achieve 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 that it has not been started
Private Long starttime =-1;
/deceleration interpolation device
Private Decelerateinterpolator decelerateinterpolator = null;

/**
* @desc The construction method, completes 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 startup, is the first time to record the start of the timestamp, this value is only assigned once
if (StartTime = = 1) {
StartTime = System.currenttimemillis ();
}
Get the current time stamp of this moment
Long currenttime = System.currenttimemillis ();
magnification, in order to enlarge the floating-point precision of 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 ratio of the animation's progress through the interpolation to the response, multiplied by the start and the target coordinates to arrive at the current moment, the view should scroll the distance.
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 key points:

1. The purpose of using threads is to call yourself recursively, rolling a little in each run () method, which is determined by the frame rate and the interpolation.

2. The interpolation is actually a function (function in mathematics), input 0-1 floating point number, output between 0-1 floating-point number, the output curve is what kind of, see what is the interpolation, decelerate is the deceleration interpolation, in the plane rectangular coordinate system, the X value evenly change, The y-axis changes more and more slowly.

3. The magnification (where multiplied by 1000) is to improve accuracy, because the result of the practice finding is that the elapsed millisecond divided by the entire animation cycle is 0.0-> 0.0-> 0.0-> 0.0-> 1.0-> 1.0-> 1.0-& Gt 1.0-> 1.0-> 2.0-> 2.0 2.0, although the floating-point number, but the precision is inexplicably kept in single-digit, multiplied by 1000, there will be 0-1000 uniform changes, this time to divide by 1000, you can get 0.000- The number of uniform variations between 1.000.

4. There's a wonderful one. The value of Motionevent.gety () and the value of Scrollto (Int,int) do not seem to be in the same coordinate system. This has yet to be further analysis and research AH.

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.