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学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用),布布扣,bubuko.com

时间: 2024-08-04 10:16:53

Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)的相关文章

十三、Android学习笔记_Andorid控件样式汇总

<!-- 设置activity为透明 --> <style name="translucent"> <item name="android:windowBackground">@color/translucent</item> <item name="android:windowIsTranslucent">true</item> <item name="an

Android学习笔记_75_Andorid控件样式汇总

<!-- 设置activity为透明 --> <style name="translucent"> <item name="android:windowBackground">@color/translucent</item> <item name="android:windowIsTranslucent">true</item> <item name="an

android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件

主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: 1 <TextView 2 3 android:layout_width="wrap_content" 4 5 android:layout_height="wrap_content" 6 7 android:textColor="#0000ff" 8 9 android:textSize="40sp

Android学习笔记--RecyclerView控件的使用

首先我们要依赖 compile 'com.android.support:recyclerview-v7:25.0.0'之后才可以使用 recyclerview XMl文件 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:t

android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)

DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础上提供了一些方法来获取用户所选日期.时间: 程序中如果需要获取用户选择的日期.时间,可通过添加DataPicker添加OnDataChangeListener进行监听,为TimePicker添加OnTimeChangeListener进行监听. 实例——略 进度条==> ProgressBar.标题进度

android学习笔记八——控件

SeekBar——拖动条 拖动条(SeekBar)组件与ProgressBar水平形式的显示进度条类似,不过其最大的区别在于,拖动条可以由用户自己进行手工的调节,例如当用户需要调整播放器音量或者电影的播放速度都会使用到拖动条SeekBar类. android:thumb,指定一个Drawable对象,该对象将作为自定义滑块:为了实现程序拖动滑块位置的改变,程序可以绑定一个OnSeekBarChangeListener监听器. android:thumb,用于改变拖动条上滑块的外观. 布局文件==

Android学习笔记-常用控件

单选按钮 Radio         <RadioGroup              android:id="@+id/genderGroup"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:orientation="vertical"

Android学习笔记之控件Spinner

1.mian.xml布局文件 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_p

Android学习笔记之控件DatePicker和TimePicker

1.xml布局文件 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent