Android中的scrollBy和scrollTo

一、不多说先上效果图



二、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>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值