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
- @Override
- Public void Computescroll () {
- if (Mscroller.computescrolloffset ()) {
- ScrollTo (Mscroller.getcurrx (), 0);
- Postinvalidate ();
- }
- }
The source code for the example above is attached below
First, Myviewgroup.java.
[Java]View Plaincopy
- Package COM.WB;
- Import Android.content.Context;
- Import Android.util.AttributeSet;
- Import Android.view.View;
- Import Android.view.ViewGroup;
- Import Android.widget.LinearLayout;
- Import Android.widget.Scroller;
- Public class Myviewgroup extends LinearLayout {
- Private Boolean s1=true;
- Scroller mscroller=null;
- Public Myviewgroup (context context, AttributeSet attrs) {
- Super (context, attrs);
- mscroller=New Scroller (context);
- //TODO auto-generated constructor stub
- }
- @Override
- public void Computescroll () {
- if (Mscroller.computescrolloffset ()) {
- ScrollTo (Mscroller.getcurrx (), 0);
- Postinvalidate ();
- }
- }
- public void Beginscroll () {
- if (!s1) {
- Mscroller.startscroll (0, 0, 0, 0, 1000);
- S1 = true;
- } Else {
- Mscroller.startscroll (0, 0,-0, 1000);
- S1 = false;
- }
- Invalidate ();
- }
- }
And then the Wheelactivity.java.
[Java]View Plaincopy
- Package COM.WB;
- Import android.app.Activity;
- Import Android.graphics.Color;
- Import Android.os.Bundle;
- Import android.view.Gravity;
- Import Android.view.View;
- Import Android.view.ViewGroup;
- Import Android.widget.AbsListView;
- Import Android.widget.AbsListView.LayoutParams;
- Import Android.widget.AbsListView.OnScrollListener;
- Import Android.widget.Adapter;
- Import Android.widget.BaseAdapter;
- Import Android.widget.ListView;
- Import Android.widget.TextView;
- Public class Wheelactivity extends Activity {
- Private ListView ListView = null;
- private Myviewgroup Myviewgroup;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Myviewgroup = (myviewgroup) Findviewbyid (R.id.myviewgroup);
- }
- public void Scroll (view view) {
- Myviewgroup.beginscroll ();
- }
- }
Main.xml
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="scroll"
- android:onclick="scroll" />
- <com.wb.MyViewGroup
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation= "vertical" android:id="@+id/myviewgroup">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:background="#ff0000"
- android:text="I'm Here"/>
- </com.wb.MyViewGroup>
- </linearlayout>
Source code: http://download.csdn.net/detail/c_weibin/4208751
Android uses scroller for slow movement