1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.graphics; 17 18 import android.graphics.Canvas; 19 import android.graphics.Color; 20 import android.support.v4.graphics.ColorUtils; 21 import android.view.View; 22 import android.view.animation.Interpolator; 23 24 import com.android.launcher3.R; 25 import com.android.launcher3.anim.Interpolators; 26 import com.android.launcher3.uioverrides.WallpaperColorInfo; 27 28 /** 29 * Simple scrim which draws a color 30 */ 31 public class ColorScrim extends ViewScrim { 32 33 private final int mColor; 34 private final Interpolator mInterpolator; 35 private int mCurrentColor; 36 37 public ColorScrim(View view, int color, Interpolator interpolator) { 38 super(view); 39 mColor = color; 40 mInterpolator = interpolator; 41 } 42 43 @Override 44 protected void onProgressChanged() { 45 mCurrentColor = ColorUtils.setAlphaComponent(mColor, 46 Math.round(mInterpolator.getInterpolation(mProgress) * Color.alpha(mColor))); 47 } 48 49 @Override 50 public void draw(Canvas canvas, int width, int height) { 51 if (mProgress > 0) { 52 canvas.drawColor(mCurrentColor); 53 } 54 } 55 56 public static ColorScrim createExtractedColorScrim(View view) { 57 WallpaperColorInfo colors = WallpaperColorInfo.getInstance(view.getContext()); 58 int alpha = view.getResources().getInteger(R.integer.extracted_color_gradient_alpha); 59 ColorScrim scrim = new ColorScrim(view, ColorUtils.setAlphaComponent( 60 colors.getSecondaryColor(), alpha), Interpolators.LINEAR); 61 scrim.attach(); 62 return scrim; 63 } 64 } 65