Disable aspect ratio logic when it is negative (#1296)

Gallery can set aspect ratio to -1 which would result in negative measured height. It seem that Android treats dimensions as unsigned numbers, so negative numbers actually become huge positive numbers resulting in "infinite" height.

It should be noted that this change allows to disable aspect ratio logic even when some dimension is set to `wrap_content`.
This commit is contained in:
Sergei Kozelko 2023-01-21 12:51:44 +08:00 committed by GitHub
parent aaa55a6af9
commit c42f183696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,16 +44,18 @@ public class AspectRatioGifImageView extends GifImageView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
if (width != 0 || height != 0) {
if (width > 0) {
height = (int) ((float) width * this.ratio);
} else {
width = (int) ((float) height / this.ratio);
}
if (this.ratio > 0) {
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
if (width != 0 || height != 0) {
if (width > 0) {
height = (int) ((float) width * this.ratio);
} else {
width = (int) ((float) height / this.ratio);
}
this.setMeasuredDimension(width, height);
this.setMeasuredDimension(width, height);
}
}
}