zoukankan      html  css  js  c++  java
  • Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)

    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使用演示样例——让控件平移划过屏幕
     * 
     * 參考资料:
     * http://blog.csdn.net/c_weibin/article/details/7438323
     * Thank you very much
     * 
     * 注意事项:
     * 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>

  • 相关阅读:
    Android Studio 开发
    Jsp编写的页面如何适应手机浏览器页面
    电影
    Oracle 拆分列为多行 Splitting string into multiple rows in Oracle
    sql server 2008 自动备份
    WINGIDE 激活失败
    python安装 错误 “User installations are disabled via policy on the machine”
    ble编程-外设发送数据到中心
    iOS开发-NSString去掉所有换行及空格
    ios9 字符串与UTF-8 互相转换
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4279831.html
Copyright © 2011-2022 走看看