Jun_27.md

今日任务

  • BackgroundFadeController 分析

Que 0x01 mUpdateColors

private final OnGetTintInfo mUpdateColors = new OnGetTintInfo() {
        public void onGetTintInfo(int listType, long albumId, TintInfo tintInfo) {
            BackgroundFadeController.this.mEdgeLightningColors = new int[]{tintInfo.gradientColorA, tintInfo.gradientColorB};
            BackgroundFadeController.this.updateEdgeAnimation(BackgroundFadeController.this.mIsPlaying);
        }
    };
  • private 私有
  • final 不可继承
  • 创建了一个这样的 OnGetTintInfo,名为 mUpdateColors,并且通过构造函数实现了如下函数 onGetTintInfo
  • 其作用是为 mEdgeLightningColors 分配空间,并且调用 updateEdgeAnimation,根据分析,updateEdgeAnimation 用于更新 edge 动画(根据目前的情况来看, 应该是曲面屏边缘那个光圈)

Que 0x02 mUpdateColorsAfterRotation

private final Runnable mUpdateColorsAfterRotation = new Runnable() {
        public void run() {
            if (BackgroundFadeController.sActivityCount == 0) {
                BackgroundFadeController.this.stopEdgeLightning();
            }
        }
    };
  • Runnable 用于多线程
  • 通过构造函数实现了方法 run,其功能是调用 stopEdgeLightning 停止闪光

Que 0x03 static {}

static {
        sAppEdgeEffectInfo.setStrokeAlpha(0.8f);
        sAppEdgeEffectInfo.setRotateDuration(3000);
    }
  • static{} 语句只会在类创建的时候调用,并且仅调用一次,一般用于初始化操作
  • 这里初始化的应该是颜色深度以及边缘闪光的循环时间

Que 0x04 BackgroundFadeController

public BackgroundFadeController(Activity activity, com.samsung.android.app.music.common.mediainfo.observer.MediaChangeObservable mediaChangeObservable, MediaChangeObservable coreMediaChangeObservable) {
        this.mActivity = activity;
        this.mAlphaMask = (ImageView) activity.findViewById(R.id.blur_alpha_mask);
        if (this.mAlphaMask != null) {
            this.mNonPlayingColor = ContextCompat.getColor(activity, R.color.blur_alpha_mask);
            this.mPlayingColor = ContextCompat.getColor(activity, R.color.blur_alpha_mask_playing);
            this.mMediaChangeObservable = mediaChangeObservable;
            this.mCoreMediaChangeObservable = coreMediaChangeObservable;
            mediaChangeObservable.registerMediaChangeObserver(this);
        } else {
            this.mNonPlayingColor = 0;
            this.mPlayingColor = 0;
            this.mMediaChangeObservable = null;
            this.mCoreMediaChangeObservable = null;
        }
        this.mIsFirstMeta = true;
        sAppEdgeEffectInfo.setStrokeWidth(activity.getResources().getDimension(R.dimen.edge_lightning_stroke_width));
    }
  • 本类唯一构造函数,除了前面的初始化操作,最终调用 setStrokeWidth 设置宽度

Que 0x05 isLockScreen

private boolean isLockScreen() {
        return this.mActivity instanceof LockScreenActivity;
    }
  • 判断 mActivity 是否是 LockScreenActivity 的实例

Que 0x06 onMetaChanged

public void onMetaChanged(Meta m, PlayState s) {
        iLog.d(LOG_TAG, "onMetaChanged() called with: mIsFirstMeta = [" + this.mIsFirstMeta + "]");
        if (this.mIsFirstMeta) {
            this.mIsFirstMeta = false;
            updateFadeState(s.isPlaying, false);
        }
        this.mIsPlaying = s.isPlaying;
        updateColors(m.listType, m.albumId);
    }
  • 在构造方法中可见 mIsFirstMeta 会被设置成 true,所以会进入 if 分支
  • 在 if 分支中更新 fade 状态
  • 随后更新 mIsPlaying 状态值,并且更新色彩方案

Que 0x07 onPlayStateChanged

public void onPlayStateChanged(PlayState s) {
        iLog.d(LOG_TAG, "onPlayStateChanged() called with: s.isPlaying = [" + s.isPlaying + "]");
        if (this.mIsPlaying != s.isPlaying) {
            this.mIsPlaying = s.isPlaying;
            updateFadeState(s.isPlaying, true);
            if (s.isPlaying) {
                Meta currentMeta = this.mMediaChangeObservable.getCurrentMeta();
                updateColors(currentMeta.listType, currentMeta.albumId);
                return;
            }
            stopEdgeLightning();
        }
    }
  • 当播放状态发生改变的时候
  • 如果当前播放状态和即将改变的状态一致,则不做操作
  • 否则:
    • 更新当前播放状态
    • 更新 fade 状态
    • 如果即将改变的状态是"播放",则更新色彩方案
    • 如果即将改变的状态是"停止",则停止边缘闪光

Que 0x08 updateFadeState

private void updateFadeState(boolean isPlaying, boolean animation) {
        int startColor;
        int endColor;
        ColorDrawable color;
        iLog.d(LOG_TAG, "updateFadeState() called with: isPlaying = [" + isPlaying + "], animation = [" + animation + "]");
        if (isPlaying) {
            startColor = this.mNonPlayingColor;
            endColor = this.mPlayingColor;
        } else {
            startColor = this.mPlayingColor;
            endColor = this.mNonPlayingColor;
        }
        Drawable d = this.mAlphaMask.getDrawable();
        if (d instanceof ColorDrawable) {
            color = (ColorDrawable) d.mutate();
        } else {
            color = new ColorDrawable(startColor);
            this.mAlphaMask.setImageDrawable(color);
        }
        if (animation) {
            ValueAnimator alphaAnimation = ValueAnimator.ofArgb(new int[]{startColor, endColor}).setDuration(266);
            alphaAnimation.setInterpolator(InterpolatorSet.SINE_IN_OUT_33);
            alphaAnimation.addUpdateListener(new AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    color.setColor(((Integer) animation.getAnimatedValue()).intValue());
                    BackgroundFadeController.this.mAlphaMask.invalidateDrawable(color);
                }
            });
            alphaAnimation.start();
            return;
        }
        color.setColor(endColor);
        this.mAlphaMask.invalidateDrawable(color);
    }
  • getDrawable 用于获取背景图片
  • 用于改变颜色值
  • 最终分析出 updateFadeState 主要终于更改背景颜色,图片等

Que 0x09 checkEdgeLightningStopCondition

private boolean checkEdgeLightningStopCondition(boolean isPlaying) {
        if (isPlaying) {
            if ((!isLockScreen() && UiUtils.isInMultiWindowMode(this.mActivity)) || DesktopModeManagerCompat.isDesktopMode()) {
                return true;
            }
        } else if (isLockScreen() || UiUtils.isInMultiWindowMode(this.mActivity)) {
            return true;
        }
        return false;
    }
  • 判断是否满足停止边缘闪光的条件

Que 0x0A stopEdgeLightning

private void stopEdgeLightning() {
        if (sEdgeLightingController != null) {
            sEdgeLightingController.stopApplication();
        }
        sIsEdgeAnimationPlaying = false;
    }
  • 停止边缘闪光

Que 0x0B updateEdgeAnimation

private void updateEdgeAnimation(boolean isPlaying) {
        if (sEdgeLightingController == null) {
            synchronized (sEdgeLightningLock) {
                if (sEdgeLightingController == null) {
                    sEdgeLightingController = new EdgeLightingDialog(this.mActivity.getApplicationContext());
                }
            }
        }
        if (checkEdgeLightningStopCondition(isPlaying)) {
            stopEdgeLightning();
            return;
        }
        iLog.d(LOG_TAG, "updateEdgeAnimation() called with: isPlaying = [" + isPlaying + "]");
        if (isPlaying && this.mEdgeLightningColors != null) {
            if (!sIsEdgeAnimationPlaying || !Arrays.equals(this.mEdgeLightningColors, sCurrentEdgeLightningColors)) {
                sCurrentEdgeLightningColors = this.mEdgeLightningColors;
                sAppEdgeEffectInfo.setEffectColors(this.mEdgeLightningColors);
                sEdgeLightingController.startApplication(sAppEdgeEffectInfo);
                sIsEdgeAnimationPlaying = true;
            }
        }
    }
  • 更新边缘闪光状态

Que 0x0C updateColors

private void updateColors(int listType, long albumId) {
        TintColorCache.getInstance().getColor(this.mActivity, listType, albumId, this.mUpdateColors);
    }
  • 更新目标颜色

Que 0x0D 总结

如图所示:

123123123123.png

你可能感兴趣的:(Jun_27.md)