一、不多说先上效果图
二、scrollBy和scrollTo的底层实现代码如下(在View.class系统类中)
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();
}
}
}
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}
使用这个两个view自带的方法可以实现控制View的移动
三、MainActivity.java代码如下
package net.dxs.myscrolltodemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private LinearLayout test_layout;
private Button btn_x_add;
private Button btn_x_sub;
private Button btn_y_add;
private Button btn_y_sub;
private Button btn_to_zz;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test_layout = (LinearLayout) findViewById(R.id.test_layout);
btn_x_add = (Button) findViewById(R.id.btn_x_add);
btn_x_sub = (Button) findViewById(R.id.btn_x_sub);
btn_y_add = (Button) findViewById(R.id.btn_y_add);
btn_y_sub = (Button) findViewById(R.id.btn_y_sub);
btn_to_zz = (Button) findViewById(R.id.btn_to_zz);
btn_x_add.setOnClickListener(this);
btn_x_sub.setOnClickListener(this);
btn_y_add.setOnClickListener(this);
btn_y_sub.setOnClickListener(this);
btn_to_zz.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_x_add://左
test_layout.scrollBy(10, 0);//mScrollX \ mScrollY 这二个值的坐标系与其他坐标系不同
// 他为水平向左为正数 、 竖直向上为正数.
break;
case R.id.btn_x_sub://右
test_layout.scrollBy(-10, 0);
break;
case R.id.btn_y_add://上
test_layout.scrollBy(0, 10);
break;
case R.id.btn_y_sub://下
test_layout.scrollBy(0, -10);
break;
case R.id.btn_to_zz://指定回到左上角
test_layout.scrollTo(0, 0);
break;
}
}
}
四、布局activity_main.xml代码如下
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/test_layout"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_margin="10dip"
android:layout_weight="2"
android:background="@drawable/a3"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_dark"
android:orientation="horizontal"
android:padding="15dp" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="丛林小精灵" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_y_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scroll Y+10"
android:textSize="14dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_x_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scroll X+10"
android:textSize="14dip" />
<Button
android:id="@+id/btn_to_zz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scrollTo 0,0" />
<Button
android:id="@+id/btn_x_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scroll X-10"
android:textSize="14dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:textSize="14dip" >
<Button
android:id="@+id/btn_y_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scroll Y-10"
android:textSize="14dip" />
</LinearLayout>
</LinearLayout>
</LinearLayout>