Android 为什么赢了';使用Scroller时,我的自定义视图是否会滚动?

Android 为什么赢了';使用Scroller时,我的自定义视图是否会滚动?,android,android-scroll,Android,Android Scroll,我试图使用滚动条移动自定义视图,但视图不移动。有人能帮我找到我的代码有什么问题吗 public class CustomView extends View { private Scroller mScroller; public CustomView(Context context, AttributeSet attrs) { super(context, attrs); mScroller = new Scroller(context);

我试图使用滚动条移动自定义视图,但视图不移动。有人能帮我找到我的代码有什么问题吗

public class CustomView extends View {

    private Scroller mScroller;

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    public void smoothScrollTo(int destX, int destY) {
        int scrollX = getScrollX();
        int deltaX = destX - scrollX;

        int scrollY = getScrollY();
        int deltaY = destY - scrollY;

        mScroller.startScroll(scrollX, scrollY, deltaX, deltaY, 1000);

        invalidate();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            int currX = mScroller.getCurrX();
            int currY = mScroller.getCurrY();

            Log.d("SCROLL", "currX = " + currX + ", currY = " + currY);

            scrollTo(currX, currY);
            postInvalidate();
        }
    }


public class MainActivity extends AppCompatActivity {

    private CustomView mCustomView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCustomView = (CustomView)findViewById(R.id.custom_view);
    }

    public void onStart(View view) {
        mCustomView.smoothScrollTo(410, 200);
    }
}
布局XML文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context="com.qihoo.scrollerdemo.MainActivity">

    <com.sample.scrollerdemo.CustomView
        android:id="@+id/custom_view"
        android:background="#ff0"
        android:layout_width="50dp"
        android:layout_height="50dp" />


    <Button
        android:onClick="onStart"
        android:text="start"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>


@Thilek我的代码是从Android书籍中复制的,我只是尝试从ScrollView扩展它,它不起作用。你的滚动程序代码似乎是正确的。我相信问题可能出在你的
draw
code上。如果查看
视图
类的源代码,
getScrollX()
scrollTo(x,y)
只是内部变量的集合/获取。在
onDraw
过程中,必须使用
getScroll
并相应地更改图形。您的
日志如何?你能看到这些值吗?(查看<代码>的来源<代码>:)从我的日志中,值是正确的。我没有重写OnDraw方法的代码。那么,如果视图没有在屏幕上绘制任何内容,您希望“滚动”什么呢?