Android uses scroller for slow movement

Source: Internet
Author: User

In the launcher in the workspace to achieve the left and right screen switching effect, the inside of the use of the scroller record sliding trajectory, to achieve a slow movement to the left or right, here I summarize this effect:

Let's take a look at one example: the Red warp block moves slowly from left to right when the button is clicked, how does this happen?

Let's take a look, Scroller, there's a Startscroll method in this object.

void Android.widget.Scroller.startScroll (int startX, int starty, int dx, int dy, int duration)
The first parameter is the x-coordinate value of the start move, the second is the y-coordinate value of the start move, and the third fourth parameter is the coordinate value to move to a point, and duration is of course the time to move. What's the use of this? You know, there's a way to do it.

Boolean Android.widget.Scroller.computeScrollOffset ()

TheComputescrolloffset method always returns False when Startscroll executes during duration time, but returns true when the animation finishes executing.

With these two methods is not enough, we also need to rewrite viewgroup a method,

Computescroll when will this method be called?

This is what the official web says.

Public void computescroll () SINCE:API Level 1

Called by a, parent to request, a child, update its values for MSCROLLX and mscrolly if necessary. This would typically be-done if the animating a scroll using a Scroller object.

When we execute Ontouch or invalidate () or postinvalidate () will result in the execution of this method

So we call like the following,postinvalidate execution, will go to tune the Computescroll method, and this method to tune postinvalidate, so You can constantly call the Scrollto method until the end of the Mscroller animation, and of course the first time, we need to manually call the postinvalidate to call

Computescroll Method [Java]View Plaincopy
    1. @Override
    2. Public void Computescroll () {
    3. if (Mscroller.computescrolloffset ()) {
    4. ScrollTo (Mscroller.getcurrx (), 0);
    5. Postinvalidate ();
    6. }
    7. }


The source code for the example above is attached below

First, Myviewgroup.java.

[Java]View Plaincopy
  1. Package COM.WB;
  2. Import Android.content.Context;
  3. Import Android.util.AttributeSet;
  4. Import Android.view.View;
  5. Import Android.view.ViewGroup;
  6. Import Android.widget.LinearLayout;
  7. Import Android.widget.Scroller;
  8. Public class Myviewgroup extends LinearLayout {
  9. Private Boolean s1=true;
  10. Scroller mscroller=null;
  11. Public Myviewgroup (context context, AttributeSet attrs) {
  12. Super (context, attrs);
  13. mscroller=New Scroller (context);
  14. //TODO auto-generated constructor stub
  15. }
  16. @Override
  17. public void Computescroll () {
  18. if (Mscroller.computescrolloffset ()) {
  19. ScrollTo (Mscroller.getcurrx (), 0);
  20. Postinvalidate ();
  21. }
  22. }
  23. public void Beginscroll () {
  24. if (!s1) {
  25. Mscroller.startscroll (0, 0, 0, 0, 1000);
  26. S1 = true;
  27. } Else {
  28. Mscroller.startscroll (0, 0,-0, 1000);
  29. S1 = false;
  30. }
  31. Invalidate ();
  32. }
  33. }


And then the Wheelactivity.java.

[Java]View Plaincopy
  1. Package COM.WB;
  2. Import android.app.Activity;
  3. Import Android.graphics.Color;
  4. Import Android.os.Bundle;
  5. Import android.view.Gravity;
  6. Import Android.view.View;
  7. Import Android.view.ViewGroup;
  8. Import Android.widget.AbsListView;
  9. Import Android.widget.AbsListView.LayoutParams;
  10. Import Android.widget.AbsListView.OnScrollListener;
  11. Import Android.widget.Adapter;
  12. Import Android.widget.BaseAdapter;
  13. Import Android.widget.ListView;
  14. Import Android.widget.TextView;
  15. Public class Wheelactivity extends Activity {
  16. Private ListView ListView = null;
  17. private Myviewgroup Myviewgroup;
  18. @Override
  19. public void OnCreate (Bundle savedinstancestate) {
  20. super.oncreate (savedinstancestate);
  21. Setcontentview (R.layout.main);
  22. Myviewgroup = (myviewgroup) Findviewbyid (R.id.myviewgroup);
  23. }
  24. public void Scroll (view view) {
  25. Myviewgroup.beginscroll ();
  26. }
  27. }

Main.xml

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <Button
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="scroll"
  10. android:onclick="scroll" />
  11. <com.wb.MyViewGroup
  12. xmlns:android="http://schemas.android.com/apk/res/android"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:orientation= "vertical" android:id="@+id/myviewgroup">
  16. <TextView
  17. android:layout_width="wrap_content"
  18. android:layout_height="fill_parent"
  19. android:background="#ff0000"
  20. android:text="I'm Here"/>
  21. </com.wb.MyViewGroup>
  22. </linearlayout>


Source code: http://download.csdn.net/detail/c_weibin/4208751

Android uses scroller for slow movement

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.