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>

最新文章

  1. ThinkPhp 3.2 常见问题与注意事项
  2. Overview of OpenCascade Library
  3. JSP页面中的精确到秒的时间控件
  4. Netbeans 中创建数据连接池和数据源步骤(及解决无法ping通问题)
  5. xlat指令...
  6. MySQL学习笔记——约束
  7. 删除SSMS中保存的帐户信息
  8. java截取字符串中的数字
  9. magento中比较好的博客
  10. Cortex-M3 动态加载二(RWPI数据无关实现)
  11. BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )
  12. extjs4 前台导出grid数据 生成excel,数据量大后台无法接收到数据
  13. java基础解析系列(六)---深入注解原理及使用
  14. Hadoop:Hadoop简介及环境配置
  15. Python开发 文件操作
  16. tyvj/joyoi 2018 小猫爬山
  17. 关于web-root和web-inf的用处
  18. postgresql copy的使用方式
  19. 【论文阅读】Sliding Line Point Regression for Shape Robust Scene Text Detection
  20. Linux 挂载 xshell 命令 配置环境变量

热门文章

  1. 1.2 Use Cases中 Metrics官网剖析(博主推荐)
  2. 微信消息体加解密及EncodingAESKey
  3. Java网络编程之TCP、UDP
  4. android之路Gallery 画廊
  5. echarts+百度地图+vue 填坑记(一)(百度地图、鼠标移入移出标注,信息框会产生闪烁)
  6. Centos 6 vnc 部署
  7. APP测试10点
  8. 动态布局Cell的高度
  9. Android应用性能优化系列视图篇——隐藏在资源图片中的内存杀手
  10. js进阶 11-22/23 js如何实现选项卡