控件平移划过屏幕 (Scroller简单使用)
PS:
该篇博客已经deprecated,不再维护,详情请参见
站在源码的肩膀上全解Scroller工作机制
http://blog.csdn.NET/lfdfhl/article/details/53143114
MainActivity如下:
- package cc.cn;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.app.Activity;
- /**
- * Demo描述:
- * Scroller使用示例——让控件平移划过屏幕
- *
- *
- * 注意事项:
- * 1 在布局中将cc.cn.LinearLayoutSubClass的控件的宽度设置为"fill_parent"
- * 便于观察滑动的效果
- */
- public class MainActivity extends Activity {
- private Button mButton;
- private LinearLayoutSubClass mLinearLayoutSubClass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init(){
- mLinearLayoutSubClass=(LinearLayoutSubClass) findViewById(R.id.linearLayoutSubClass);
- mButton=(Button) findViewById(R.id.button);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- mLinearLayoutSubClass.beginScroll();
- }
- });
- }
- }
LinearLayoutSubClass如下:
- package cc.cn;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.widget.LinearLayout;
- import android.widget.Scroller;
- /**
- * API注释:
- *
- * 1 //第一,二个参数起始位置;第三,四个滚动的偏移量;第五个参数持续时间
- * startScroll(int startX, int startY, int dx, int dy, int duration)
- *
- * 2 //在startScroll()方法执行过程中即在duration时间内computeScrollOffset()
- * 方法会一直返回true,但当动画执行完成后会返回返加false.
- * computeScrollOffset()
- *
- * 3 当执行ontouch()或invalidate()或postInvalidate()均会调用该方法
- * computeScroll()
- *
- */
- public class LinearLayoutSubClass extends LinearLayout {
- private Scroller mScroller;
- private boolean flag=true;
- public LinearLayoutSubClass(Context context) {
- super(context);
- }
- public LinearLayoutSubClass(Context context, AttributeSet attrs) {
- super(context, attrs);
- //也可采用该构造方法传入一个interpolator
- //mScroller=new Scroller(context, interpolator);
- mScroller=new Scroller(context);
- }
- @Override
- public void computeScroll() {
- super.computeScroll();
- if(mScroller.computeScrollOffset()){
- scrollTo(mScroller.getCurrX(), 0);
- //使其再次调用computeScroll()直至滑动结束,即不满足if条件
- postInvalidate();
- }
- }
- public void beginScroll(){
- if (flag) {
- mScroller.startScroll(0, 0, -2500, 0, 2500);
- flag = false;
- } else {
- mScroller.startScroll(0, 0, 0, 0, 1500);
- flag = true;
- }
- //调用invalidate();使其调用computeScroll()
- invalidate();
- }
- }
main.xml如下:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:text="点击后滑动" />
- <cc.cn.LinearLayoutSubClass
- android:id="@+id/linearLayoutSubClass"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#ff1100"
- android:text="测试Scroller" />
- </cc.cn.LinearLayoutSubClass>
- </RelativeLayout>

浙公网安备 33010602011771号