實現View彈性滑動例子,view彈性滑動

來源:互聯網
上載者:User

實現View彈性滑動例子,view彈性滑動

彈性滑動原理

將一次大的滑動非為若干次小的滑動,並在一個時間段內完成。更好的使用者體驗

實現方式很多種,包括用Scroller,動畫,延時策略.

 

使用Handler實現彈性滑動

效果可以看到按鈕Button向滑動。注意這裡是將View的內容改變。

你可以試一試將Button外層的RelitiveLayout去掉,把id放在Button下。發現是Button的文字滑動

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <RelativeLayout        android:id="@+id/button1"        android:layout_height="wrap_content"        android:layout_width="300dp"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true">        <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"              android:layout_alignParentRight="true"           android:layout_alignParentTop="true"                android:text="Button" />    </RelativeLayout>    </RelativeLayout>
import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.widget.RelativeLayout;public class MainActivity extends Activity {    private static final int MESSAGE_SCROLL_TO = 1;    private static final int FRAME_OUT = 30;    private static final int DELAYED_TIME = 30;    private RelativeLayout button;    private int mcount;    private Handler handler = new Handler(){        public void handleMessage(Message msg){            switch (msg.what) {            case MESSAGE_SCROLL_TO:                mcount++;                if (mcount <= FRAME_OUT) {                    float fraction = mcount / (float)FRAME_OUT;                    int scrollx =(int) (fraction * 100);                    button.scrollTo(scrollx, 0);                    handler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO, DELAYED_TIME);                }                break;            default:                break;            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (RelativeLayout) findViewById(R.id.button1);        handler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO, DELAYED_TIME);            }}

 參考:《Android開發藝術探索》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.