之前写的《Android 使用Scroller自动滚动第一种实现》, 通过Runnable中获取滚动后地址,当前例子是使用覆写computeScroll方法获取滚动后地址。
一、有图有真相
二、实现介绍
例子比较简单 Activity + ViewGroup
1. Activity源码
public class ScrollerDemoActivity extends Activity {
// ===========================================================
// Constants
// ===========================================================
private static final String INFO = "白色背景CustomLinearLayout," +
"包含一个灰色TextView \n 以下显示的是CustomLinearLayout的参数";
// ===========================================================
// Fields
// ===========================================================
private TextView mOutput;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Public Methods
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOutput = (TextView) findViewById(R.id.output);
mOutput.setText(INFO
+ "\n x = "
+ "\n y = "
+ "\n scroll X = "
+ "\n scroll Y = ");
final CustomLinearLayout customLayout = (CustomLinearLayout) findViewById(R.id.custom_linearlayout);
customLayout.setOnLogListener(new CustomLogListener());
// 向左上移动
Button leftButton = (Button) findViewById(R.id.left_button);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
customLayout.scrollToLeft();
}
});
// 向右下移动
View rightButton = findViewById(R.id.right_button);
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
customLayout.scrollToRight();
}
});
}
// ===========================================================
// Private Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
private class CustomLogListener implements OnLogListener {
@Override
public void setOutput(String pOutput) {
mOutput.setText(INFO + pOutput);
}
}
}
2. ViewGroup源码
public class CustomLinearLayout extends LinearLayout {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
private Scroller mScroller;
private OnLogListener mOnLogListener;
// ===========================================================
// Constructors
// ===========================================================
public CustomLinearLayout(Context context) {
super(context);
initCustomLinearLayout(context);
}
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initCustomLinearLayout(context);
}
// ===========================================================
// Public Methods
// ===========================================================
public void scrollToRight() {
// 以提供的起始点和将要滑动的距离开始滚动。滚动会使用缺省值250ms作为持续时间。
mScroller.startScroll(0, 0, -300, -400, 9000);
invalidate();
updateLog();
}
public void scrollToLeft() {
mScroller.startScroll(-300, -400, 300, 400, 9000);
invalidate();
updateLog();
}
public void logUtil(String output) {
Log.e("CustomLinearLayout", output);
}
public void setOnLogListener(OnLogListener pLogListener) {
mOnLogListener = pLogListener;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
public void computeScroll() {
if (!mScroller.isFinished()) {
// computeScrollOffset() 返回false表示滚动结束
if (mScroller.computeScrollOffset()) {
updateLog();
// 设置滚动到何处
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
// 让系统重绘视图
invalidate();
}
}
}
// ===========================================================
// Private Methods
// ===========================================================
private void initCustomLinearLayout(Context context) {
mScroller = new Scroller(context);
}
private void updateLog() {
if (mOnLogListener != null) {
mOnLogListener.setOutput("\n x = " + getLeft()
+ "\n y = " + getTop()
+ "\n scroll X = " + getScrollX()
+ "\n scroll Y = " + getScrollY());
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public interface OnLogListener {
public void setOutput(String pOutput);
}
}
三、源码
原文地址:http://blog.youkuaiyun.com/love_world_/article/details/8689077