先看效果。java
ScrollAnimationSherlock是一个用来打造上述引导界面动画效果的Scroll框架,集成进github.com/Jerey-Jobs/…中,做为首次启动的欢迎界面。android
工程源码:github.com/Jerey-Jobs/…git
目前支持:github
如何使用app
如何使用框架
project's build.gradle (工程下的 build.gradle)maven
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}复制代码
module's build.gradle (模块的build.gradle)ide
dependencies {
compile 'com.github.Jerey-Jobs:ScrollAnimationSherlock:1.0'
}复制代码
顶层布局:cn.jerey.animationlib.SherlockScrollView,内嵌一个SherlockLinearLayout
布局
<cn.jerey.animationlib.SherlockScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<cn.jerey.animationlib.SherlockLinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical">
<include layout="@layout/splash_layout"></include>
</cn.jerey.animationlib.SherlockLinearLayout>
</cn.jerey.animationlib.SherlockScrollView>复制代码
SherlockLinearLayout
的第一个子View会被默认设置为全屏,所以gradle
复制代码
在splash_layout
中完成第一个界面的搭建
这是上图的splash_layout
接下来就是使用SherlockLinearLayout
与SherlockRelativeLayout
进行其余界面搭建。demo中使用的是SherlockRelativeLayout
其中放置了四个子View,并设置了相应动画。
<cn.jerey.animationlib.SherlockRelativeLayout android:layout_width="match_parent" android:layout_height="300dp">
<ImageView android:id="@+id/moon" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="30dp" android:layout_marginTop="30dp" android:background="@drawable/moon" app:animation_alpha="true" app:animation_translation="left"/>
<ImageView android:id="@+id/astronaut" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="120dp" android:layout_marginTop="28dp" android:background="@drawable/astronaut" app:animation_alpha="true" app:animation_translation="right|bottom"/>
<ImageView android:id="@+id/imageView" android:layout_width="200dp" android:layout_height="180dp" android:layout_alignParentRight="true" android:layout_marginTop="100dp" android:background="@drawable/planet_earth_1" app:animation_alpha="true" app:animation_translation="right|bottom"/>
<ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentBottom="true" android:layout_marginLeft="30dp" android:background="@drawable/rocket_1" app:animation_alpha="true"/>
</cn.jerey.animationlib.SherlockRelativeLayout>复制代码
总的原理一句话:赋予每一个须要进行动画变换的View动画属性,并根据位置改变属性。
咱们须要作的事情有
ViewGroup的addView方法,该方法是添加子View的时候调用的。
public void addView(View child, int index, ViewGroup.LayoutParams params)复制代码
咱们须要在这边进行子View的判断,如何判断呢,咱们能够参照support包的设计,添加app属性,
咱们去定义几个属性
<attr name="animation_alpha" format="boolean" />//是否支持透明度动画;
<attr name="animation_scaleX" format="boolean" />//是否支持X轴缩放动画;
<attr name="animation_scaleY" format="boolean" />//是否支持Y轴缩放动画;
<attr name="bgColorStart" format="color" />//背景渐变颜色的开始颜色值;
<attr name="bgColorEnd" format="color" />//背景渐变颜色的结束颜色值,与bgColorStart成对出现;
<attr name="animation_translation">//移动动画,是一个枚举类型,支持上下左右四种值。
<flag name="left" value="0x01" />
<flag name="top" value="0x02" />
<flag name="right" value="0x04" />
<flag name="bottom" value="0x08" />
</attr>复制代码
在addView时候,经过layoutParams参数来判断,那么这里的LayoutParams是咱们自定义的,继承于系统的LayoutParams,
不过在其构造方法时追加参数解析。
/** * 不能将此LayoutParams抽象出来, 其继承的是本身内部类的Params */
public class RelativeLayoutParams extends LayoutParams {
//是否支持透明度;
public boolean mAlphaSupport;
//是否支持X Y轴缩放;
public boolean mScaleXSupport;
public boolean mScaleYSupport;
//颜色变化的起始值;
public int mBgColorStart;
public int mBgColorEnd;
//移动值;
public int mTranslationValue;
public RelativeLayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray typedArray = c.obtainStyledAttributes(attrs, R.styleable.MyFrameLayout);
mAlphaSupport = typedArray.getBoolean(R.styleable.MyFrameLayout_animation_alpha, false);
mBgColorStart = typedArray.getColor(R.styleable.MyFrameLayout_bgColorStart, -1);
mBgColorEnd = typedArray.getColor(R.styleable.MyFrameLayout_bgColorEnd, -1);
mScaleXSupport = typedArray.getBoolean(R.styleable.MyFrameLayout_animation_scaleX, false);
mScaleYSupport = typedArray.getBoolean(R.styleable.MyFrameLayout_animation_scaleY, false);
mTranslationValue = typedArray.getInt(R.styleable.MyFrameLayout_animation_translation, -1);
typedArray.recycle();
}
/** * 判断当前params是否包含自定义属性; * * @return */
public boolean isHaveMyProperty() {
if (mAlphaSupport || mScaleXSupport || mScaleYSupport || (mBgColorStart != -1 && mBgColorEnd != -1) || mTranslationValue != -1) {
return true;
}
return false;
}
}复制代码
这样咱们在addView时可以拿到这个params,而且里面已经解析了是否支持动画了。
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
RelativeLayoutParams myLayoutParams = (RelativeLayoutParams) params;
if (myLayoutParams.isHaveMyProperty())复制代码
咱们的View是不大可能本身动的,并且咱们也无法去改view的代码。这些view都是系统的view,
这样咱们只能说让view有一个父类,去操做它了。或者说。给View“伪增长”一个方法,使其接收到咱们的移动事件后,可以进行动画变换。
如何增长呢?
咱们在解析view的属性时,即addView
时,在其外面包裹一层父View,我称之为 Frame。 使用FrameView去包裹它,
固然须要注意的是,为了让view可以直接完整的进行动画显示。咱们须要设置各个父类的ClipChildren
属性为false。
所以封装了SherlockFrame
,其继承于FrameLayout,并实现咱们的位移callback接口的,注意只有实现了咱们的位移回调接口的。咱们分发事件时才会分发。
一样,这个接口提供了自定义扩展,能够本身编写实现这个接口的自定义view,一样会接收到位移分发。
public class SherlockFrame extends FrameLayout implements SherlockAnimationCallBack{
//从哪一个方向开始动画;
private static final int TRANSLATION_LEFT = 0x01;
private static final int TRANSLATION_TOP = 0x02;
private static final int TRANSLATION_RIGHT = 0x04;
private static final int TRANSLATION_BOTTOM = 0x08;
//是否支持透明度;
private boolean mAlphaSupport;
//颜色变化的起始值;
private int mBgColorStart;
private int mBgColorEnd;
//是否支持X Y轴缩放;
private boolean mScaleXSupport;
private boolean mScaleYSupport;
//移动值;
private int mTranslationValue;
//当前View宽高;
private int mHeight, mWidth;
}复制代码
SherlockFrame
的这些属性,会在addview的时候进行赋值。
SherlockAnimationCallBack
回调excuteanimation
方法时,SherlockFrame
就根据自身的属性状况进行动画变换。
做为scrollView,滚动事件的分发确定是在onScrollChanged
了。在这里面进行滚动事件分发
parseViewGroup方法有点讲究了,这是一个递归遍历子view,看其是否实现了SherlockAnimationCallBack
接口,若没有则去判断是不是ViewGroup,如果的话则继续递归遍历其子view。
如果实现了SherlockAnimationCallBack
接口的view。咱们要根据其距离顶部的高度来计算动画应该执行百分之多少。咱们能够经过view的getTop方法,这个方法是获得view距离其父view的顶部的距离。
所以咱们还要进行递归传递。
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
parseViewGroup(mLinearLayout, l, t, oldl, oldt, true, 0);
}
/** * @param linearLayout * @param l * @param t * @param oldl * @param oldt * @param isRootLinearLayout 是不是顶层布局 * @param getTop 距离顶部高度 */
private void parseViewGroup(ViewGroup linearLayout, int l, int t, int oldl, int oldt, boolean isRootLinearLayout, int getTop) {
int scrollViewHeight = getHeight();
Log.w(TAG, "linearLayout.getChildCount()" + linearLayout.getChildCount());
for (int i = 0; i < linearLayout.getChildCount(); i++) {
//若是子控件不是MyFrameLayout则循环下一个子控件;
View child = linearLayout.getChildAt(i);
// 若不是动画控件,则进入判断是不是ViewGroup,是的话递归其子view.不是的话则判断下一个
if (!(child instanceof SherlockAnimationCallBack)) {
if (child instanceof ViewGroup) {
Log.d(TAG, "parseViewGroup: 该View不是FrameLayout,是ViewGroup: " + child
.getClass().getName());
parseViewGroup((ViewGroup) child, l, t, oldl, oldt, false,
child.getTop() + getTop);
}
continue;
}
//如下为执行动画逻辑;
SherlockAnimationCallBack myCallBack = (SherlockAnimationCallBack) child;
//获取子View高度;
int childHeight = child.getHeight();
//子控件到父控件的距离;
int childTop = child.getTop();
if (!isRootLinearLayout) {
childTop += getTop;
}
//滚动过程当中,子View距离父控件顶部距离;
int childAbsluteTop = childTop - t;
//进入了屏幕
if (childAbsluteTop <= scrollViewHeight) {
//当前子控件显示出来的高度;
int childShowHeight = scrollViewHeight - childAbsluteTop - 100 ;
float moveRadio = childShowHeight / (float) childHeight;//这里必定要转化成float类型;
//执行动画;
myCallBack.excuteanimation(getMiddleValue(moveRadio, 0, 1));
} else {
//没在屏幕内,恢复数据;
myCallBack.resetViewanimation();
}
}
}复制代码
有了事件的分发了。咱们只须要在excuteanimation
的回调中实现咱们的动画便可了。默认的SherlockFrame已经实现了一些了。若要强大的自定义动画效果,实现这个接口便可。
public interface SherlockAnimationCallBack {
/** * 执行自定义动画方法; */
void excuteanimation(float moveRadio);
/** * 恢复初始状态; */
void resetViewanimation();
}复制代码
有了上面的几步,咱们的动画已经能正常跑起来了,不过因为从最下面一开始就执行动画,有点感受过快了。所以在分发位置移动事件的时候,在计算当前子控件显示出来的高度时减小了100, 这样动画就能延迟,看起来更加天然
int childShowHeight = scrollViewHeight - childAbsluteTop - 100 ;复制代码
还有不少不完善的地方,你们多多指教。
欢迎star github.com/Jerey-Jobs/…
本文做者:Anderson/Jerey_Jobs
博客地址 : jerey.cn/
简书地址 : Anderson大码渣
github地址 : github.com/Jerey-Jobs