Android 开源代码学习——NoBoringActionBar

代码功能

该示例工程实现了自定义ActionBar,可以使一个图片平滑过渡到ActionBar Icon的位置,并以ActionBar Icon的形式展现出来。而且还实现了背景图片的自动切换,缩放和平移。可以在展示用户资料等场景使用。

clip_image001

 

关键代码学习

 

2.1 NoBoringActionBarActivity.java

 

setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));

注:这行代码是用来设置ActionBar Title 的透明度的,该方法控制Title在向上移动可移动总高度的4/5之后才开始显示。

 

public static float clamp(float value, float min, float max) {

    return Math.max(min, Math.min(value, max));

}

注:该方法限制Value的取值范围为[min,max]

 

2.2 KenBurnsView.java

 

Handler.post(Runnable r);

注:将参数Runnable的实例对象加入到Handler消息队列中

 

Handler.removeCallbacks(Runnable r);

注:将参数Runnable的实例对象从消息队列中移除


private void start(View view, long duration, float fromScale, float toScale, float fromTranslationX,

            float fromTranslationY, float toTranslationX, float toTranslationY) {

        view.setScaleX(fromScale);

        view.setScaleY(fromScale);

        view.setTranslationX(fromTranslationX);

        view.setTranslationY(fromTranslationY);

        ViewPropertyAnimator propertyAnimator = view.animate().translationX(toTranslationX)

                .translationY(toTranslationY).scaleX(toScale).scaleY(toScale).setDuration(duration);

        propertyAnimator.start();

        Log.d(TAG, "starting Ken Burns animation " + propertyAnimator);

}

注:执行该方法的效果是使背景图片随机平移,缩放。

 

系统API使用与理解

 

3.1 ViewPropertyAnimator


This class may provide better performance for several simultaneous animations, because it will optimize invalidate calls to take place only once for several properties instead of each animated property independently causing its own invalidation.
根据官方API的解释,该类在几个动画同时执行的时候使用性能比较好。

 

总结


这么一个简单的demo自己愣是用了3周时间才整理出来,太拖拉了。而且动画那块的东西现在自己还是没有吃透,也许过几天会发一篇关于安卓动画的博客吧。不啰嗦了,附件为自己简单注释过的代码,有兴趣的同学可以研究一下。

 

http://yunpan.cn/cfGZLjBZS6udk 访问密码 b0c7

 

你可能感兴趣的:(Actionbar)