控件平移划过屏幕 (Scroller简单使用)

PS:

该篇博客已经deprecated,不再维护,详情请参见 

站在源码的肩膀上全解Scroller工作机制

 http://blog.csdn.NET/lfdfhl/article/details/53143114

 

MainActivity如下:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cc.cn;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.app.Activity;  
  8. /** 
  9.  * Demo描述: 
  10.  * Scroller使用示例——让控件平移划过屏幕 
  11.  *  
  12.  *  
  13.  * 注意事项: 
  14.  * 1 在布局中将cc.cn.LinearLayoutSubClass的控件的宽度设置为"fill_parent" 
  15.  *   便于观察滑动的效果 
  16.  */  
  17. public class MainActivity extends Activity {  
  18.     private Button mButton;  
  19.     private LinearLayoutSubClass mLinearLayoutSubClass;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         init();  
  25.     }  
  26.       
  27.     private void init(){  
  28.         mLinearLayoutSubClass=(LinearLayoutSubClass) findViewById(R.id.linearLayoutSubClass);  
  29.         mButton=(Button) findViewById(R.id.button);  
  30.         mButton.setOnClickListener(new OnClickListener() {  
  31.             @Override  
  32.             public void onClick(View view) {  
  33.                 mLinearLayoutSubClass.beginScroll();  
  34.             }  
  35.         });  
  36.     }  
  37.   
  38. }  


LinearLayoutSubClass如下:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cc.cn;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.LinearLayout;  
  6. import android.widget.Scroller;  
  7. /** 
  8.  * API注释: 
  9.  *  
  10.  * 1 //第一,二个参数起始位置;第三,四个滚动的偏移量;第五个参数持续时间 
  11.  *   startScroll(int startX, int startY, int dx, int dy, int duration) 
  12.  *    
  13.  * 2 //在startScroll()方法执行过程中即在duration时间内computeScrollOffset() 
  14.  *   方法会一直返回true,但当动画执行完成后会返回返加false. 
  15.  *   computeScrollOffset() 
  16.  *    
  17.  * 3 当执行ontouch()或invalidate()或postInvalidate()均会调用该方法  
  18.  *   computeScroll() 
  19.  * 
  20.  */  
  21. public class LinearLayoutSubClass extends LinearLayout {  
  22.     private Scroller mScroller;  
  23.     private boolean flag=true;  
  24.       
  25.     public LinearLayoutSubClass(Context context) {  
  26.         super(context);  
  27.     }  
  28.       
  29.     public LinearLayoutSubClass(Context context, AttributeSet attrs) {  
  30.         super(context, attrs);  
  31.         //也可采用该构造方法传入一个interpolator  
  32.         //mScroller=new Scroller(context, interpolator);  
  33.         mScroller=new Scroller(context);  
  34.     }  
  35.       
  36.     @Override  
  37.     public void computeScroll() {  
  38.         super.computeScroll();  
  39.         if(mScroller.computeScrollOffset()){  
  40.             scrollTo(mScroller.getCurrX(), 0);  
  41.             //使其再次调用computeScroll()直至滑动结束,即不满足if条件  
  42.             postInvalidate();  
  43.         }  
  44.     }  
  45.       
  46.     public void beginScroll(){  
  47.         if (flag) {  
  48.             mScroller.startScroll(0, 0, -2500, 0, 2500);  
  49.             flag = false;  
  50.         } else {  
  51.             mScroller.startScroll(0, 0, 0, 0, 1500);  
  52.             flag = true;  
  53.         }  
  54.         //调用invalidate();使其调用computeScroll()  
  55.         invalidate();  
  56.     }  
  57.   
  58. }  


main.xml如下:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent" >  
    5.   
    6.     <Button  
    7.         android:id="@+id/button"  
    8.         android:layout_width="wrap_content"  
    9.         android:layout_height="wrap_content"  
    10.         android:layout_centerHorizontal="true"  
    11.         android:text="点击后滑动" />  
    12.   
    13.     <cc.cn.LinearLayoutSubClass  
    14.         android:id="@+id/linearLayoutSubClass"  
    15.         android:layout_width="fill_parent"  
    16.         android:layout_height="wrap_content"  
    17.         android:layout_centerVertical="true"  
    18.      >  
    19.   
    20.         <TextView  
    21.             android:layout_width="wrap_content"  
    22.             android:layout_height="wrap_content"  
    23.             android:background="#ff1100"  
    24.             android:text="测试Scroller" />  
    25.     </cc.cn.LinearLayoutSubClass>  
    26.   
    27. </RelativeLayout>  
posted @ 2016-11-26 15:20  天涯海角路  阅读(124)  评论(0)    收藏  举报