Audinaut-subsonic-app-android/app/src/main/java/net/nullsum/audinaut/view/RecyclingImageView.java

99 lines
3.6 KiB
Java
Raw Normal View History

2016-12-18 18:41:30 +01:00
/*
This file is part of Subsonic.
2018-03-24 20:25:12 +01:00
Subsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015 (C) Scott Jackson
2016-12-18 18:41:30 +01:00
*/
2017-01-09 08:01:12 +01:00
package net.nullsum.audinaut.view;
2016-12-18 18:41:30 +01:00
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
2019-08-25 05:06:03 +02:00
import androidx.appcompat.widget.AppCompatImageView;
2017-03-05 17:23:13 +01:00
public class RecyclingImageView extends AppCompatImageView {
2018-03-24 20:25:12 +01:00
private boolean invalidated = false;
public RecyclingImageView(Context context) {
super(context);
}
public RecyclingImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RecyclingImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onDraw(Canvas canvas) {
Drawable drawable = this.getDrawable();
2018-03-25 03:28:28 +02:00
if (drawable != null) {
if (drawable instanceof BitmapDrawable) {
2018-03-24 20:25:12 +01:00
if (isBitmapRecycled(drawable)) {
this.setImageDrawable(null);
this.invalidated = true;
2018-03-24 20:25:12 +01:00
}
2018-03-25 03:28:28 +02:00
} else if (drawable instanceof TransitionDrawable) {
2018-03-24 20:25:12 +01:00
TransitionDrawable transitionDrawable = (TransitionDrawable) drawable;
// If last bitmap in chain is recycled, just blank this out since it would be invalid anyways
Drawable lastDrawable = transitionDrawable.getDrawable(transitionDrawable.getNumberOfLayers() - 1);
2018-03-25 03:28:28 +02:00
if (isBitmapRecycled(lastDrawable)) {
2018-03-24 20:25:12 +01:00
this.setImageDrawable(null);
setInvalidated(true);
} else {
// Go through earlier bitmaps and make sure that they are not recycled
for (int i = 0; i < transitionDrawable.getNumberOfLayers(); i++) {
Drawable layerDrawable = transitionDrawable.getDrawable(i);
if (isBitmapRecycled(layerDrawable)) {
// If anything in the chain is broken, just get rid of transition and go to last drawable
this.setImageDrawable(lastDrawable);
break;
}
}
}
}
}
super.onDraw(canvas);
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
setInvalidated(false);
}
private boolean isBitmapRecycled(Drawable drawable) {
2018-03-25 03:28:28 +02:00
if (!(drawable instanceof BitmapDrawable)) {
2018-03-24 20:25:12 +01:00
return false;
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2018-03-25 03:28:28 +02:00
return bitmapDrawable.getBitmap() != null && bitmapDrawable.getBitmap().isRecycled();
2018-03-24 20:25:12 +01:00
}
public boolean isInvalidated() {
return invalidated;
}
2018-03-25 03:28:28 +02:00
private void setInvalidated(boolean invalidated) {
this.invalidated = invalidated;
2018-03-24 20:25:12 +01:00
}
2016-12-18 18:41:30 +01:00
}