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