今天是实现了一个小功能的东西。看看效果图:
实现方式:
1.自定义ScrollView 复写onScrollChange方法,来计算滑动的位置。
2.自定义接口,通过接口来在ScrollView中控制,滑动的高度的进度。
3.在自定义View中去执行动画。
代码实现:
1.ScrollView 最主要的代码只有计算滑动位置的代码了,其实也是很简单的,获取子View的个数,每次都去for循环,去计算字View的位置,以及当前ScrollView的top bottom
代码:
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
int currentBottom = t + height ;
int currentTop = t ;
Log.e("Slide", "onScrollChange") ;
<strong>for</strong> (<strong>int</strong> i = 0; i < childCount; i++) {
View childView = contentLayout.getChildAt(i ) ;
<strong>if</strong> (!(childView <strong>instanceof</strong> EasySlideInter)) {
<strong>continue</strong> ;
}
<strong>int</strong> childTop = childView.getTop() ;
<strong>int</strong> childBottom = childView.getBottom() ;
<strong>int</strong> childHeight = childView.getHeight() ;
EasySlideInter inter = (EasySlideInter) childView ;
<strong>if</strong> ( currentTop > childTop && currentTop < childBottom ) {
inter.contentSlide(countProgress(currentTop, childBottom, childHeight));
}<strong>else</strong> <strong>if</strong> (currentBottom > childTop && currentBottom < childBottom ) {
inter.contentSlide(100 - countProgress(currentBottom, childBottom, childHeight));
}<strong>else</strong> <strong>if</strong>(childTop >= currentTop && childBottom <= currentBottom){
inter.resetContent();
}
}
}
通过childView的top位置与ScrollView的当前的top位置来判断是哪个子View正在慢慢的出现,计算出progress 传递给子View中去。
其实最终要的代码就是这么一段,动画的执行都在子View的接口方法中去做的。
我贴上一个子View的实现:
@Override
<strong>public</strong> <strong>void</strong> contentSlide(<strong>int</strong> progress) {
textAnimator.setCurrentPlayTime(progress);
backAnimator.setCurrentPlayTime(progress);
}
@Override
<strong>public</strong> <strong>void</strong> resetContent() {
textAnimator.setCurrentPlayTime(100);
backAnimator.setCurrentPlayTime(100);
}
<strong>private</strong> <strong>void</strong> initAnimation(){
textAnimator = ObjectAnimator.ofInt(text, "textColor", Color.BLUE , Color.RED);
textAnimator.setEvaluator(<strong>new</strong> ArgbEvaluator());
textAnimator.setDuration(100) ;
textAnimator.setInterpolator(<strong>new</strong> LinearInterpolator()) ;
backAnimator = ObjectAnimator.ofInt(<strong>this</strong>, "backgroundColor", Color.BLACK , Color.BLUE , Color.BLACK);
backAnimator.setEvaluator(<strong>new</strong> ArgbEvaluator());
backAnimator.setDuration(100) ;
backAnimator.setInterpolator(<strong>new</strong> LinearInterpolator()) ;
} <br style="margin:0px; padding:0px">
实现两个接口的方法,在这两个方法中,去控制动画的进度。
很简单的,不再累赘叙述了。
源码下载:
百度网盘: http://pan.baidu.com/s/1dDtVzSt
github地址 : https://github.com/flyme2012/EasySlide
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>