您的位置:首页 > 移动开发 > Android开发

AnAndroid开发艺术探索读后感—View的滑动

2016-11-16 22:07 239 查看
View的滑动常用的有三种方式:第一种是通过View本身提供的scrollTo/scrollBy方法来实现滑动;第二种是通过动画给View施加平移效果来实现滑动;第三种是通过改变View的LayoutParams使得View重新布局从而实现滑动。

1.使用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;

invalidateParentCach
bad8
es();

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实现了基于所传递参数的绝对滑动。利用这个两个方法实现了View的滑动。另外要弄清mScrollX和mScrolly的改变规则,这两个属性可以通过getScrollX和getScrollY方法分别得到。先简要概括下:滑动过程中,mScrollX的值总是等于View左边缘和View内容左边缘在水平方向的距离,而mScrollY的值总是等于View上边缘和View内容上边缘在竖直方向的距离。scrollTo和scrollBy只能改变View内容的位置而不能改变View在布局中的位置。mScrollX和mScrollY的单位是像素,并且当View左边缘在View内容左边缘的右边时,mScrollX为正值,反之为负值;当View上边缘在View内容上边缘的下边时,mScrollY为正值,反之为负值。见下图,一目了然。



2.使用动画
使用动画来移动View,主要是操作View的translationX和translationY属性,既可以采用传统的View动画,也可以采用属性动画,如果采用属性动画的话,为了能够兼容3.0以下版本,需要采用开源动画库nineoldandroids(http://nineoldandroids.com/).
采用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:interpolator="android:anim/linear_interpolator"
android:toXDelta="100"
android:toYDelta="100"/>
</set>

如果采用属性动画就更简单了,以下代码可以将一个View在100ms内从原始位置向右平移100像素
ObjectAnimator.ofFloat(targetView,"translationX",1,100).setDuration(100).start();
View动画来移动View的方法只是一个影像操作,并没有改变View的真实位置,即对View进行点击等交互操作时,仍旧需要在View的原始位置进行操作,而且必须将fillAfter设置为true才能保持动画之后的位置。但是属性动画就不会,不过3.0以下无法使用属性动画,所以需要用到动画兼容库。其实在3.0以下动画兼容库的属性动画本质上还是View动画。
在3.0以下无法使用属性动画的情况下,我们可以婉转的解决问题,我们可以在新位置预先创建要给和目标View一模一样的View,他们不但外观一样连onClick事件也一样,当目标Button完成平移动画后,就把目标Button隐藏,同时把预先创建的Button显示出来,通过这种方式也可以解决上述问题。
3.改变布局参数
第三种实现View滑动的方式就是改变布局参数,即改变LayoutParams。比如我们想把一个Button向右平移100PX,我们只需要将这个Button的LayoutParams里的marginLeft参数的值增加100Px即可;还可以在Button的左边放置一个空的View,这个空View的默认宽度为0,当我们需要向右移动Button时,只需要重新设置空View的宽度即可,当空View的宽度增大时(假设Button的父容器是水平方向的LinearLayout),Button就自动被挤向右边,即实现了向右平移的效果。重新设置View的LayoutParams的代码如下:

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

各种滑动方式的对比:
scrollTo/scrollBy:操作简单,适合对View内容的滑动
动画:操作简单,主要适用于没有交互的View和实现复杂的动画效果
改变布局参数:操作稍微复杂,适用于有交互的View。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: