diff --git a/go/quickstep/res/drawable/empty_content_box.xml b/go/quickstep/res/drawable/empty_content_box.xml new file mode 100644 index 0000000000..a4883889fe --- /dev/null +++ b/go/quickstep/res/drawable/empty_content_box.xml @@ -0,0 +1,23 @@ + + + + + + + \ No newline at end of file diff --git a/go/quickstep/src/com/android/quickstep/views/TaskLayerDrawable.java b/go/quickstep/src/com/android/quickstep/views/TaskLayerDrawable.java new file mode 100644 index 0000000000..3a230482b7 --- /dev/null +++ b/go/quickstep/src/com/android/quickstep/views/TaskLayerDrawable.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.quickstep.views; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.LayerDrawable; + +import androidx.annotation.NonNull; + +import com.android.launcher3.R; + +/** + * A layer drawable for task content that transitions between two drawables by crossfading. Similar + * to {@link android.graphics.drawable.TransitionDrawable} but allows callers to control transition + * progress and provides a default, empty drawable. + */ +public final class TaskLayerDrawable extends LayerDrawable { + private final Drawable mEmptyDrawable; + + public TaskLayerDrawable(Context context) { + super(new Drawable[0]); + + // Use empty drawable for both layers initially. + mEmptyDrawable = context.getResources().getDrawable( + R.drawable.empty_content_box, context.getTheme()); + addLayer(mEmptyDrawable); + addLayer(mEmptyDrawable); + setTransitionProgress(1.0f); + } + + /** + * Immediately set the front-most drawable layer. + * + * @param drawable drawable to set + */ + public void setCurrentDrawable(@NonNull Drawable drawable) { + setDrawable(0, drawable); + } + + /** + * Immediately reset the drawable to showing the empty drawable. + */ + public void resetDrawable() { + setCurrentDrawable(mEmptyDrawable); + } + + /** + * Prepare to start animating the transition by pushing the current drawable to the back and + * setting a new drawable to the front layer and making it invisible. + * + * @param endDrawable drawable to animate to + */ + public void startNewTransition(@NonNull Drawable endDrawable) { + Drawable oldDrawable = getDrawable(0); + setDrawable(1, oldDrawable); + setDrawable(0, endDrawable); + setTransitionProgress(0.0f); + } + + /** + * Set the progress of the transition animation to crossfade the two drawables. + * + * @param progress current transition progress between 0 (front view invisible) and 1 + * (front view visible) + */ + public void setTransitionProgress(float progress) { + if (progress > 1 || progress < 0) { + throw new IllegalArgumentException("Transition progress should be between 0 and 1"); + } + int drawableAlpha = (int) (progress * 255); + getDrawable(0).setAlpha(drawableAlpha); + getDrawable(1).setAlpha(255 - drawableAlpha); + invalidateSelf(); + } +}