Android uses handler to implement view elastic sliding _android

Source: Internet
Author: User

The principle of elastic sliding

A large slide is not a number of minor slides and is completed within a time period. A better user experience

There are many ways to implement it, including using scroller, animation, and latency strategies.

Using handler to achieve elastic sliding

The effect can see the button buttons sliding. Note that this changes the contents of the view.

You can try to remove the outer relitivelayout of the button and place the ID under the button. Found the text slide of the 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 FRAM
E_out = 30;
private static final int delayed_time = 30;
Private Relativelayout button;
private int mcount; Private Handler Handler = new Handler () {public void Handlemessage(msg) {switch (msg.what) {case message_scroll_to:mcount++; if (mcount <= frame_out) {Float fraction = mcount/(float) FRA
Me_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); }
}

The above is a small set to introduce the Android use handler to achieve view elastic sliding, I hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.