package demo.scrollercompute;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Scroller;
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);
}
}
Android 使用Scroller自动滚动第二种实现
5星 · 超过95%的资源 需积分: 18 79 浏览量
2013-03-18
21:28:47
上传
评论
收藏 18KB ZIP 举报
love_world_
- 粉丝: 706
- 资源: 29
- 1
- 2
前往页