-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoFitTextureView.java
47 lines (40 loc) · 1.56 KB
/
AutoFitTextureView.java
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
package org.faceit.demo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;
import android.view.View.MeasureSpec;
public class AutoFitTextureView extends TextureView {
private int ratioHeight;
private int ratioWidth;
public AutoFitTextureView(Context context) {
this(context, null);
}
public AutoFitTextureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.ratioWidth = 0;
this.ratioHeight = 0;
}
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
this.ratioWidth = width;
this.ratioHeight = height;
requestLayout();
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (this.ratioWidth == 0 || this.ratioHeight == 0) {
setMeasuredDimension(width, height);
} else if (width < (this.ratioWidth * height) / this.ratioHeight) {
setMeasuredDimension(width, (this.ratioHeight * width) / this.ratioWidth);
} else {
setMeasuredDimension((this.ratioWidth * height) / this.ratioHeight, height);
}
}
}