Android开发艺术View滑动

来源:互联网 发布:声音变换软件 编辑:程序博客网 时间:2024/06/07 04:39

滑动在Android开发中具有重要作用,不管一些滑动效果有多秀,终究都是由不同的滑动外加一些特效所组成的。通过常见的三种方式可以实现View的滑动:

  1. 通过View本身提供的scrollTo/scrollBy方法。
  2. 通过动画给View施加平移效果来实现滑动。
  3. 通过改变View的LayoutParams使得View重新布局来实现滑动。

1、使用scrollTo/scrollBy

为了实现View的滑动,View专门提供了scrollTo和scrollBy,先看看源码实现,如下:

    /**     * 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 the x position to scroll to     * @param y the y position to scroll to     */    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();            }        }    }
    /**     * 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 the amount of pixels to scroll by horizontally     * @param y the amount of pixels to scroll by vertically     */    public void scrollBy(int x, int y) {        scrollTo(mScrollX + x, mScrollY + y);    }

从源码中可以知道,scrollBy方法里调用的scrollTo方法,实现了基于当前位置的相对滑动,而scrollTo实现了基于所参数传递的绝对滑动。通过那两个方法来实现滑动,要明白滑动过程中View内部的两个属性mScrollX,mScrollY的改变规则,这两个属性可以通过getScrollX和getScrollY方法分别得到。

在滑动过程中,mScrollX的值总是等于View左边缘和View内容左边缘在水平方向上的距离,而mScrollY的值总是等于View上边缘和View内容上边缘在垂直方向上的距离。View边缘指View的位置,由四个顶点组成,View内容边缘指View中的内容的边缘。scrollTo和scrollBy只能改变View内容的位置,不能改变View在布局中的位置。scrollTo和scrollBy的单位为像素,并且当View左边缘在View内容左边缘的右边时,mScrollX为正值,反之为负值。当View上边缘在View内容上边缘的下边时,mScrollY为正值,反之为负值。也就是,如果从左向右滑动,mScrollX为负值,如果从上往下滑动,mScrollY为负值。举个例子来理解,下图所示,在图中假设水平和竖直方向的滑动距离为100像,针对各种情况给出mScrollX,mScrollY对应的值。

注意:使用scrollTo和scrollBy来实现View的滑动,只能将View的内容进行移动,不能将View本身进行移动。

变换规律图

                   图1 mScrollX和mScrollY的变换规律示意

2、使用动画

使用动画来实现View滑动,主要是操作View的translationX和translationY属性,可以使用传统动画,也可以使用属性动画。采用View动画的代码,如下所示。此动画可以在100ms内将一个View从原始位置向右下角移动100个像素。

<?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:toXDelta="100"        android:toYDelta="100"        android:interpolator="@android:anmin/linear_interpolator"/></set>

如果采用属性动画,那么一下代码可以将View在100ms内从原始位置向右平移100像素。

ObjectAnimator.ofFloat(targetView, "translationX", 0, 100)    .setDuration(100)    .start();

使用动画来做View的滑动需要注意的是View动画是对View的影像做操作,它并不能真正改变View的位置参数,包括宽和高,并且如果希望View动画后的状态保留还必须将fillAfter属性设置为true,否则动画完成后动画结果会消失。

3、改变布局参数

比如想把一个Button向右移动100px,我们只需要将这个Button的LayoutParams里的marginLeft参数的值增加100px。还有一种情形,为了达到移动Button的目的,可以在Button的左边放置一个空的View,默认宽度为0,当需要向右移动Button时,只需要重新设置空View的宽度即可。如何重新设置一个View的LayoutParams,示例如下:

MarginLayoutParams params =         (MarginLayoutParams)mButton.getLayoutParams();params.width += 100;params.leftMargin += 100;mButton.requestLayout();// 或者 mButton.setLayoutParams(params); 

4、总结

  • scrollTo/scrollBy:操作简单,是专门对View的滑动,它可以比较方便地实现滑动效果并且不影响内部元素的单击事件。适合对View内容的滑动,不能滑动View本身。
  • 动画:操作简单,主要适用于没有交互的View和实现复杂的动画效果。不能改变View本身属性。
  • 改变布局参数:操作稍微复杂,适用于有交互的View。
原创粉丝点击