美文网首页
初识:View【二: View滑动】

初识:View【二: View滑动】

作者: Antonylr | 来源:发表于2019-12-27 14:46 被阅读0次

视图显示了,我们得让它动起来啊!下面就介绍实现几种View滑动的方式

一. 使用View提供的api scrollTo / scrollBy 实现View的滑动

这两个方法都是View提供的方法,专门来搞滑动的,下面贴出了它们的源码,你会发现scrollBy() 最后调用的还是scrollTo()方法。

/**
 * 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 要滚动到的x位置
 * @param y 要滚动到的y位置
 */
public void scrollTo(int x, int y) {
    //mScrollX 该视图内容水平滚动时的偏移量(以像素为单位)。
    //mScrollY 该视图的内容垂直滚动时的偏移量(以像素为单位)。
    if (mScrollX != x || mScrollY != y) {
        int oldX = mScrollX;
        int oldY = mScrollY;
        mScrollX = x;mScrollX 
        mScrollY = y;
        /*用于指示此视图的父视图应清除其缓存。这个功能用于强制父类重新构建其显示列表(当硬件加速时),
当视图的各种父管理属性发生变化时,例如alpha, translationX/Y, scrollX/Y, scaleX/Y,和rotation/X/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 水平滚动的像素量
 * @param y 垂直滚动的像素量
 */
public void scrollBy(int x, int y) {
    scrollTo(mScrollX + x, mScrollY + y);
}

下面用实例代码验证一下看看效果, 我们在布局上 添加三个视图View都是TextView--- t1, t2, t3【用t1控制 t2, t3移动】

TextView t1 = findViewById(R.id.tv1);
TextView t2 = findViewById(R.id.tv2);
TextView t3 = findViewById(R.id.tv3);
t1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        t2.scrollTo(100, 0); //X轴移动像素100, Y轴不移动
        t3.scrollBy(100, 0); 
    }
});
1577428421.jpg
1577428463.jpg

发现 t2, t3的内容在Y轴方向,都向下移动了100像素。下面总结一下。
scrollTo / scrollBy 它们移动的都是View视图控件中的“内容”,大家还要注意 X, Y轴的正负值对移动方向产生的作用。【X正数--》左移, Y正数--》上移,我就不贴图了!】

二. 使用动画使动画滑动

通过动画我们能够让一个View进行平移, 而平移就是一种滑动,主要操作了View的translationX和translationY属性
translationX:此视图相对于其左侧位置的水平位置,以像素为单位。
translationY:此视图相对于其顶部位置的垂直位置,以像素为单位。
写段代码示例演示一下,在我们的res/anim/下创建一个scrol.xml动画文件。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:zAdjustment="normal">
    <translate
        android:duration="100"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXDelta="400"
        android:toYDelta="400" />
</set>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seconde);
    TextView t1 = findViewById(R.id.tv1);
    TextView t2 = findViewById(R.id.tv2);
    //获取动画.xml文件
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.scrol);
    t1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t2.clearAnimation();
            t2.startAnimation(animation);
            Log.i("Location", "滑动位置-"+"top_:"+t2.getTop()+" left_:"+t2.getLeft()+ "  bottom_:"+t2.getBottom() + "  right_:"+t2.getRight());
        }
    });
    t2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Location", "开始位置-"+"top*:"+t2.getTop()+" left*:"+t2.getLeft()+ "  bottom*:"+t2.getBottom() + "  right*:"+t2.getRight());
        }
    });
}

UI展示在下图,我们先点击t2 打印一下看看它的起始位置。 然后在点击t1让t2滑动打印一下t2的位置看看。


1577428647.jpg
1577428670.jpg
12-27 13:43:32.831 540-540/com.example.antony.cdemo I/Location: 开始位置-top*:336 left*:0  bottom*:552  right*:216
12-27 13:46:51.239 540-540/com.example.antony.cdemo I/Location: 滑动位置-top_:336 left_:0  bottom_:552  right_:216

发现 t2开始的位置, 滑动位置并没有发生改变。 点击滑动后的t2也没有任何打印。然而我点击t2原来的位置,发现出现了打印。【大家可以把代码拷贝验证一下】

12-27 13:51:35.931 5720-5720/com.example.antony.cdemo I/Location: 开始位置-top*:336 left*:0  bottom*:552  right*:216
12-27 13:51:40.837 5720-5720/com.example.antony.cdemo I/Location: 滑动位置-top_:336 left_:0  bottom_:552  right_:216
12-27 13:51:42.930 5720-5720/com.example.antony.cdemo I/Location: 开始位置-top*:336 left*:0  bottom*:552  right*:216

说明动画滑动并不能真正改变View的位置。点击滑动后的View也不会触发onClick事件。

三. 改变布局参数

通过改变View的LayoutParams里的布局参数, 来实现View的滑动。代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seconde);
    TextView t1 = findViewById(R.id.tv1);
    TextView t2 = findViewById(R.id.tv2);
    t1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) t2.getLayoutParams();
            params.leftMargin += 300;
            params.topMargin += 300;
            t2.requestLayout(); //t2.setLayoutParams(params)
            Log.i("Location", "滑动位置-" + "top_:" + t2.getTop() + " left_:" + t2.getLeft() + "  bottom_:" + t2.getBottom() + "  right_:" + t2.getRight());
        }
    });
    t2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Location", "开始位置-" + "top*:" + t2.getTop() + " left*:" + t2.getLeft() + "  bottom*:" + t2.getBottom() + "  right*:" + t2.getRight());
        }
    });
}

我们同样先点击t2打印一下, 在点击t1, 然后在点击滑动后的t2看看打印


1577428917.jpg
1577428867.jpg
12-27 14:13:54.811 9793-9793/com.example.antony.cdemo I/Location: 开始位置-top*:336 left*:0  bottom*:552  right*:216
12-27 14:13:57.403 9793-9793/com.example.antony.cdemo I/Location: 滑动位置-top_:336 left_:0  bottom_:552  right_:216
12-27 14:14:04.827 9793-9793/com.example.antony.cdemo I/Location: 开始位置-top*:636 left*:300  bottom*:852  right*:516

四. 总结

scrollTo / scrollBy: 操作简单,适用对View的内容的滑动
动画: 操作简单,主要适用于没有交互的View和实现复杂的动画效果
改变布局参数: 操作稍微复杂,适用于有交互的View

相关文章

网友评论

      本文标题:初识:View【二: View滑动】

      本文链接:https://www.haomeiwen.com/subject/joldoctx.html