前言
最近一直想写个app,可是不知道想些啥,今天给大家分享一下 渐变动起来的效果
效果
GIF.gif
因为gif图比较大,所有不够清晰,这里给大家讲解一下
文章最后的最终效果图
GIF.gif
image.png
最初的渐变色是这个颜色,由紫色变为蓝色,我们暂时分为这两块颜色,右边紫色会不断变浅变成粉红、橙黄、深蓝最后变回来。左边深蓝也会不断加深变成深蓝,深紫,深褐然后变回
给人的错觉就是右边移动到左边,移动的过程不断变色
其实我们仔细分析一下,他就是改变了起始端和末端的颜色
分析
上面就简单的分析了一下,现在来彻底分析一下怎么做
写出一个xml布局,然后自定义一个全屏的LinearLayout
onDraw加ValueAnimator 动态改变LinearGradient渐变色
是不是很简单?
实验一
xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
java
public class gradual extends View {
private int animatedValue;
private int colorEnd;
private int colorStart;
public gradual(Context context) {
super(context);
init();
}
public gradual(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
System.out.println(111);
init();
}
public void init() {
ValueAnimator animator=ValueAnimator.ofInt(0,255);
animator.setDuration(20000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onAnimationUpdate(ValueAnimator animation) {
animatedValue = (int) animation.getAnimatedValue();
colorStart = Color.rgb(255-animatedValue,animatedValue,255-animatedValue);
colorEnd = Color.rgb(animatedValue,animatedValue,255-animatedVal