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. 12.模板别名以及auto定义返回值

    #include <iostream> #include <array> using namespace std; //定义返回值类型 template<class T1 ...

  2. 15、python学习手册之:python语句、赋值、表达式和打印

    1.语句的另一个特殊规则是用一对括号把语句括起来就可以:括号().方括号[].字典的大括号{}.任何括在这些符号里的程序代码都可横跨好几行. 2.括号是可以包含一切的,因为任何表达式都可以包含在内,只 ...

  3. hibernate 注解配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/X

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. 关于python的冒号截取

    https://zhidao.baidu.com/question/877855739656978372.html

  5. WebService--CXF以及CXF与Spring的整合(jaxws:server形式配置)

    前言:好记性不如烂笔头,写博客的好处是,以前接触的东西即便忘记了,也可以从这里查找. Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来 ...

  6. 异步FIFO设计

    参考http://www.cnblogs.com/BitArt/archive/2013/04/10/3010073.html http://blog.sina.com.cn/s/blog_6d30f ...

  7. IIS服务器设置http自动跳转https方法

    很多站长在部署SSL证书后,网站实现https加密访问,但考虑到用户习惯了http访问,很多外链也是http访问形式,所以需要在IIS服务器配置http自动跳转https,避免用户通过http访问不到 ...

  8. Android Java Mail与Apache Mail发送邮件对比

    原文链接: 一.邮件简介  一封邮件由很多信息构成,主要的信息如下,其他的暂时不考虑,例如抄送等:  1.收件人:收件人的邮箱地址,例如xxx@xx.com  2.收件人姓名:大部分的邮件显示时都会显 ...

  9. keil出现一些库函数没有定义

  10. Shiro的Filter机制详解---源码分析(转)

    Shiro的Filter机制详解 首先从spring-shiro.xml的filter配置说起,先回答两个问题: 1, 为什么相同url规则,后面定义的会覆盖前面定义的(执行的时候只执行最后一个). ...