2014-02-04 19:36:55 +01:00
|
|
|
/*
|
|
|
|
* This is the source code of Telegram for Android v. 1.3.x.
|
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
2016-01-11 18:19:48 +01:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2016.
|
2014-02-04 19:36:55 +01:00
|
|
|
*/
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
package org.telegram.messenger;
|
2014-02-04 19:36:55 +01:00
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
2014-10-21 22:35:16 +02:00
|
|
|
import android.graphics.BitmapShader;
|
2014-02-04 19:36:55 +01:00
|
|
|
import android.graphics.Canvas;
|
2015-05-03 13:48:36 +02:00
|
|
|
import android.graphics.ColorFilter;
|
2014-10-21 22:35:16 +02:00
|
|
|
import android.graphics.Matrix;
|
|
|
|
import android.graphics.Paint;
|
2015-01-02 23:15:07 +01:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffColorFilter;
|
2014-06-07 01:35:21 +02:00
|
|
|
import android.graphics.Rect;
|
2014-10-21 22:35:16 +02:00
|
|
|
import android.graphics.RectF;
|
|
|
|
import android.graphics.Shader;
|
2014-02-04 19:36:55 +01:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.view.View;
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.tgnet.TLObject;
|
|
|
|
import org.telegram.tgnet.TLRPC;
|
2016-01-11 18:19:48 +01:00
|
|
|
import org.telegram.ui.Components.AnimatedFileDrawable;
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
public class ImageReceiver implements NotificationCenter.NotificationCenterDelegate {
|
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
public interface ImageReceiverDelegate {
|
|
|
|
void didSetImage(ImageReceiver imageReceiver, boolean set, boolean thumb);
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2015-05-03 13:48:36 +02:00
|
|
|
private class SetImageBackup {
|
|
|
|
public TLObject fileLocation;
|
|
|
|
public String httpUrl;
|
|
|
|
public String filter;
|
|
|
|
public Drawable thumb;
|
|
|
|
public TLRPC.FileLocation thumbLocation;
|
|
|
|
public String thumbFilter;
|
|
|
|
public int size;
|
|
|
|
public boolean cacheOnly;
|
2015-05-21 23:27:27 +02:00
|
|
|
public String ext;
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
private View parentView;
|
|
|
|
private Integer tag;
|
|
|
|
private Integer thumbTag;
|
|
|
|
private MessageObject parentMessageObject;
|
|
|
|
private boolean canceledLoading;
|
2016-04-22 15:49:00 +02:00
|
|
|
private static PorterDuffColorFilter selectedColorFilter = new PorterDuffColorFilter(0xffdddddd, PorterDuff.Mode.MULTIPLY);
|
2015-02-01 19:51:02 +01:00
|
|
|
|
2015-05-03 13:48:36 +02:00
|
|
|
private SetImageBackup setImageBackup;
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
private TLObject currentImageLocation;
|
|
|
|
private String currentKey;
|
|
|
|
private String currentThumbKey;
|
|
|
|
private String currentHttpUrl;
|
|
|
|
private String currentFilter;
|
|
|
|
private String currentThumbFilter;
|
2015-05-21 23:27:27 +02:00
|
|
|
private String currentExt;
|
2015-02-01 19:51:02 +01:00
|
|
|
private TLRPC.FileLocation currentThumbLocation;
|
|
|
|
private int currentSize;
|
|
|
|
private boolean currentCacheOnly;
|
2016-01-11 18:19:48 +01:00
|
|
|
private Drawable currentImage;
|
|
|
|
private Drawable currentThumb;
|
2015-02-01 19:51:02 +01:00
|
|
|
private Drawable staticThumb;
|
2016-01-11 18:19:48 +01:00
|
|
|
private boolean allowStartAnimation = true;
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
private boolean needsQualityThumb;
|
|
|
|
private boolean shouldGenerateQualityThumb;
|
2015-05-21 23:27:27 +02:00
|
|
|
private boolean invalidateAll;
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
private int imageX, imageY, imageW, imageH;
|
2014-08-22 16:24:33 +02:00
|
|
|
private Rect drawRegion = new Rect();
|
2014-06-07 01:35:21 +02:00
|
|
|
private boolean isVisible = true;
|
2015-02-01 19:51:02 +01:00
|
|
|
private boolean isAspectFit;
|
|
|
|
private boolean forcePreview;
|
|
|
|
private int roundRadius;
|
|
|
|
private BitmapShader bitmapShader;
|
2016-04-22 15:49:00 +02:00
|
|
|
private BitmapShader bitmapShaderThumb;
|
|
|
|
private static Paint roundPaint;
|
|
|
|
private RectF roundRect = new RectF();
|
|
|
|
private RectF bitmapRect = new RectF();
|
|
|
|
private Matrix shaderMatrix = new Matrix();
|
2015-05-03 13:48:36 +02:00
|
|
|
private float overrideAlpha = 1.0f;
|
2015-01-02 23:15:07 +01:00
|
|
|
private boolean isPressed;
|
2015-02-26 02:32:51 +01:00
|
|
|
private int orientation;
|
|
|
|
private boolean centerRotation;
|
2015-02-01 19:51:02 +01:00
|
|
|
private ImageReceiverDelegate delegate;
|
2015-05-03 13:48:36 +02:00
|
|
|
private float currentAlpha;
|
|
|
|
private long lastUpdateAlphaTime;
|
|
|
|
private byte crossfadeAlpha = 1;
|
|
|
|
private boolean crossfadeWithThumb;
|
|
|
|
private ColorFilter colorFilter;
|
2014-08-22 16:24:33 +02:00
|
|
|
|
|
|
|
public ImageReceiver() {
|
2016-04-22 15:49:00 +02:00
|
|
|
this(null);
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ImageReceiver(View view) {
|
|
|
|
parentView = view;
|
2016-04-22 15:49:00 +02:00
|
|
|
if (roundPaint == null) {
|
|
|
|
roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
public void cancelLoadImage() {
|
|
|
|
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
|
|
|
|
canceledLoading = true;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public void setImage(TLObject path, String filter, Drawable thumb, String ext, boolean cacheOnly) {
|
|
|
|
setImage(path, null, filter, thumb, null, null, 0, ext, cacheOnly);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public void setImage(TLObject path, String filter, Drawable thumb, int size, String ext, boolean cacheOnly) {
|
|
|
|
setImage(path, null, filter, thumb, null, null, size, ext, cacheOnly);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public void setImage(String httpUrl, String filter, Drawable thumb, String ext, int size) {
|
|
|
|
setImage(null, httpUrl, filter, thumb, null, null, size, ext, true);
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public void setImage(TLObject fileLocation, String filter, TLRPC.FileLocation thumbLocation, String thumbFilter, String ext, boolean cacheOnly) {
|
|
|
|
setImage(fileLocation, null, filter, null, thumbLocation, thumbFilter, 0, ext, cacheOnly);
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public void setImage(TLObject fileLocation, String filter, TLRPC.FileLocation thumbLocation, String thumbFilter, int size, String ext, boolean cacheOnly) {
|
|
|
|
setImage(fileLocation, null, filter, null, thumbLocation, thumbFilter, size, ext, cacheOnly);
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public void setImage(TLObject fileLocation, String httpUrl, String filter, Drawable thumb, TLRPC.FileLocation thumbLocation, String thumbFilter, int size, String ext, boolean cacheOnly) {
|
2015-05-03 13:48:36 +02:00
|
|
|
if (setImageBackup != null) {
|
|
|
|
setImageBackup.fileLocation = null;
|
|
|
|
setImageBackup.httpUrl = null;
|
|
|
|
setImageBackup.thumbLocation = null;
|
|
|
|
setImageBackup.thumb = null;
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
if ((fileLocation == null && httpUrl == null && thumbLocation == null)
|
|
|
|
|| (fileLocation != null && !(fileLocation instanceof TLRPC.TL_fileLocation)
|
|
|
|
&& !(fileLocation instanceof TLRPC.TL_fileEncryptedLocation)
|
2016-03-06 02:49:31 +01:00
|
|
|
&& !(fileLocation instanceof TLRPC.TL_document)
|
|
|
|
&& !(fileLocation instanceof TLRPC.TL_documentEncrypted))) {
|
2015-02-01 19:51:02 +01:00
|
|
|
recycleBitmap(null, false);
|
|
|
|
recycleBitmap(null, true);
|
|
|
|
currentKey = null;
|
2015-05-21 23:27:27 +02:00
|
|
|
currentExt = ext;
|
2015-02-01 19:51:02 +01:00
|
|
|
currentThumbKey = null;
|
|
|
|
currentThumbFilter = null;
|
|
|
|
currentImageLocation = null;
|
|
|
|
currentHttpUrl = null;
|
|
|
|
currentFilter = null;
|
|
|
|
currentCacheOnly = false;
|
|
|
|
staticThumb = thumb;
|
2015-05-03 13:48:36 +02:00
|
|
|
currentAlpha = 1;
|
2015-02-01 19:51:02 +01:00
|
|
|
currentThumbLocation = null;
|
|
|
|
currentSize = 0;
|
2014-02-04 19:36:55 +01:00
|
|
|
currentImage = null;
|
2015-02-01 19:51:02 +01:00
|
|
|
bitmapShader = null;
|
2016-04-22 15:49:00 +02:00
|
|
|
bitmapShaderThumb = null;
|
2015-02-01 19:51:02 +01:00
|
|
|
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
|
2014-07-03 00:39:05 +02:00
|
|
|
if (parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
if (delegate != null) {
|
|
|
|
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
if (!(thumbLocation instanceof TLRPC.TL_fileLocation)) {
|
|
|
|
thumbLocation = null;
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
String key = null;
|
2014-08-22 16:24:33 +02:00
|
|
|
if (fileLocation != null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
if (fileLocation instanceof TLRPC.FileLocation) {
|
|
|
|
TLRPC.FileLocation location = (TLRPC.FileLocation) fileLocation;
|
|
|
|
key = location.volume_id + "_" + location.local_id;
|
2015-09-24 22:52:02 +02:00
|
|
|
} else {
|
2015-01-02 23:15:07 +01:00
|
|
|
TLRPC.Document location = (TLRPC.Document) fileLocation;
|
2016-01-11 18:19:48 +01:00
|
|
|
if (location.dc_id != 0) {
|
|
|
|
key = location.dc_id + "_" + location.id;
|
|
|
|
} else {
|
|
|
|
fileLocation = null;
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
} else if (httpUrl != null) {
|
2014-02-04 19:36:55 +01:00
|
|
|
key = Utilities.MD5(httpUrl);
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
if (key != null) {
|
|
|
|
if (filter != null) {
|
|
|
|
key += "@" + filter;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
if (currentKey != null && key != null && currentKey.equals(key)) {
|
|
|
|
if (delegate != null) {
|
|
|
|
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
if (!canceledLoading && !forcePreview) {
|
|
|
|
return;
|
2014-10-18 01:06:51 +02:00
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
String thumbKey = null;
|
|
|
|
if (thumbLocation != null) {
|
|
|
|
thumbKey = thumbLocation.volume_id + "_" + thumbLocation.local_id;
|
|
|
|
if (thumbFilter != null) {
|
|
|
|
thumbKey += "@" + thumbFilter;
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
recycleBitmap(key, false);
|
|
|
|
recycleBitmap(thumbKey, true);
|
|
|
|
|
|
|
|
currentThumbKey = thumbKey;
|
|
|
|
currentKey = key;
|
2015-05-21 23:27:27 +02:00
|
|
|
currentExt = ext;
|
2015-02-01 19:51:02 +01:00
|
|
|
currentImageLocation = fileLocation;
|
|
|
|
currentHttpUrl = httpUrl;
|
|
|
|
currentFilter = filter;
|
|
|
|
currentThumbFilter = thumbFilter;
|
|
|
|
currentSize = size;
|
|
|
|
currentCacheOnly = cacheOnly;
|
|
|
|
currentThumbLocation = thumbLocation;
|
|
|
|
staticThumb = thumb;
|
|
|
|
bitmapShader = null;
|
2016-04-22 15:49:00 +02:00
|
|
|
bitmapShaderThumb = null;
|
2015-05-03 13:48:36 +02:00
|
|
|
currentAlpha = 1.0f;
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
if (delegate != null) {
|
|
|
|
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
|
2014-10-21 22:35:16 +02:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
ImageLoader.getInstance().loadImageForImageReceiver(this);
|
2014-08-29 23:06:04 +02:00
|
|
|
if (parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 13:48:36 +02:00
|
|
|
public void setColorFilter(ColorFilter filter) {
|
|
|
|
colorFilter = filter;
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
public void setDelegate(ImageReceiverDelegate delegate) {
|
|
|
|
this.delegate = delegate;
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
public void setPressed(boolean value) {
|
|
|
|
isPressed = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getPressed() {
|
|
|
|
return isPressed;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
public void setOrientation(int angle, boolean center) {
|
2016-03-06 02:49:31 +01:00
|
|
|
while (angle < 0) {
|
|
|
|
angle += 360;
|
|
|
|
}
|
|
|
|
while (angle > 360) {
|
|
|
|
angle -= 360;
|
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
orientation = angle;
|
|
|
|
centerRotation = center;
|
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public void setInvalidateAll(boolean value) {
|
|
|
|
invalidateAll = value;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
public int getOrientation() {
|
|
|
|
return orientation;
|
|
|
|
}
|
|
|
|
|
2014-03-31 16:14:49 +02:00
|
|
|
public void setImageBitmap(Bitmap bitmap) {
|
2015-01-02 23:15:07 +01:00
|
|
|
setImageBitmap(bitmap != null ? new BitmapDrawable(null, bitmap) : null);
|
|
|
|
}
|
|
|
|
|
2014-03-31 16:14:49 +02:00
|
|
|
public void setImageBitmap(Drawable bitmap) {
|
2015-02-01 19:51:02 +01:00
|
|
|
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
|
|
|
|
recycleBitmap(null, false);
|
|
|
|
recycleBitmap(null, true);
|
|
|
|
staticThumb = bitmap;
|
|
|
|
currentThumbLocation = null;
|
|
|
|
currentKey = null;
|
2015-05-21 23:27:27 +02:00
|
|
|
currentExt = null;
|
2015-02-01 19:51:02 +01:00
|
|
|
currentThumbKey = null;
|
2014-04-04 01:22:09 +02:00
|
|
|
currentImage = null;
|
2015-02-01 19:51:02 +01:00
|
|
|
currentThumbFilter = null;
|
|
|
|
currentImageLocation = null;
|
|
|
|
currentHttpUrl = null;
|
|
|
|
currentFilter = null;
|
|
|
|
currentSize = 0;
|
|
|
|
currentCacheOnly = false;
|
2014-10-21 22:35:16 +02:00
|
|
|
bitmapShader = null;
|
2016-04-22 15:49:00 +02:00
|
|
|
bitmapShaderThumb = null;
|
2015-05-03 13:48:36 +02:00
|
|
|
if (setImageBackup != null) {
|
|
|
|
setImageBackup.fileLocation = null;
|
|
|
|
setImageBackup.httpUrl = null;
|
|
|
|
setImageBackup.thumbLocation = null;
|
|
|
|
setImageBackup.thumb = null;
|
|
|
|
}
|
|
|
|
currentAlpha = 1;
|
2015-02-01 19:51:02 +01:00
|
|
|
if (delegate != null) {
|
2015-09-24 22:52:02 +02:00
|
|
|
delegate.didSetImage(this, currentThumb != null || staticThumb != null, true);
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
2014-07-23 18:53:14 +02:00
|
|
|
if (parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2014-07-23 18:53:14 +02:00
|
|
|
}
|
2014-03-31 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
public void clearImage() {
|
2015-02-01 19:51:02 +01:00
|
|
|
recycleBitmap(null, false);
|
|
|
|
recycleBitmap(null, true);
|
|
|
|
if (needsQualityThumb) {
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.messageThumbGenerated);
|
|
|
|
ImageLoader.getInstance().cancelLoadingForImageReceiver(this, 0);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 13:48:36 +02:00
|
|
|
public void onDetachedFromWindow() {
|
|
|
|
if (currentImageLocation != null || currentHttpUrl != null || currentThumbLocation != null || staticThumb != null) {
|
|
|
|
if (setImageBackup == null) {
|
|
|
|
setImageBackup = new SetImageBackup();
|
2014-06-11 01:05:54 +02:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
setImageBackup.fileLocation = currentImageLocation;
|
|
|
|
setImageBackup.httpUrl = currentHttpUrl;
|
|
|
|
setImageBackup.filter = currentFilter;
|
|
|
|
setImageBackup.thumb = staticThumb;
|
|
|
|
setImageBackup.thumbLocation = currentThumbLocation;
|
|
|
|
setImageBackup.thumbFilter = currentThumbFilter;
|
|
|
|
setImageBackup.size = currentSize;
|
2015-05-21 23:27:27 +02:00
|
|
|
setImageBackup.ext = currentExt;
|
2015-05-03 13:48:36 +02:00
|
|
|
setImageBackup.cacheOnly = currentCacheOnly;
|
|
|
|
}
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReplacedPhotoInMemCache);
|
|
|
|
clearImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onAttachedToWindow() {
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReplacedPhotoInMemCache);
|
|
|
|
if (setImageBackup != null && (setImageBackup.fileLocation != null || setImageBackup.httpUrl != null || setImageBackup.thumbLocation != null || setImageBackup.thumb != null)) {
|
2015-05-21 23:27:27 +02:00
|
|
|
setImage(setImageBackup.fileLocation, setImageBackup.httpUrl, setImageBackup.filter, setImageBackup.thumb, setImageBackup.thumbLocation, setImageBackup.thumbFilter, setImageBackup.size, setImageBackup.ext, setImageBackup.cacheOnly);
|
2015-05-03 13:48:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
private void drawDrawable(Canvas canvas, Drawable drawable, int alpha, BitmapShader shader) {
|
2015-05-03 13:48:36 +02:00
|
|
|
if (drawable instanceof BitmapDrawable) {
|
|
|
|
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
|
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
Paint paint;
|
|
|
|
if (shader != null) {
|
|
|
|
paint = roundPaint;
|
|
|
|
} else {
|
|
|
|
paint = bitmapDrawable.getPaint();
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
boolean hasFilter = paint != null && paint.getColorFilter() != null;
|
|
|
|
if (hasFilter && !isPressed) {
|
2016-04-22 15:49:00 +02:00
|
|
|
if (shader != null) {
|
|
|
|
roundPaint.setColorFilter(null);
|
|
|
|
} else {
|
|
|
|
bitmapDrawable.setColorFilter(null);
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
} else if (!hasFilter && isPressed) {
|
2016-04-22 15:49:00 +02:00
|
|
|
if (shader != null) {
|
|
|
|
roundPaint.setColorFilter(selectedColorFilter);
|
|
|
|
} else {
|
|
|
|
bitmapDrawable.setColorFilter(selectedColorFilter);
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
|
|
|
if (colorFilter != null) {
|
2016-04-22 15:49:00 +02:00
|
|
|
if (shader != null) {
|
|
|
|
roundPaint.setColorFilter(colorFilter);
|
|
|
|
} else {
|
|
|
|
bitmapDrawable.setColorFilter(colorFilter);
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2016-04-22 15:49:00 +02:00
|
|
|
}
|
|
|
|
int bitmapW;
|
|
|
|
int bitmapH;
|
|
|
|
if (bitmapDrawable instanceof AnimatedFileDrawable) {
|
2016-03-06 02:49:31 +01:00
|
|
|
if (orientation % 360 == 90 || orientation % 360 == 270) {
|
2015-05-03 13:48:36 +02:00
|
|
|
bitmapW = bitmapDrawable.getIntrinsicHeight();
|
|
|
|
bitmapH = bitmapDrawable.getIntrinsicWidth();
|
2014-05-17 01:05:49 +02:00
|
|
|
} else {
|
2015-05-03 13:48:36 +02:00
|
|
|
bitmapW = bitmapDrawable.getIntrinsicWidth();
|
|
|
|
bitmapH = bitmapDrawable.getIntrinsicHeight();
|
|
|
|
}
|
2016-04-22 15:49:00 +02:00
|
|
|
} else {
|
|
|
|
if (orientation % 360 == 90 || orientation % 360 == 270) {
|
|
|
|
bitmapW = bitmapDrawable.getBitmap().getHeight();
|
|
|
|
bitmapH = bitmapDrawable.getBitmap().getWidth();
|
|
|
|
} else {
|
|
|
|
bitmapW = bitmapDrawable.getBitmap().getWidth();
|
|
|
|
bitmapH = bitmapDrawable.getBitmap().getHeight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
float scaleW = bitmapW / (float) imageW;
|
|
|
|
float scaleH = bitmapH / (float) imageH;
|
|
|
|
|
|
|
|
if (shader != null) {
|
|
|
|
roundPaint.setShader(shader);
|
|
|
|
float scale = Math.min(scaleW, scaleH);
|
|
|
|
roundRect.set(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
shaderMatrix.reset();
|
|
|
|
if (Math.abs(scaleW - scaleH) > 0.00001f) {
|
|
|
|
if (bitmapW / scaleH > imageW) {
|
|
|
|
drawRegion.set(imageX - ((int) (bitmapW / scaleH) - imageW) / 2, imageY, imageX + ((int) (bitmapW / scaleH) + imageW) / 2, imageY + imageH);
|
|
|
|
} else {
|
|
|
|
drawRegion.set(imageX, imageY - ((int) (bitmapH / scaleW) - imageH) / 2, imageX + imageW, imageY + ((int) (bitmapH / scaleW) + imageH) / 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
|
|
|
if (isVisible) {
|
|
|
|
if (Math.abs(scaleW - scaleH) > 0.00001f) {
|
|
|
|
int w = (int) Math.floor(imageW * scale);
|
|
|
|
int h = (int) Math.floor(imageH * scale);
|
|
|
|
bitmapRect.set((bitmapW - w) / 2, (bitmapH - h) / 2, (bitmapW + w) / 2, (bitmapH + h) / 2);
|
|
|
|
shaderMatrix.setRectToRect(bitmapRect, roundRect, Matrix.ScaleToFit.START);
|
|
|
|
} else {
|
|
|
|
bitmapRect.set(0, 0, bitmapW, bitmapH);
|
|
|
|
shaderMatrix.setRectToRect(bitmapRect, roundRect, Matrix.ScaleToFit.FILL);
|
|
|
|
}
|
|
|
|
shader.setLocalMatrix(shaderMatrix);
|
|
|
|
roundPaint.setAlpha(alpha);
|
|
|
|
canvas.drawRoundRect(roundRect, roundRadius, roundRadius, roundPaint);
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-03 13:48:36 +02:00
|
|
|
if (isAspectFit) {
|
|
|
|
float scale = Math.max(scaleW, scaleH);
|
|
|
|
canvas.save();
|
|
|
|
bitmapW /= scale;
|
|
|
|
bitmapH /= scale;
|
|
|
|
drawRegion.set(imageX + (imageW - bitmapW) / 2, imageY + (imageH - bitmapH) / 2, imageX + (imageW + bitmapW) / 2, imageY + (imageH + bitmapH) / 2);
|
|
|
|
bitmapDrawable.setBounds(drawRegion);
|
|
|
|
try {
|
|
|
|
bitmapDrawable.setAlpha(alpha);
|
|
|
|
bitmapDrawable.draw(canvas);
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (bitmapDrawable == currentImage && currentKey != null) {
|
|
|
|
ImageLoader.getInstance().removeImage(currentKey);
|
|
|
|
currentKey = null;
|
|
|
|
} else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
|
|
|
|
ImageLoader.getInstance().removeImage(currentThumbKey);
|
|
|
|
currentThumbKey = null;
|
|
|
|
}
|
2015-05-21 23:27:27 +02:00
|
|
|
setImage(currentImageLocation, currentHttpUrl, currentFilter, currentThumb, currentThumbLocation, currentThumbFilter, currentSize, currentExt, currentCacheOnly);
|
2015-05-03 13:48:36 +02:00
|
|
|
FileLog.e("tmessages", e);
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
canvas.restore();
|
|
|
|
} else {
|
|
|
|
if (Math.abs(scaleW - scaleH) > 0.00001f) {
|
2014-10-21 22:35:16 +02:00
|
|
|
canvas.save();
|
2015-05-03 13:48:36 +02:00
|
|
|
canvas.clipRect(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
|
2016-03-06 02:49:31 +01:00
|
|
|
if (orientation % 360 != 0) {
|
2015-05-03 13:48:36 +02:00
|
|
|
if (centerRotation) {
|
|
|
|
canvas.rotate(orientation, imageW / 2, imageH / 2);
|
|
|
|
} else {
|
|
|
|
canvas.rotate(orientation, 0, 0);
|
2014-10-01 21:55:24 +02:00
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
if (bitmapDrawable instanceof AnimatedFileDrawable) {
|
|
|
|
drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
|
2015-05-03 13:48:36 +02:00
|
|
|
} else {
|
2016-04-22 15:49:00 +02:00
|
|
|
if (bitmapW / scaleH > imageW) {
|
|
|
|
bitmapW /= scaleH;
|
|
|
|
drawRegion.set(imageX - (bitmapW - imageW) / 2, imageY, imageX + (bitmapW + imageW) / 2, imageY + imageH);
|
|
|
|
} else {
|
|
|
|
bitmapH /= scaleW;
|
|
|
|
drawRegion.set(imageX, imageY - (bitmapH - imageH) / 2, imageX + imageW, imageY + (bitmapH + imageH) / 2);
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
2016-03-06 02:49:31 +01:00
|
|
|
if (orientation % 360 == 90 || orientation % 360 == 270) {
|
2015-05-03 13:48:36 +02:00
|
|
|
int width = (drawRegion.right - drawRegion.left) / 2;
|
|
|
|
int height = (drawRegion.bottom - drawRegion.top) / 2;
|
|
|
|
int centerX = (drawRegion.right + drawRegion.left) / 2;
|
|
|
|
int centerY = (drawRegion.top + drawRegion.bottom) / 2;
|
|
|
|
bitmapDrawable.setBounds(centerX - height, centerY - width, centerX + height, centerY + width);
|
|
|
|
} else {
|
|
|
|
bitmapDrawable.setBounds(drawRegion);
|
|
|
|
}
|
|
|
|
if (isVisible) {
|
|
|
|
try {
|
|
|
|
bitmapDrawable.setAlpha(alpha);
|
|
|
|
bitmapDrawable.draw(canvas);
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (bitmapDrawable == currentImage && currentKey != null) {
|
|
|
|
ImageLoader.getInstance().removeImage(currentKey);
|
|
|
|
currentKey = null;
|
|
|
|
} else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
|
|
|
|
ImageLoader.getInstance().removeImage(currentThumbKey);
|
|
|
|
currentThumbKey = null;
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2015-05-21 23:27:27 +02:00
|
|
|
setImage(currentImageLocation, currentHttpUrl, currentFilter, currentThumb, currentThumbLocation, currentThumbFilter, currentSize, currentExt, currentCacheOnly);
|
2015-05-03 13:48:36 +02:00
|
|
|
FileLog.e("tmessages", e);
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
|
2015-05-03 13:48:36 +02:00
|
|
|
canvas.restore();
|
|
|
|
} else {
|
|
|
|
canvas.save();
|
2016-03-06 02:49:31 +01:00
|
|
|
if (orientation % 360 != 0) {
|
2015-05-03 13:48:36 +02:00
|
|
|
if (centerRotation) {
|
|
|
|
canvas.rotate(orientation, imageW / 2, imageH / 2);
|
2015-02-26 02:32:51 +01:00
|
|
|
} else {
|
2015-05-03 13:48:36 +02:00
|
|
|
canvas.rotate(orientation, 0, 0);
|
2014-10-21 22:35:16 +02:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
|
|
|
drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
|
2016-03-06 02:49:31 +01:00
|
|
|
if (orientation % 360 == 90 || orientation % 360 == 270) {
|
2015-05-03 13:48:36 +02:00
|
|
|
int width = (drawRegion.right - drawRegion.left) / 2;
|
|
|
|
int height = (drawRegion.bottom - drawRegion.top) / 2;
|
|
|
|
int centerX = (drawRegion.right + drawRegion.left) / 2;
|
|
|
|
int centerY = (drawRegion.top + drawRegion.bottom) / 2;
|
|
|
|
bitmapDrawable.setBounds(centerX - height, centerY - width, centerX + height, centerY + width);
|
2014-10-21 22:35:16 +02:00
|
|
|
} else {
|
2015-05-03 13:48:36 +02:00
|
|
|
bitmapDrawable.setBounds(drawRegion);
|
|
|
|
}
|
|
|
|
if (isVisible) {
|
|
|
|
try {
|
|
|
|
bitmapDrawable.setAlpha(alpha);
|
|
|
|
bitmapDrawable.draw(canvas);
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (bitmapDrawable == currentImage && currentKey != null) {
|
|
|
|
ImageLoader.getInstance().removeImage(currentKey);
|
|
|
|
currentKey = null;
|
|
|
|
} else if (bitmapDrawable == currentThumb && currentThumbKey != null) {
|
|
|
|
ImageLoader.getInstance().removeImage(currentThumbKey);
|
|
|
|
currentThumbKey = null;
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2015-05-21 23:27:27 +02:00
|
|
|
setImage(currentImageLocation, currentHttpUrl, currentFilter, currentThumb, currentThumbLocation, currentThumbFilter, currentSize, currentExt, currentCacheOnly);
|
2015-05-03 13:48:36 +02:00
|
|
|
FileLog.e("tmessages", e);
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
|
|
|
canvas.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
drawable.setBounds(drawRegion);
|
|
|
|
if (isVisible) {
|
|
|
|
try {
|
|
|
|
drawable.setAlpha(alpha);
|
|
|
|
drawable.draw(canvas);
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 18:19:48 +01:00
|
|
|
private void checkAlphaAnimation(boolean skip) {
|
2015-05-03 13:48:36 +02:00
|
|
|
if (currentAlpha != 1) {
|
2016-01-11 18:19:48 +01:00
|
|
|
if (!skip) {
|
|
|
|
long currentTime = System.currentTimeMillis();
|
2016-04-22 15:49:00 +02:00
|
|
|
long dt = currentTime - lastUpdateAlphaTime;
|
|
|
|
if (dt > 18) {
|
|
|
|
dt = 18;
|
|
|
|
}
|
|
|
|
currentAlpha += dt / 150.0f;
|
2016-01-11 18:19:48 +01:00
|
|
|
if (currentAlpha > 1) {
|
|
|
|
currentAlpha = 1;
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
|
|
|
lastUpdateAlphaTime = System.currentTimeMillis();
|
|
|
|
if (parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean draw(Canvas canvas) {
|
|
|
|
try {
|
2016-01-11 18:19:48 +01:00
|
|
|
Drawable drawable = null;
|
|
|
|
boolean animationNotReady = currentImage instanceof AnimatedFileDrawable && !((AnimatedFileDrawable) currentImage).hasBitmap();
|
2016-04-22 15:49:00 +02:00
|
|
|
boolean isThumb = false;
|
2016-01-11 18:19:48 +01:00
|
|
|
if (!forcePreview && currentImage != null && !animationNotReady) {
|
|
|
|
drawable = currentImage;
|
2015-05-03 13:48:36 +02:00
|
|
|
} else if (staticThumb instanceof BitmapDrawable) {
|
2016-01-11 18:19:48 +01:00
|
|
|
drawable = staticThumb;
|
2016-04-22 15:49:00 +02:00
|
|
|
isThumb = true;
|
2015-05-03 13:48:36 +02:00
|
|
|
} else if (currentThumb != null) {
|
2016-01-11 18:19:48 +01:00
|
|
|
drawable = currentThumb;
|
2016-04-22 15:49:00 +02:00
|
|
|
isThumb = true;
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
2016-01-11 18:19:48 +01:00
|
|
|
if (drawable != null) {
|
2015-05-03 13:48:36 +02:00
|
|
|
if (crossfadeAlpha != 0) {
|
2016-01-11 18:19:48 +01:00
|
|
|
if (crossfadeWithThumb && animationNotReady) {
|
2016-04-22 15:49:00 +02:00
|
|
|
drawDrawable(canvas, drawable, (int) (overrideAlpha * 255), bitmapShaderThumb);
|
2016-01-11 18:19:48 +01:00
|
|
|
} else {
|
|
|
|
if (crossfadeWithThumb && currentAlpha != 1.0f) {
|
|
|
|
Drawable thumbDrawable = null;
|
|
|
|
if (drawable == currentImage) {
|
|
|
|
if (staticThumb != null) {
|
|
|
|
thumbDrawable = staticThumb;
|
|
|
|
} else if (currentThumb != null) {
|
|
|
|
thumbDrawable = currentThumb;
|
|
|
|
}
|
|
|
|
} else if (drawable == currentThumb) {
|
|
|
|
if (staticThumb != null) {
|
|
|
|
thumbDrawable = staticThumb;
|
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
2016-01-11 18:19:48 +01:00
|
|
|
if (thumbDrawable != null) {
|
2016-04-22 15:49:00 +02:00
|
|
|
drawDrawable(canvas, thumbDrawable, (int) (overrideAlpha * 255), bitmapShaderThumb);
|
2014-10-01 21:55:24 +02:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
2016-04-22 15:49:00 +02:00
|
|
|
drawDrawable(canvas, drawable, (int) (overrideAlpha * currentAlpha * 255), isThumb ? bitmapShaderThumb : bitmapShader);
|
2014-06-11 01:05:54 +02:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
} else {
|
2016-04-22 15:49:00 +02:00
|
|
|
drawDrawable(canvas, drawable, (int) (overrideAlpha * 255), isThumb ? bitmapShaderThumb : bitmapShader);
|
2014-05-17 01:05:49 +02:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
|
2016-01-11 18:19:48 +01:00
|
|
|
checkAlphaAnimation(animationNotReady && crossfadeWithThumb);
|
2014-08-29 23:06:04 +02:00
|
|
|
return true;
|
2015-02-01 19:51:02 +01:00
|
|
|
} else if (staticThumb != null) {
|
2016-04-22 15:49:00 +02:00
|
|
|
drawDrawable(canvas, staticThumb, 255, null);
|
2016-01-11 18:19:48 +01:00
|
|
|
checkAlphaAnimation(animationNotReady);
|
2014-08-29 23:06:04 +02:00
|
|
|
return true;
|
2016-01-11 18:19:48 +01:00
|
|
|
} else {
|
|
|
|
checkAlphaAnimation(animationNotReady);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
2014-08-29 23:06:04 +02:00
|
|
|
return false;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2014-06-07 01:35:21 +02:00
|
|
|
|
|
|
|
public Bitmap getBitmap() {
|
2016-01-11 18:19:48 +01:00
|
|
|
if (currentImage instanceof AnimatedFileDrawable) {
|
|
|
|
return ((AnimatedFileDrawable) currentImage).getAnimatedBitmap();
|
|
|
|
} else if (currentImage instanceof BitmapDrawable) {
|
|
|
|
return ((BitmapDrawable) currentImage).getBitmap();
|
|
|
|
} else if (currentThumb instanceof BitmapDrawable) {
|
|
|
|
return ((BitmapDrawable) currentThumb).getBitmap();
|
2015-02-01 19:51:02 +01:00
|
|
|
} else if (staticThumb instanceof BitmapDrawable) {
|
|
|
|
return ((BitmapDrawable) staticThumb).getBitmap();
|
2014-06-07 01:35:21 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:32:51 +01:00
|
|
|
public int getBitmapWidth() {
|
2016-01-11 18:19:48 +01:00
|
|
|
if (currentImage instanceof AnimatedFileDrawable) {
|
2016-03-06 02:49:31 +01:00
|
|
|
return orientation % 360 == 0 || orientation % 360 == 180 ? currentImage.getIntrinsicWidth() : currentImage.getIntrinsicHeight();
|
2016-01-11 18:19:48 +01:00
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
Bitmap bitmap = getBitmap();
|
2016-03-06 02:49:31 +01:00
|
|
|
return orientation % 360 == 0 || orientation % 360 == 180 ? bitmap.getWidth() : bitmap.getHeight();
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getBitmapHeight() {
|
2016-01-11 18:19:48 +01:00
|
|
|
if (currentImage instanceof AnimatedFileDrawable) {
|
2016-03-06 02:49:31 +01:00
|
|
|
return orientation % 360 == 0 || orientation % 360 == 180 ? currentImage.getIntrinsicHeight() : currentImage.getIntrinsicWidth();
|
2016-01-11 18:19:48 +01:00
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
Bitmap bitmap = getBitmap();
|
2016-03-06 02:49:31 +01:00
|
|
|
return orientation % 360 == 0 || orientation % 360 == 180 ? bitmap.getHeight() : bitmap.getWidth();
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
|
|
|
|
2014-06-11 01:05:54 +02:00
|
|
|
public void setVisible(boolean value, boolean invalidate) {
|
2014-06-07 01:35:21 +02:00
|
|
|
if (isVisible == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isVisible = value;
|
2014-06-12 21:55:13 +02:00
|
|
|
if (invalidate && parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2014-06-07 01:35:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getVisible() {
|
|
|
|
return isVisible;
|
|
|
|
}
|
2014-06-11 01:05:54 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
public void setAlpha(float value) {
|
2015-05-03 13:48:36 +02:00
|
|
|
overrideAlpha = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCrossfadeAlpha(byte value) {
|
|
|
|
crossfadeAlpha = value;
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
|
|
|
|
2014-06-11 01:05:54 +02:00
|
|
|
public boolean hasImage() {
|
2015-02-01 19:51:02 +01:00
|
|
|
return currentImage != null || currentThumb != null || currentKey != null || currentHttpUrl != null || staticThumb != null;
|
2014-06-11 01:05:54 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
|
2016-01-11 18:19:48 +01:00
|
|
|
public boolean hasBitmapImage() {
|
|
|
|
return currentImage != null || currentThumb != null || staticThumb != null;
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public void setAspectFit(boolean value) {
|
|
|
|
isAspectFit = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setParentView(View view) {
|
|
|
|
parentView = view;
|
2016-01-11 18:19:48 +01:00
|
|
|
if (currentImage instanceof AnimatedFileDrawable) {
|
|
|
|
AnimatedFileDrawable fileDrawable = (AnimatedFileDrawable) currentImage;
|
|
|
|
fileDrawable.setParentView(parentView);
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setImageCoords(int x, int y, int width, int height) {
|
|
|
|
imageX = x;
|
|
|
|
imageY = y;
|
|
|
|
imageW = width;
|
|
|
|
imageH = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getImageX() {
|
|
|
|
return imageX;
|
|
|
|
}
|
|
|
|
|
2015-04-09 20:00:14 +02:00
|
|
|
public int getImageX2() {
|
|
|
|
return imageX + imageW;
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public int getImageY() {
|
|
|
|
return imageY;
|
|
|
|
}
|
|
|
|
|
2015-04-09 20:00:14 +02:00
|
|
|
public int getImageY2() {
|
|
|
|
return imageY + imageH;
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public int getImageWidth() {
|
|
|
|
return imageW;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getImageHeight() {
|
|
|
|
return imageH;
|
|
|
|
}
|
|
|
|
|
2015-05-21 23:27:27 +02:00
|
|
|
public String getExt() {
|
|
|
|
return currentExt;
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public boolean isInsideImage(float x, float y) {
|
|
|
|
return x >= imageX && x <= imageX + imageW && y >= imageY && y <= imageY + imageH;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Rect getDrawRegion() {
|
|
|
|
return drawRegion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFilter() {
|
2015-02-01 19:51:02 +01:00
|
|
|
return currentFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getThumbFilter() {
|
|
|
|
return currentThumbFilter;
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getKey() {
|
2015-02-01 19:51:02 +01:00
|
|
|
return currentKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getThumbKey() {
|
|
|
|
return currentThumbKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSize() {
|
|
|
|
return currentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TLObject getImageLocation() {
|
|
|
|
return currentImageLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TLRPC.FileLocation getThumbLocation() {
|
|
|
|
return currentThumbLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHttpImageLocation() {
|
|
|
|
return currentHttpUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getCacheOnly() {
|
|
|
|
return currentCacheOnly;
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
2014-10-09 17:55:05 +02:00
|
|
|
|
|
|
|
public void setForcePreview(boolean value) {
|
|
|
|
forcePreview = value;
|
|
|
|
}
|
2014-10-21 22:35:16 +02:00
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
public boolean isForcePreview() {
|
|
|
|
return forcePreview;
|
|
|
|
}
|
|
|
|
|
2014-10-21 22:35:16 +02:00
|
|
|
public void setRoundRadius(int value) {
|
|
|
|
roundRadius = value;
|
|
|
|
}
|
2014-11-17 03:44:57 +01:00
|
|
|
|
|
|
|
public int getRoundRadius() {
|
|
|
|
return roundRadius;
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
|
|
|
|
public void setParentMessageObject(MessageObject messageObject) {
|
|
|
|
parentMessageObject = messageObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MessageObject getParentMessageObject() {
|
|
|
|
return parentMessageObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setNeedsQualityThumb(boolean value) {
|
|
|
|
needsQualityThumb = value;
|
|
|
|
if (needsQualityThumb) {
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageThumbGenerated);
|
|
|
|
} else {
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.messageThumbGenerated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isNeedsQualityThumb() {
|
|
|
|
return needsQualityThumb;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setShouldGenerateQualityThumb(boolean value) {
|
|
|
|
shouldGenerateQualityThumb = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isShouldGenerateQualityThumb() {
|
|
|
|
return shouldGenerateQualityThumb;
|
|
|
|
}
|
|
|
|
|
2016-01-11 18:19:48 +01:00
|
|
|
public void setAllowStartAnimation(boolean value) {
|
|
|
|
allowStartAnimation = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAllowStartAnimation() {
|
|
|
|
return allowStartAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startAnimation() {
|
|
|
|
if (currentImage instanceof AnimatedFileDrawable) {
|
|
|
|
((AnimatedFileDrawable) currentImage).start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stopAnimation() {
|
|
|
|
if (currentImage instanceof AnimatedFileDrawable) {
|
|
|
|
((AnimatedFileDrawable) currentImage).stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAnimationRunning() {
|
|
|
|
return currentImage instanceof AnimatedFileDrawable && ((AnimatedFileDrawable) currentImage).isRunning();
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
protected Integer getTag(boolean thumb) {
|
|
|
|
if (thumb) {
|
|
|
|
return thumbTag;
|
|
|
|
} else {
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setTag(Integer value, boolean thumb) {
|
|
|
|
if (thumb) {
|
|
|
|
thumbTag = value;
|
|
|
|
} else {
|
|
|
|
tag = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 18:19:48 +01:00
|
|
|
protected boolean setImageBitmapByKey(BitmapDrawable bitmap, String key, boolean thumb, boolean memCache) {
|
2015-02-01 19:51:02 +01:00
|
|
|
if (bitmap == null || key == null) {
|
2016-01-11 18:19:48 +01:00
|
|
|
return false;
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
if (!thumb) {
|
|
|
|
if (currentKey == null || !key.equals(currentKey)) {
|
2016-01-11 18:19:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(bitmap instanceof AnimatedFileDrawable)) {
|
|
|
|
ImageLoader.getInstance().incrementUseCount(currentKey);
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
currentImage = bitmap;
|
2016-04-22 15:49:00 +02:00
|
|
|
if (roundRadius != 0 && bitmap instanceof BitmapDrawable) {
|
|
|
|
if (bitmap instanceof AnimatedFileDrawable) {
|
|
|
|
((AnimatedFileDrawable) bitmap).setRoundRadius(roundRadius);
|
|
|
|
} else {
|
|
|
|
Bitmap object = bitmap.getBitmap();
|
|
|
|
bitmapShader = new BitmapShader(object, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bitmapShader = null;
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
|
|
|
|
if (!memCache && !forcePreview) {
|
|
|
|
if (currentThumb == null && staticThumb == null || currentAlpha == 1.0f) {
|
|
|
|
currentAlpha = 0.0f;
|
|
|
|
lastUpdateAlphaTime = System.currentTimeMillis();
|
|
|
|
crossfadeWithThumb = currentThumb != null || staticThumb != null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentAlpha = 1.0f;
|
|
|
|
}
|
2016-01-11 18:19:48 +01:00
|
|
|
if (bitmap instanceof AnimatedFileDrawable) {
|
|
|
|
AnimatedFileDrawable fileDrawable = (AnimatedFileDrawable) bitmap;
|
|
|
|
fileDrawable.setParentView(parentView);
|
|
|
|
if (allowStartAnimation) {
|
|
|
|
fileDrawable.start();
|
|
|
|
}
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
if (parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
2016-01-11 18:19:48 +01:00
|
|
|
} else if (currentThumb == null && (currentImage == null || (currentImage instanceof AnimatedFileDrawable && !((AnimatedFileDrawable) currentImage).hasBitmap()) || forcePreview)) {
|
2015-02-01 19:51:02 +01:00
|
|
|
if (currentThumbKey == null || !key.equals(currentThumbKey)) {
|
2016-01-11 18:19:48 +01:00
|
|
|
return false;
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
ImageLoader.getInstance().incrementUseCount(currentThumbKey);
|
2015-05-03 13:48:36 +02:00
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
currentThumb = bitmap;
|
2015-05-03 13:48:36 +02:00
|
|
|
|
2016-04-22 15:49:00 +02:00
|
|
|
if (roundRadius != 0 && currentImage == null && bitmap instanceof BitmapDrawable) {
|
|
|
|
if (bitmap instanceof AnimatedFileDrawable) {
|
|
|
|
((AnimatedFileDrawable) bitmap).setRoundRadius(roundRadius);
|
|
|
|
} else {
|
|
|
|
Bitmap object = bitmap.getBitmap();
|
|
|
|
bitmapShaderThumb = new BitmapShader(object, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bitmapShaderThumb = null;
|
|
|
|
}
|
|
|
|
|
2015-05-03 13:48:36 +02:00
|
|
|
if (!memCache && crossfadeAlpha != 2) {
|
|
|
|
currentAlpha = 0.0f;
|
|
|
|
lastUpdateAlphaTime = System.currentTimeMillis();
|
|
|
|
crossfadeWithThumb = staticThumb != null && currentKey == null;
|
|
|
|
} else {
|
|
|
|
currentAlpha = 1.0f;
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
if (!(staticThumb instanceof BitmapDrawable) && parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delegate != null) {
|
|
|
|
delegate.didSetImage(this, currentImage != null || currentThumb != null || staticThumb != null, currentImage == null);
|
|
|
|
}
|
2016-01-11 18:19:48 +01:00
|
|
|
return true;
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void recycleBitmap(String newKey, boolean thumb) {
|
|
|
|
String key;
|
2016-01-11 18:19:48 +01:00
|
|
|
Drawable image;
|
2015-02-01 19:51:02 +01:00
|
|
|
if (thumb) {
|
|
|
|
key = currentThumbKey;
|
|
|
|
image = currentThumb;
|
|
|
|
} else {
|
|
|
|
key = currentKey;
|
|
|
|
image = currentImage;
|
|
|
|
}
|
2016-01-11 18:19:48 +01:00
|
|
|
if (key != null && (newKey == null || !newKey.equals(key)) && image != null) {
|
|
|
|
if (image instanceof AnimatedFileDrawable) {
|
|
|
|
AnimatedFileDrawable fileDrawable = (AnimatedFileDrawable) image;
|
|
|
|
fileDrawable.stop();
|
|
|
|
fileDrawable.recycle();
|
|
|
|
} else if (image instanceof BitmapDrawable) {
|
|
|
|
Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
|
|
|
|
boolean canDelete = ImageLoader.getInstance().decrementUseCount(key);
|
|
|
|
if (!ImageLoader.getInstance().isInCache(key)) {
|
|
|
|
if (ImageLoader.getInstance().runtimeHack != null) {
|
|
|
|
ImageLoader.getInstance().runtimeHack.trackAlloc(bitmap.getRowBytes() * bitmap.getHeight());
|
|
|
|
}
|
|
|
|
if (canDelete) {
|
|
|
|
bitmap.recycle();
|
|
|
|
}
|
2015-09-24 22:52:02 +02:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (thumb) {
|
|
|
|
currentThumb = null;
|
|
|
|
currentThumbKey = null;
|
|
|
|
} else {
|
|
|
|
currentImage = null;
|
|
|
|
currentKey = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, Object... args) {
|
|
|
|
if (id == NotificationCenter.messageThumbGenerated) {
|
|
|
|
String key = (String) args[1];
|
|
|
|
if (currentThumbKey != null && currentThumbKey.equals(key)) {
|
|
|
|
if (currentThumb == null) {
|
|
|
|
ImageLoader.getInstance().incrementUseCount(currentThumbKey);
|
|
|
|
}
|
|
|
|
currentThumb = (BitmapDrawable) args[0];
|
2016-04-22 15:49:00 +02:00
|
|
|
if (roundRadius != 0 && currentImage == null && currentThumb instanceof BitmapDrawable && !(currentThumb instanceof AnimatedFileDrawable)) {
|
|
|
|
Bitmap object = ((BitmapDrawable) currentThumb).getBitmap();
|
|
|
|
bitmapShaderThumb = new BitmapShader(object, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
|
|
|
|
} else {
|
|
|
|
bitmapShaderThumb = null;
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
if (staticThumb instanceof BitmapDrawable) {
|
|
|
|
staticThumb = null;
|
|
|
|
}
|
|
|
|
if (parentView != null) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (invalidateAll) {
|
|
|
|
parentView.invalidate();
|
|
|
|
} else {
|
|
|
|
parentView.invalidate(imageX, imageY, imageX + imageW, imageY + imageH);
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (id == NotificationCenter.didReplacedPhotoInMemCache) {
|
|
|
|
String oldKey = (String) args[0];
|
|
|
|
if (currentKey != null && currentKey.equals(oldKey)) {
|
|
|
|
currentKey = (String) args[1];
|
|
|
|
currentImageLocation = (TLRPC.FileLocation) args[2];
|
|
|
|
}
|
|
|
|
if (currentThumbKey != null && currentThumbKey.equals(oldKey)) {
|
|
|
|
currentThumbKey = (String) args[1];
|
|
|
|
currentThumbLocation = (TLRPC.FileLocation) args[2];
|
|
|
|
}
|
|
|
|
if (setImageBackup != null) {
|
|
|
|
if (currentKey != null && currentKey.equals(oldKey)) {
|
|
|
|
currentKey = (String) args[1];
|
|
|
|
currentImageLocation = (TLRPC.FileLocation) args[2];
|
|
|
|
}
|
|
|
|
if (currentThumbKey != null && currentThumbKey.equals(oldKey)) {
|
|
|
|
currentThumbKey = (String) args[1];
|
|
|
|
currentThumbLocation = (TLRPC.FileLocation) args[2];
|
2015-02-01 19:51:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|