android开发教程之使用线程实现视图平滑滚动示例

*近一直想做下拉刷新的效果,琢磨了好久,才走到通过onTouch方法把整个视图往下拉的步骤,接下来就是能拉下来,松开手要能滑回去啊。网上看了好久,没有找到详细的下拉刷新的例子,只有自己慢慢琢磨了。昨天和今天,研究了两天,下拉之后回滚回去的效果终于今天做出来了!开心。现在来分享下我的实现方法和一些心得体会吧。
我看了网上一个大神的例子,发现是在onTouch里面使用View的scrollTo(int, int)方法,来使整个视图往下滚动的,我尝试了使用setTranslationY()来对视图进行回滚,*次是没问题的,但多滚动几次之后,整个视图实际上已经到了非常“高”的地方了,要拉很长的距离才能看到内容。所以回滚也必须使用scrollTo(int, int)方法来操作。
但scrollTo(int, int)执行是瞬间的,方法名讲是滚动到,实际上就是“瞬移到”某个位置,因此需要一步一步的去瞬移它,让它看上去是滚过去的……

因为等下要去跑步了,还有就是也没有什么很多的要点需要讲解,我就直接上代码给大家看,注释都写好了,要点会单独提一下,更详细的讲解与心得就等着我哪天把下拉刷新实现了吧。

复制代码 代码如下:
/**
* @desc    平滑滚动
* @param    v        需要操控的视图
* @param    fromY    起始Y坐标
* @param    toY        终止Y坐标
* @param    fps        帧率
* @param    durtion    动画完成时间(毫秒)
* */
private void smoothScroll(View v, int fromY, int toY, int fps, long durtion) {
smoothScrollThread = new SmoothScrollThread(v, fromY, toY, durtion, fps);
smoothScrollThread.run();
}

/**
* @desc    平滑滚动线程,用于递归调用自己来实现某个视图的平滑滚动
* */
class SmoothScrollThread implements Runnable {
//需要操控的视图
private View v = null;
//原Y坐标
private int fromY = 0;
//目标Y坐标
private int toY = 0;
//动画执行时间(毫秒)
private long durtion = 0;
//帧率
private int fps = 60;
//间隔时间(毫秒),间隔时间 = 1000 / 帧率
private int interval = 0;
//启动时间,-1 表示尚未启动
private long startTime = -1;
/减速插值器
private DecelerateInterpolator decelerateInterpolator = null;

/**
* @desc    构造方法,做好*次配置
* */
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() {
//先判断是否是*次启动,是*次启动就记录下启动的时间戳,该值仅此一次赋值
if (startTime == -1) {
startTime = System.currentTimeMillis();
}
//得到当前这个瞬间的时间戳
long currentTime = System.currentTimeMillis();
//放大倍数,为了扩大除法计算的浮点精度
int enlargement = 1000;
//算出当前这个瞬间运行到整个动画时间的百分之多少
float rate = (currentTime – startTime) * enlargement / durtion;
//这个比率不可能在 0 – 1 之间,放大了之后即是 0 – 1000 之间
rate = Math.min(rate, 1000);
//将动画的进度通过插值器得出响应的比率,乘以起始与目标坐标得出当前这个瞬间,视图应该滚动的距离。
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);
}
}

一些要点:

1.使用线程的目的是可以递归的调用自己,在每个run()方法里只滚动一点点,这个一点点根据帧率和插值器来决定。

2.插值器实际上就是一个函数(数学里的函数),输入0-1之间的浮点数,输出0-1之间的浮点数,输出的曲线是什么样的,就看是什么插值器了,decelerate就是减速插值器了,在平面直角坐标系里面,x值均匀变化,y轴的变化越来越慢。

3.放大倍数(就是那里乘以1000)是为了提高精度,因为通过实践发现用已经过的毫秒数除以整个动画周期得出的结果是0.0 -> 0.0 -> 0.0 -> 0.0 -> 1.0 -> 1.0 -> 1.0 -> 1.0 -> 1.0 -> 2.0 -> 2.0 -> 2.0,虽然是浮点数,但精度却莫名的保持在个位数上,乘以1000后,便会出现0-1000的均匀变化,这个时候去除以1000,便可得到0.000 – 1.000之间的均匀变化的数。

4.还有个很奇葩的是MotionEvent.getY()的值和scrollTo(int,int)的值貌似不是在同一个坐标系里面的。这个还有待进一步的分析和研究啊。

 

android开发教程之使用线程实现视图平滑滚动示例 改

复制代码
package com.melonsapp.messenger.ui.popupuser;

import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;

import java.util.Timer;

/**
 * Created by lidaqiang on 17/5/3.
 */

public class SmoothScroll {
    private Handler mHandler = new Handler();
    SmoothScrollThread smoothScrollThread;
    public static int noData = 0;

    /**
     * @param v       需要操控的视图
     * @param fromX   起始Y坐标
     * @param toX     终止Y坐标
     * @param fps     帧率
     * @param durtion 动画完成时间(毫秒)
     * @desc 平滑滚动
     */
    public SmoothScroll(View v, WindowManager windowManager, WindowManager.LayoutParams windowParams, int fromX, int toX, int fps, long durtion) {
        this(v, windowManager, windowParams, fromX, toX, noData, noData, 60, durtion);
    }

    public SmoothScroll(View v, WindowManager windowManager, WindowManager.LayoutParams windowParams, int fromX, int toX, int fromY, int toY, long durtion) {
        this(v, windowManager, windowParams, fromX, toX, fromY, toY, 60, durtion);
    }

    public SmoothScroll(View v, WindowManager windowManager, WindowManager.LayoutParams windowParams, int fromX, int toX, int fromY, int toY, int fps, long durtion) {
        smoothScrollThread = new SmoothScrollThread(v, windowManager, windowParams, fromX, toX, fromY, toY, durtion, fps);
    }

    public void start() {
        if (smoothScrollThread == null) {
            return;
        }
        smoothScrollThread.run();
    }

    public void stop() {
        if (smoothScrollThread == null) {
            return;
        }
        smoothScrollThread.stop();
    }

    /**
     * @desc 平滑滚动线程,用于递归调用自己来实现某个视图的平滑滚动
     */
    class SmoothScrollThread implements Runnable {
        WindowManager mWindowManager;
        WindowManager.LayoutParams mWindowParams;
        //需要操控的视图
        private View v = null;
        //原X坐标
        private int fromX = noData;
        //目标X坐标
        private int toX = noData;

        //原Y坐标
        private int fromY = noData;
        //目标Y坐标
        private int toY = noData;
        //动画执行时间(毫秒)
        private long durtion = 0;
        //帧率
        private int fps = 60;
        //间隔时间(毫秒),间隔时间 = 1000 / 帧率
        private int interval = 0;
        //启动时间,-1 表示尚未启动
        private long startTime = -1;
        //        /减速插值器
        private DecelerateInterpolator decelerateInterpolator = null;

        private int mChangeState = 0;  // 0 x,y都不变   1 x变      2 y变   3 x,y都变

        /**
         * @desc 构造方法,做好*次配置
         */
        public SmoothScrollThread(View v, WindowManager windowManager, WindowManager.LayoutParams windowParams, int fromX, int toX, int fromY, int toY, long durtion, int fps) {
            mWindowManager = windowManager;
            mWindowParams = windowParams;
            this.v = v;
            this.fromX = fromX;
            this.toX = toX;
            this.fromY = fromY;
            this.toY = toY;
            this.durtion = durtion;
            this.fps = fps;
            this.interval = 1000 / this.fps;
            decelerateInterpolator = new DecelerateInterpolator();
            mChangeState = 0;

            if (fromX != toX && fromY == toY) {
                mChangeState = 1;
            } else if (fromX == toX && fromY != toY) {
                mChangeState = 2;
            } else if (fromX != toX && fromY != toY) {
                mChangeState = 3;
            }
        }

        @Override
        public void run() {

            if (mChangeState == 0) {
                return;
            }

            //先判断是否是*次启动,是*次启动就记录下启动的时间戳,该值仅此一次赋值
            if (startTime == -1) {
                startTime = System.currentTimeMillis();
            }
            //得到当前这个瞬间的时间戳
            long currentTime = System.currentTimeMillis();
            //放大倍数,为了扩大除法计算的浮点精度
            int enlargement = 1000;
            //算出当前这个瞬间运行到整个动画时间的百分之多少
            float rate = (currentTime - startTime) * enlargement / durtion;
            //这个比率不可能在 0 - 1 之间,放大了之后即是 0 - 1000 之间
            rate = Math.min(rate, 1000);
            //将动画的进度通过插值器得出响应的比率,乘以起始与目标坐标得出当前这个瞬间,视图应该滚动的距离。

            int currentX = fromX;
            if (mChangeState == 1 || mChangeState == 3) {
                int changeDistanceX = (int) ((fromX - toX) * decelerateInterpolator.getInterpolation(rate / enlargement));
                currentX = fromX - changeDistanceX;
            }

            int currentY = fromY;
            if (mChangeState == 2 || mChangeState == 3) {
                int changeDistanceY = (int) ((fromY - toY) * decelerateInterpolator.getInterpolation(rate / enlargement));
                currentY = fromY - changeDistanceY;
            }


            notifyViewLayout(currentX, currentY);

            if (currentX != toX || currentY != toY) {


                mHandler.postDelayed(this, this.interval);
            } else {
                return;
            }
        }


        private void notifyViewLayout(int currentX, int currentY) {
//            v.scrollTo(0, currentY);
            if (mWindowParams == null || mWindowParams == null || v == null) {
                return;
            }

            if (mChangeState == 1 || mChangeState == 3) {
                mWindowParams.x = currentX;
            }


            if (mChangeState == 2 || mChangeState == 3) {
                mWindowParams.y = currentY;
            }


            if (v.getParent() != null) {
                mWindowManager.updateViewLayout(v, mWindowParams);
            }

        }

        public void stop() {
            mHandler.removeCallbacks(this);
        }
    }


}
复制代码