ScrollTo,ScrollBy,offsetLeftAndRight,offsetTopAndBottom以及侧滑的简单实现

来源:互联网 发布:蓝胖升级数据 编辑:程序博客网 时间:2024/05/22 17:34

ScrollTo,ScrollBy


一、滚动的是什么

[java] view plain copy
  1. scrollgoback.setOnClickListener(new OnClickListener() {  
  2.               
  3.     @Override  
  4.     public void onClick(View v) {  
  5.         text.scrollBy(2, -10);  
  6. //      text.scrollTo(20, -50);  
  7.         textInfo.setText("ScrollX = "+text.getScrollX()+"\n"+"ScrollY = "+text.getScrollY());  
  8.     }  
  9.    });  
  10.           
  11. scroll.setOnClickListener(new OnClickListener() {  
  12.               
  13.     @Override  
  14.     public void onClick(View v) {  
  15.     text.scrollBy(-210);  
  16. //  text.scrollTo(-20, 50);  
  17.     textInfo.setText("ScrollX = "+text.getScrollX()+"\n"+"ScrollY = "+text.getScrollY());  
  18.     }  
  19.    });  



首先明白一点调用View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是把某个View的位置进行改变。如果想改变莫个View在屏幕中的位置,可以先getparent,得到父view,再对父view调用scrollto或scrollby


scrollto是移动绝对位置,scrollby是相对现在的位置移动多少


二、移动的方向


调用这两个方法会导致视图重绘.
即调用public void invalidate(int l, int t, int r, int b)方法.
此处的l,t,r,b四个参数就表示View原来的坐标.
在该方法中最终会调用:
tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
p.invalidateChild(this, tmpr);
其中tmpr是ViewParent,tmpr是Rect,this是原来的View.
通过这两行代码就把View在一个Rect中重绘.
请注意第一行代码:
原来的l和r均减去了scrollX
原来的t和b均减去了scrollY
就是说scrollX如果是正值,那么重绘后的View的宽度反而减少了;反之同理
就是说scrollY如果是正值,那么重绘后的View的高度反而减少了;反之同理
所以,TextView调用scrollTo(0,25)和我们的理解相反


二、简单的侧滑:

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/rlall"    tools:context="com.qf.zhouyi.test.MainActivity">    <RelativeLayout        android:id="@+id/rlcenter"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/colorAccent">        <Button            android:onClick="onShowLeft"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="左侧"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:text="右侧"/>    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/colorPrimary"        android:layout_toLeftOf="@id/rlcenter"/>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#000000"        android:layout_toRightOf="@id/rlcenter"/></RelativeLayout>
package com.qf.zhouyi.test;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.RelativeLayout;public class MainActivity extends AppCompatActivity {    RelativeLayout mview;    boolean bShowLeft;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mview = (RelativeLayout) findViewById(R.id.rlall);        bShowLeft = false;    }    public void onShowLeft(View view) {        if (!bShowLeft){            mview.scrollBy(-200,0);            bShowLeft = true;        }else {            mview.scrollBy(200,0);            bShowLeft =false;        }    }}

offsetLeftAndRight,offsetTopAndBottom


这两个方法也是view的方法,他们移动的不是内容,而是view自己的位置,方向也不一样,他们是右下为正,左上为负




0 0
原创粉丝点击