1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package ml.docilealligator.infinityforreddit.customviews;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import pl.droidsonroids.gif.GifImageView;
public class AspectRatioGifImageView extends GifImageView {
private float ratio;
public AspectRatioGifImageView(Context context) {
super(context);
this.ratio = 1.0F;
}
public AspectRatioGifImageView(Context context, AttributeSet attrs) {
super(context, attrs);
this.ratio = 1.0F;
this.init(context, attrs);
}
public final float getRatio() {
return this.ratio;
}
public final void setRatio(float var1) {
if (Math.abs(this.ratio - var1) > 0.0001) {
this.ratio = var1;
requestLayout();
invalidate();
}
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, com.santalu.aspectratioimageview.R.styleable.AspectRatioImageView);
this.ratio = a.getFloat(com.santalu.aspectratioimageview.R.styleable.AspectRatioImageView_ari_ratio, 1.0F);
a.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
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);
}
}
}
/*@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int desiredHeight = (int) (widthSize * ratio);
int selectedHeight;
if(heightMode == MeasureSpec.EXACTLY) {
selectedHeight = heightSize;
} else if(heightMode == MeasureSpec.AT_MOST) {
selectedHeight = Math.min(heightSize, desiredHeight);
} else {
selectedHeight = desiredHeight;
}
super.onMeasure(MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(selectedHeight, MeasureSpec.EXACTLY));
}*/
}
|