关于Android中的ScrollBy和ScrollTo方法相信大家并不陌生,这两个方法是在View中实现的。所以在各个继承了View的类都可以使用改方法。

在View中对这两个方法的源码编写是这样的,有兴趣的朋友可以研究一下:

/**

 

/**
* Set the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the x position to scroll to
* @param y the y position to scroll to
*/
public void scrollTo(int x, int y) {
if (mScrollX != x || mScrollY != y) {
int oldX = mScrollX;
int oldY = mScrollY;
mScrollX = x;
mScrollY = y;
invalidateParentCaches();
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
if (!awakenScrollBars()) {
postInvalidateOnAnimation();
}
}
}

/**
* Move the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the amount of pixels to scroll by horizontally
* @param y the amount of pixels to scroll by vertically
*/
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}

 

 

首先我们来看看To方法,它有一个这样的判断
if (mScrollX != x || mScrollY != y)
这是什么意思呢,这里出现了mScrollX和mScrollY两个数值,做动画的朋友肯定对这两个参数不会陌生。 mScrollX是原点(也就是左上角)到指定View的左上角的X轴距离,mScrollY亦然。
这时这个判断的意义就明确了,就是用于判断移动量是否是View当前原点,如果不是,则开始下面的代码

接下来就是交换保存值,刷新视图,开始调用onScrollChanged方法移动View位置。

而by方法其实就是在调用To方法,这时也就能看出,To单次移动的,而By可以反复的按照自己所给的值移动

 


在这里特别说一下,我们都知道在Android中,坐标原点是在左上角,往右代表x,往下代表Y ,在eclipse中,在填这两个参数时若要往下xy移动20,写法是ScrollBy(-20,-20);
而在Android studio中写法则是ScrollBy(-+20,-+20);来代表放下分别移动20点,如果直接填写20,会编译不通过。修改成-+20则不会出问题,不知道是否是我一个人有这个问题。


接下来测试一下这两个方法。

 

在布局文件中放入两个Button,一个是用于By另一个是To,代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xh.admin.myscrolltest.MainActivity">


<Button
android:id="@+id/scrollBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="By"
/>

<Button
android:id="@+id/srcollTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/scrollBy"
android:text="To"
/>


</RelativeLayout>


而在MainActivity中简单的几行代码代码量很小,直接写到一个方法里。
package com.xh.admin.myscrolltest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import static com.xh.admin.myscrolltest.R.id.scrollBy;

public class MainActivity extends AppCompatActivity {

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

final RelativeLayout re = (RelativeLayout) findViewById(R.id.activity_main);
Button by = (Button) findViewById(scrollBy);
Button To = (Button) findViewById(R.id.srcollTo);

by.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
re.scrollBy(-+20, -+20);

}
});

To.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
re.scrollTo(-+20,-+20);
}
});
}
}

至于为什么Scroll方法中的值是负数,而不是正数,这有一篇文章写的很好:http://www.tuicool.com/articles/uM7ruy
,在运行这个小的Test代码时会发现一个问题,无论点击那个按钮,另一个按钮也会一起移动,就算是添加与Scroll方法无关的控件,也一样会移动,千万不要以为这是因为相对布局原因,线性布局中也是一样的,
这是因为,ScrollBy和 To 的行为是,哪个View调用的它,哪个View的内容就开始整个移动,如果是Button调用的其中某个方法,那么Button的位置不会变,但是Button中的内容会移动,现在我们希望Button移动
那马我们就指定Button的父容器来调用这两个方法。

如果想要在某个界面中只希望一部分内容移动,可以嵌套布局方法,指定某一个ViewGroup移动。

最近在slidingmenu和ViewPager中常接触到这两个方法,特此记录,写的很差,大牛勿喷

posted on 2017-03-03 09:50  霞辉  阅读(4178)  评论(0编辑  收藏  举报