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简单使用)的更多相关文章

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

    主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...

  2. Android学习笔记_11_ListView控件使用

    一.界面设计: 1.activity_main.xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  3. [Android学习笔记]组合控件的使用

    组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...

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

    DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...

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

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

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

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  7. Android学习笔记_57_ExpandableListView控件应用

    1.布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  8. Android学习路-UI控件

  9. 三、Android学习第三天——Activity的布局初步介绍(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...

随机推荐

  1. 1.IntelliJ IDEA搭建SpringBoot的小Demo

    转自:http://www.cnblogs.com/weizaibug/p/6657077.html 首先简单介绍下Spring Boot,来自度娘百科:Spring Boot是由Pivotal团队提 ...

  2. Redis笔记教程

    一.redis简介 1.1.1.什么是redis? REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. 读 ...

  3. winform程序,备份数据库+并压缩+并删除以前的备份

    说明:为了定时备份服务器上的数据库并压缩到指定目录,方便下载到本地而写本程序.配合windows的任务计划,可以达到定时备份数据库的目的. 程序需引用SQLDMO.DLL,如电脑上已安装sqlserv ...

  4. [ACM] POJ 1046 Color Me Less

    Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30146   Accepted: 14634 D ...

  5. 写给自己的TypeScript 入门小纲

    前几日,在知乎上写了一些技术类的文章,有人私信问我,是不是要找一份工作,有没有想过要跳槽,然后我回到,你们公司都是用的什么框架什么技术,他罗列了一堆,其中就包含了TypeScript,我甚至不知道有这 ...

  6. 5W1H分析法和5W2H分析法

    5W1H分析法也称六何分析法,是一种思考方法,也可以说是一种创造技法.是对选定的项目.工序或操作,都要从原因(WHY).对象(WHAT).地点(WHERE).时间(WHEN).人员(WHO).方法(H ...

  7. HDU 1018 Big Number 数学题解

    Problem Description In many applications very large integers numbers are required. Some of these app ...

  8. python的list和数组的区别

    list不是数组(额外安装Pynum) 1)可修改,list数据结构内容可以被程序修改 2)可动态增减,长度不固定 3)list里面的数据项可以是不同类型数据,也可以是list 4)两个list可“链 ...

  9. [WASM] Compile C Code into WebAssembly

    We use the C language instead of pure WAST to create a square root function using WASM Fiddle (https ...

  10. log4cxx入门篇

    log4cxx入门篇     先看官网:http://logging.apache.org/log4cxx/index.html 转载自:http://wenku.baidu.com/view/d88 ...