public class Scroller extends Object
介绍:
This class encapsulates scrolling. You can use scrollers (Scroller or OverScroller) to collect the data you need to produce a scrolling animation—for example, in response to a fling gesture. Scrollers track scroll offsets for you over time, but they don't automatically apply those positions to your view. It's your responsibility to get and apply new coordinates at a rate that will make the scrolling animation look smooth.
该类封装了滚动操作,你可以使用滚动条(滚轮和OverScroller)去收集你需要制作的滚动动画,例如,相应手势,滚动条滚动效果跟踪补偿给你一段时间,但是不会自动去应用这些View的位置,这是你的责任去不断获取和请求新的坐标使得动画看起来更加的光滑。
使用:在ViewGroup中通过使用Scroller来控制子控件的滑动,比如ListVIew,ScrollerVIew,Launcher中的PageView等
如下示例一个Button滑动一段距离的例子,在自定义的MyLinearLayout重写了computeScroll(),其他的ContentLiearnLayout,TestButton,只为了看清楚Log的执行,Scroller类的startScroll是触发滚动的方法。
public class TestActivity extends Activity {
private Scroller mScroller;
LinearLayout lay1, lay0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScroller = new Scroller(this, new DecelerateInterpolator(5.0F));
lay0 = new ContentLinearLayout(this);
lay0.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams p0 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
this.setContentView(lay0, p0);
lay1 = new MyLinearLayout(this);
lay1.setBackgroundColor(this.getResources().getColor(
android.R.color.darker_gray));
//
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
p1.weight = 1;
lay0.addView(lay1, p1);
Button btn1 = new TestButton(this);
btn1.setText("btn in layout1");
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mScroller.startScroll(0, 0, -500, 0, 8000);
}
});
lay1.addView(btn1);
// lay0.setLayerType(View.LAYER_TYPE_HARDWARE, null);
lay1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
class TestButton extends Button {
public TestButton(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("ABC", "button_ondraw");
}
}
class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context ctx) {
super(ctx);
}
public void computeScroll() {
Log.i("ABC", "layout_computeScroll");
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), 0);
Log.i("ABC", "child_layout_scrollTo_X" + mScroller.getCurrX());
}
}
}
class ContentLinearLayout extends LinearLayout {
public ContentLinearLayout(Context ctx) {
super(ctx);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Log.i("ABC", "parent_layout_dispatchDraw");
}
}
}
执行了之后打印的Log如下
01-31 14:40:45.013: I/ABC(3757): child_layout_scrollTo_X=-2
01-31 14:40:45.028: I/ABC(3757): child_layout_scrollTo_X=-14
01-31 14:40:45.113: I/ABC(3757): child_layout_scrollTo_X=-64
01-31 14:40:45.171: I/ABC(3757): child_layout_scrollTo_X=-95
01-31 14:40:45.246: I/ABC(3757): child_layout_scrollTo_X=-132
01-31 14:40:45.317: I/ABC(3757): child_layout_scrollTo_X=-164
01-31 14:40:45.381: I/ABC(3757): child_layout_scrollTo_X=-191
01-31 14:40:45.395: I/ABC(3757): child_layout_scrollTo_X=-197
01-31 14:40:45.416: I/ABC(3757): child_layout_scrollTo_X=-205
01-31 14:40:45.433: I/ABC(3757): child_layout_scrollTo_X=-211
01-31 14:40:45.506: I/ABC(3757): child_layout_scrollTo_X=-238
01-31 14:40:45.572: I/ABC(3757): child_layout_scrollTo_X=-260
01-31 14:40:45.647: I/ABC(3757): child_layout_scrollTo_X=-283
01-31 14:40:45.663: I/ABC(3757): child_layout_scrollTo_X=-288
01-31 14:40:45.725: I/ABC(3757): child_layout_scrollTo_X=-305
01-31 14:40:45.785: I/ABC(3757): child_layout_scrollTo_X=-321
01-31 14:40:45.847: I/ABC(3757): child_layout_scrollTo_X=-335
01-31 14:40:45.865: I/ABC(3757): child_layout_scrollTo_X=-339
01-31 14:40:45.885: I/ABC(3757): child_layout_scrollTo_X=-344
01-31 14:40:45.900: I/ABC(3757): child_layout_scrollTo_X=-347
01-31 14:40:45.971: I/ABC(3757): child_layout_scrollTo_X=-362
01-31 14:40:45.988: I/ABC(3757): child_layout_scrollTo_X=-365
01-31 14:40:46.006: I/ABC(3757): child_layout_scrollTo_X=-369
01-31 14:40:46.022: I/ABC(3757): child_layout_scrollTo_X=-372
01-31 14:40:46.043: I/ABC(3757): child_layout_scrollTo_X=-375
01-31 14:40:46.061: I/ABC(3757): child_layout_scrollTo_X=-378
01-31 14:40:46.076: I/ABC(3757): child_layout_scrollTo_X=-381
01-31 14:40:46.096: I/ABC(3757): child_layout_scrollTo_X=-384
01-31 14:40:46.112: I/ABC(3757): child_layout_scrollTo_X=-387
01-31 14:40:46.130: I/ABC(3757): child_layout_scrollTo_X=-390
01-31 14:40:46.191: I/ABC(3757): child_layout_scrollTo_X=-400
01-31 14:40:46.248: I/ABC(3757): child_layout_scrollTo_X=-407
01-31 14:40:46.312: I/ABC(3757): child_layout_scrollTo_X=-416
01-31 14:40:46.375: I/ABC(3757): child_layout_scrollTo_X=-424
01-31 14:40:46.436: I/ABC(3757): child_layout_scrollTo_X=-430
01-31 14:40:46.500: I/ABC(3757): child_layout_scrollTo_X=-437
01-31 14:40:46.516: I/ABC(3757): child_layout_scrollTo_X=-438
01-31 14:40:46.534: I/ABC(3757): child_layout_scrollTo_X=-440
01-31 14:40:46.556: I/ABC(3757): child_layout_scrollTo_X=-442
01-31 14:40:46.572: I/ABC(3757): child_layout_scrollTo_X=-443
01-31 14:40:46.658: I/ABC(3757): child_layout_scrollTo_X=-451
01-31 14:40:46.719: I/ABC(3757): child_layout_scrollTo_X=-455
01-31 14:40:46.789: I/ABC(3757): child_layout_scrollTo_X=-460
01-31 14:40:46.805: I/ABC(3757): child_layout_scrollTo_X=-461
01-31 14:40:46.871: I/ABC(3757): child_layout_scrollTo_X=-465
01-31 14:40:46.942: I/ABC(3757): child_layout_scrollTo_X=-469
01-31 14:40:47.008: I/ABC(3757): child_layout_scrollTo_X=-472
01-31 14:40:47.073: I/ABC(3757): child_layout_scrollTo_X=-475
01-31 14:40:47.090: I/ABC(3757): child_layout_scrollTo_X=-476
01-31 14:40:47.111: I/ABC(3757): child_layout_scrollTo_X=-476
在绘制Button的时候在持续的调用scrollto造成顺滑滚动的效果