Android中的scrollBy和scrollTo
来源:互联网 发布:淘宝网热线电话 编辑:程序博客网 时间:2024/05/16 06:05
一、不多说先上效果图
二、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;@Overrideprotected 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);}@Overridepublic 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
- Android中的scrollBy和scrollTo
- View中的scrollTo和scrollBy
- view中的scrollto 和 scrollby
- View中的ScrollTo和ScrollBy
- Android View 中的scrollBy() scrollTo() getScrollY() getScrollX()
- android中scrollTo和scrollBy的理解~~
- Android View 的scrollTo 和 scrollBy方法
- android中scrollTo和scrollBy的理解
- android中scrollTo和scrollBy的理解~~
- 【Android 疑难杂症】scrollTo和scrollBy探究
- android中scrollTo和scrollBy的理解
- Android scrollTo和scrollBy方法使用说明
- Android 浅谈scrollTo和scrollBy源码
- Android View scrollTo scrollBy
- android scrollto scrollby 扯一扯
- 【Android】【滑屏】scrollTo,scrollBy
- Android滑动--scrollTo()、scrollBy()
- ScrollTo 和ScrollBy学习
- 第五周-项目4-指向学生类的指针
- C#类型和变量——两种类型
- 老程序员的下场
- Opencv-图像形态学处理比较(膨胀、腐蚀、开闭)
- 一步步学习微软InfoPath2010和SP2010--第三章节--表单设计基础:处理InfoPath布局、控件和视图(4)--控件属性功能区
- Android中的scrollBy和scrollTo
- 在netFPGA1G上实现openflow switch
- ORA-01019 on Windows 7,SP2-1503,SP2-0152
- php isset()
- Android开发之如何保证Service不被杀掉(broadcast+system/app)
- 批处理 Bat文件编写
- wireshark---- Lua: Error during loading: [string "/usr/share/wireshark/init.lua"]:45问题解决
- oracle创建用户
- 题目1084:整数拆分