使用延时策略实现弹性滑动

来源:互联网 发布:数控铣好看编程图案 编辑:程序博客网 时间:2024/05/17 03:22

使用延时策略实现弹性滑动

延时策略。它的核心思想是通过发送一系列延时消息从而打到一种渐进式的效果

  • 具体来说可以使用Handler或View的postDelayed方法,也可以使用现成的sleep方法。
    • 对于postDelayed方法来说,我们可以通过它来发送一个消息,然后在消息中来进行View的滑动,如果接连不断地发送这种延时消息,那么就可以实现弹性滑动的效果。
    • 对于sleep方法来说,通过在while循环中不断的滑动View 和 sleep,就可以实现弹性滑动的效果。

采用Handler的一个实例。功能:大约1000ms内将View的内容向左移动100px.

private static final int MESSAGE_SCROLL_TO = 1;private static final int FRAME_COUNT  = 30;private static final int DELAYED_TIME = 33;private int mCount = 0;@SuppressLint("HandlerLeak")private Handler mHandler = new Handler(){    public void handleMessage(Message msg){        switch(msg.what){            case MESSAGE_SCROLL_TO:                mCount++;                if(mCount <= FRAME_COUNT){                    float fraction = mCount /(float)FRAME_COUNT;                    int scrollX = (int)(fraction * 100) ;                    mButton1.scrollTo(scrollX,0);                    mHandler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO,                 DELAYED_TIME);                }            break;        }    }}

—————————《android 开发艺术探索》 作者 : 任玉刚

0 0
原创粉丝点击