1、shimmer-android是Facebook出品的给任意视图添加微光效果的第三方库。
2、配置
dependencies {
implementation 'com.facebook.shimmer:shimmer:0.4.0'
}
3、基本使用
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmer_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!--我们希望起微光效果的视图-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我的微信公众号:自学者" />
</com.facebook.shimmer.ShimmerFrameLayout>
4、在Java文件中开启微光动画
ShimmerFrameLayout container = (ShimmerFrameLayout) findViewById(R.id.shimmer_view_container);
//开启微光动画
container.startShimmerAnimation();
//container.startShimmer() 上面执行的开始动画可能失效,可以使用标记的这个
5、方法参数
setAutoStart():设置是否自动启动微光动画
setBaseAlpha:设置基视图的背景颜色
setDuration():从一个布局到另一个布局亮点需要时间。
setRepeatCount():设置当前动画重复的次数
setRepeatDelay():延迟多长时间重复当前动画
setRepeatMode():设置动画重复的模式
setMaskShape():设置当前高亮面具的形状
setAngle():设置当前高亮面具的角度
6、常用属性
设置shimmer动画的时间间隔 :ShimmerFrameLayout.setDuration(int); //传入一个int值来对动画的时间间隔设置,单位是毫秒
设置shimmer动画重复类型:shimmerFrameLayout.setRepeatMode(ObjectAnimator.REVERSE);
REVERSE表示:闪光从左到右,之后在从右到左这样往复;
RESTART表示:闪光每次总是从左到右。
设置shimmer闪光的倾斜角度:调用setTilt方法设置光的倾斜角度,传入参数是float类型,表示倾斜的角度,正值表示顺时针倾斜,负值表示逆时针倾斜。
1、ShimmerFrameLayout.MaskAngle.CW_0 表示从左到右的方向
2、ShimmerFrameLayout.MaskAngle.CW_90 表示从上到下的方向
3、ShimmerFrameLayout.MaskAngle.CW_180 表示从右到左的方向
4、ShimmerFrameLayout.MaskAngle.CW_270 表示从下到上的方向
设置shimmer闪光的宽度:调用setDropoff方法设置光的宽度,该值表示的是一个相对的宽度,即表示整个ShimmerFrameLayout宽度的比例。即若设置该值为0.5f则表示光的宽度是ShimmerFrameLayout的一半。
设置shimmer闪光的透明度:
1、setBaseAlpha方法允许我们设置没有光照的地方的透明度。
2、setIntensity设置光的强度,根据测试来看,应该是被光照的边缘部分的透明度。
设置shimmer闪光的形状:
1、setMaskShape方法可以允许我们设置光的形状,目前可以设置为线性
2、ShimmerFrameLayout.MaskShape.LINEAR(默认),
3、圆形或者叫辐射状ShimmerFrameLayout.MaskShape.RADIAL。
7、GitHub:
https://github.com/facebook/shimmer-android