彈性滑動原理
將一次大的滑動非為若干次小的滑動,并在一個時間段內(nèi)完成。更好的用戶體驗
實現(xiàn)方式很多種,包括用Scroller,動畫,延時策略.
使用Handler實現(xiàn)彈性滑動
效果可以看到按鈕Button向滑動。注意這里是將View的內(nèi)容改變。
你可以試一試將Button外層的RelitiveLayout去掉,把id放在Button下。發(fā)現(xiàn)是Button的文字滑動
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><RelativeLayoutandroid:id="@+id/button1"android:layout_height="wrap_content"android:layout_width="300dp"android:layout_alignParentRight="true"android:layout_alignParentTop="true"><Buttonandroid: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;}}};@Overrideprotected 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使用Handler實現(xiàn)View彈性滑動,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答