Home | History | Annotate | Download | only in graphics
      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 
     17 package com.android.launcher3.graphics;
     18 
     19 import android.graphics.Canvas;
     20 import android.util.Property;
     21 import android.view.View;
     22 import android.view.ViewParent;
     23 
     24 import com.android.launcher3.R;
     25 
     26 /**
     27  * A utility class that can be used to draw a scrim behind a view
     28  */
     29 public abstract class ViewScrim<T extends View> {
     30 
     31     public static Property<ViewScrim, Float> PROGRESS =
     32             new Property<ViewScrim, Float>(Float.TYPE, "progress") {
     33                 @Override
     34                 public Float get(ViewScrim viewScrim) {
     35                     return viewScrim.mProgress;
     36                 }
     37 
     38                 @Override
     39                 public void set(ViewScrim object, Float value) {
     40                     object.setProgress(value);
     41                 }
     42             };
     43 
     44     protected final T mView;
     45     protected float mProgress = 0;
     46 
     47     public ViewScrim(T view) {
     48         mView = view;
     49     }
     50 
     51     public void attach() {
     52         mView.setTag(R.id.view_scrim, this);
     53     }
     54 
     55     public void setProgress(float progress) {
     56         if (mProgress != progress) {
     57             mProgress = progress;
     58             onProgressChanged();
     59             invalidate();
     60         }
     61     }
     62 
     63     public abstract void draw(Canvas canvas, int width, int height);
     64 
     65     protected void onProgressChanged() { }
     66 
     67     public void invalidate() {
     68         ViewParent parent = mView.getParent();
     69         if (parent != null) {
     70             ((View) parent).invalidate();
     71         }
     72     }
     73 
     74     public static ViewScrim get(View view) {
     75         return (ViewScrim) view.getTag(R.id.view_scrim);
     76     }
     77 }
     78