Android Scroller简单用法,字节跳动+京东+360+网易面试题整理
作者:互联网
-
mScroller.setFinalY(int newY) //设置mScroller最终停留的竖直位置,没有动画效果,直接跳到目标位置
-
//滚动,startX, startY为开始滚动的位置,dx,dy为滚动的偏移量, duration为完成滚动的时间
-
mScroller.startScroll(int startX, int startY, int dx, int dy) //使用默认完成时间250ms
-
mScroller.startScroll(int startX, int startY, int dx, int dy, int duration)
-
mScroller.computeScrollOffset() //返回值为boolean,true说明滚动尚未完成,false说明滚动已经完成。这是一个很重要的方法,通常放在View.computeScroll()中,用来判断是否滚动是否结束。
Scroller这个类理解起来有一定的困难,刚开始接触Scroller类的程序员可能无法理解Scroller和View系统是怎么样联系起来的。我经过自己的学习和实践,对Scroller的用法和工作原理有了一定的理解,在这里和大家分享一下,希望大家多多指教。
首先从源码开始分析:
View.java
[java] view plain copy
-
/**
-
* Called by a parent to request that a child update its values for mScrollX
-
* and mScrollY if necessary. This will typically be done if the child is
-
* animating a scroll using a {@link android.widget.Scroller Scroller}
-
* object.
-
*/
-
public void computeScroll()
-
{
-
}
computeScroll是一个空函数,很明显我们需要去实现它,至于做什么,就由我们自己来决定了。
因为View的子类很多,在下面的例子中,我会在一个自定义的类MyLinearLayout中去实现它。
ViewGroup.java
[java] view plain copy
-
@Override
-
protected void dispatchDraw(Canvas canvas) {
-
…
-
…
-
…
-
…
-
for (int i = 0; i < count; i++) {
-
final View child = children[i];
-
if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)
-
{
-
more |= drawChild(canvas, child, drawingTime);
-
}
-
…
-
…
-
…
从dispatchDraw函数可以看出,ViewGroup会对它的每个孩子调用drawChild(), 在下面的例子中, ContentLinearLayout的孩子有2个,是2个MyLinearLayout类型的实例。
再看看drawChild函数:
[java] view plain copy
-
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
-
…
-
…
-
child.computeScroll();
-
…
-
…
-
}
看到这里,我想大家应该就明白了,在父容器重画自己的孩子时,它会调用孩子的computScroll方法,也就是说例程中的ContentLinearLayout在调用dispatchDraw()函数时会调用MyLinearLayout的computeScroll方法。
这个computeScroll()函数正是我们大展身手的地方,在这个函数里我们可以去取得事先设置好的成员变量mScroller中的位置信息、速度信息等等,用这些参数来做我们想做的事情。
看到这里大家一定迫不及待的想看代码了,代码如下:
[java] view plain copy
-
package com.yulongfei.scroller;
-
import android.widget.LinearLayout;
-
import android.widget.Scroller;
-
import android.app.Activity;
-
import android.content.Context;
-
import android.graphics.Canvas;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.Button;
-
import android.view.View.OnClickListener;
-
public class TestScrollerActivity extends Activity {
-
private static final String TAG = “TestScrollerActivity”;
-
LinearLayout lay1,lay2,lay0;
-
private Scroller mScroller;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
mScroller = new Scroller(this);
-
lay1 = new MyLinearLayout(this);
-
lay2 = new MyLinearLayout(this);
-
lay1.setBackgroundColor(this.getResources().getColor(android.R.color.darker_gray));
-
lay2.setBackgroundColor(this.getResources().getColor(android.R.color.white));
-
lay0 = new ContentLinearLayout(this);
-
lay0.setOrientation(LinearLayout.VERTICAL);
-
LinearLayout.LayoutParams p0 = new LinearLayout.LayoutParams
-
(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
-
this.setContentView(lay0, p0);
-
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams
-
(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
-
p1.weight=1;
-
lay0.addView(lay1,p1);
-
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams
-
(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
-
p2.weight=1;
-
lay0.addView(lay2,p2);
-
MyButton btn1 = new MyButton(this);
-
MyButton btn2 = new MyButton(this);
-
btn1.setText(“btn in layout1”);
-
btn2.setText(“btn in layout2”);
-
btn1.setOnClickListener(new OnClickListener(){
-
@Override
-
public void onClick(View v) {
-
mScroller.startScroll(0, 0, -30, -30, 50);
-
}
-
});
-
btn2.setOnClickListener(new OnClickListener(){
-
@Override
-
public void onClick(View v) {
-
mScroller.startScroll(20, 20, -50, -50, 50);
-
}
-
});
-
lay1.addView(btn1);
-
lay2.addView(btn2);
-
}
-
class MyButton extends Button
-
{
-
public MyButton(Context ctx)
-
{
-
super(ctx);
-
}
-
@Override
-
protected void onDraw(Canvas canvas)
-
{
-
super.onDraw(canvas);
-
Log.d(“MyButton”, this.toString() + " onDraw------");
-
}
-
}
-
class MyLinearLayout extends LinearLayout
-
{
-
public MyLinearLayout(Context ctx)
-
{
-
super(ctx);
-
}
-
@Override
-
/**
-
* Called by a parent to request that a child update its values for mScrollX
-
* and mScrollY if necessary. This will typically be done if the child is
-
* animating a scroll using a {@link android.widget.Scroller Scroller}
-
* object.
-
*/
-
public void computeScroll()
-
{
-
Log.d(TAG, this.toString() + " computeScroll-----------");
标签:面试题,Scroller,LayoutParams,LinearLayout,int,new,android,Android 来源: https://blog.csdn.net/m0_61362539/article/details/121225405