Using a custom view instead of ImageView to avoid relayout when the icon changes
Bug: 78585335
Change-Id: I078aec5b80ec45933ba4974df68cab23ac4a0ca0
diff --git a/quickstep/res/layout/task.xml b/quickstep/res/layout/task.xml
index b8b360a..429f3a2 100644
--- a/quickstep/res/layout/task.xml
+++ b/quickstep/res/layout/task.xml
@@ -24,7 +24,7 @@
android:layout_height="match_parent"
android:layout_marginTop="@dimen/task_thumbnail_top_margin" />
- <ImageView
+ <com.android.quickstep.views.IconView
android:id="@+id/icon"
android:layout_width="@dimen/task_thumbnail_icon_size"
android:layout_height="@dimen/task_thumbnail_icon_size"
diff --git a/quickstep/src/com/android/quickstep/util/TaskViewDrawable.java b/quickstep/src/com/android/quickstep/util/TaskViewDrawable.java
index 8549df3..34f580b 100644
--- a/quickstep/src/com/android/quickstep/util/TaskViewDrawable.java
+++ b/quickstep/src/com/android/quickstep/util/TaskViewDrawable.java
@@ -21,7 +21,7 @@
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.FloatProperty;
-import android.widget.ImageView;
+import android.view.View;
import com.android.launcher3.Utilities;
import com.android.quickstep.views.RecentsView;
@@ -47,7 +47,7 @@
(t) -> (Math.max(t, 0.3f) - 0.3f) / 0.7f;
private final RecentsView mParent;
- private final ImageView mIconView;
+ private final View mIconView;
private final int[] mIconPos;
private final TaskThumbnailView mThumbnailView;
diff --git a/quickstep/src/com/android/quickstep/views/IconView.java b/quickstep/src/com/android/quickstep/views/IconView.java
new file mode 100644
index 0000000..c359966
--- /dev/null
+++ b/quickstep/src/com/android/quickstep/views/IconView.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2018 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.Canvas;
+import android.graphics.drawable.Drawable;
+import android.util.AttributeSet;
+import android.view.View;
+
+/**
+ * A view which draws a drawable stretched to fit its size. Unlike ImageView, it avoids relayout
+ * when the drawable changes.
+ */
+public class IconView extends View {
+
+ private Drawable mDrawable;
+
+ public IconView(Context context) {
+ super(context);
+ }
+
+ public IconView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public IconView(Context context, AttributeSet attrs, int defStyleAttr) {
+ super(context, attrs, defStyleAttr);
+ }
+
+ public void setDrawable(Drawable d) {
+ if (mDrawable != null) {
+ mDrawable.setCallback(null);
+ }
+ mDrawable = d;
+ if (mDrawable != null) {
+ mDrawable.setCallback(this);
+ mDrawable.setBounds(0, 0, getWidth(), getHeight());
+ }
+ invalidate();
+ }
+
+ @Override
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+ super.onSizeChanged(w, h, oldw, oldh);
+ if (mDrawable != null) {
+ mDrawable.setBounds(0, 0, w, h);
+ }
+ }
+
+ @Override
+ protected boolean verifyDrawable(Drawable who) {
+ return super.verifyDrawable(who) || who == mDrawable;
+ }
+
+ @Override
+ protected void drawableStateChanged() {
+ super.drawableStateChanged();
+
+ final Drawable drawable = mDrawable;
+ if (drawable != null && drawable.isStateful()
+ && drawable.setState(getDrawableState())) {
+ invalidateDrawable(drawable);
+ }
+ }
+
+ @Override
+ protected void onDraw(Canvas canvas) {
+ if (mDrawable != null) {
+ mDrawable.draw(canvas);
+ }
+ }
+
+ @Override
+ public boolean hasOverlappingRendering() {
+ return false;
+ }
+}
diff --git a/quickstep/src/com/android/quickstep/views/TaskView.java b/quickstep/src/com/android/quickstep/views/TaskView.java
index a7527a6..5fffb50 100644
--- a/quickstep/src/com/android/quickstep/views/TaskView.java
+++ b/quickstep/src/com/android/quickstep/views/TaskView.java
@@ -80,7 +80,7 @@
private Task mTask;
private TaskThumbnailView mSnapshotView;
- private ImageView mIconView;
+ private IconView mIconView;
private float mCurveScale;
private float mCurveDimAlpha;
private Animator mDimAlphaAnim;
@@ -133,7 +133,7 @@
return mSnapshotView;
}
- public ImageView getIconView() {
+ public IconView getIconView() {
return mIconView;
}
@@ -163,7 +163,7 @@
@Override
public void onTaskDataLoaded(Task task, ThumbnailData thumbnailData) {
mSnapshotView.setThumbnail(task, thumbnailData);
- mIconView.setImageDrawable(task.icon);
+ mIconView.setDrawable(task.icon);
mIconView.setOnClickListener(icon -> TaskMenuView.showForTask(this));
mIconView.setOnLongClickListener(icon -> {
requestDisallowInterceptTouchEvent(true);
@@ -174,7 +174,7 @@
@Override
public void onTaskDataUnloaded() {
mSnapshotView.setThumbnail(null, null);
- mIconView.setImageDrawable(null);
+ mIconView.setDrawable(null);
mIconView.setOnLongClickListener(null);
}