public class IntroVideoView extends VideoView {
private static final int SCALE_TYPE_NORMAL = 0;
private static final int SCALE_TYPE_CENTER_CROP = 1;
private static final int SCALE_TYPE_FILL = 2;
private int mScaleType;
private int mHorizontalAspectRatioThreshold;
private int mVerticalAspectRatioThreshold;
public IntroVideoView(Context context) {
this(context, null, 0);
}
public IntroVideoView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IntroVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.IntroVideoView, 0, 0);
mScaleType = attributes.getInt(R.styleable.IntroVideoView_scaleType, SCALE_TYPE_NORMAL);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mScaleType == SCALE_TYPE_CENTER_CROP) {
applyCenterCropMeasure(widthMeasureSpec, heightMeasureSpec);
} else if (mScaleType == SCALE_TYPE_FILL) {
applyFillMeasure(widthMeasureSpec, heightMeasureSpec);
} // else default/no-op
}
@Override
public void layout(int l, int t, int r, int b) {
if (mScaleType == SCALE_TYPE_CENTER_CROP) {
applyCenterCropLayout(l, t, r, b);
} else {
super.layout(l, t, r, b);
}
}
private void applyCenterCropLayout(int left, int top, int right, int bottom) {
super.layout(left + mHorizontalAspectRatioThreshold, top + mVerticalAspectRatioThreshold, right
+ mHorizontalAspectRatioThreshold, bottom + mVerticalAspectRatioThreshold);
}
private void applyCenterCropMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int videoWidth = getMeasuredWidth();
int videoHeight = getMeasuredHeight();
int viewWidth = getDefaultSize(0, widthMeasureSpec);
int viewHeight = getDefaultSize(0, heightMeasureSpec);
mHorizontalAspectRatioThreshold = 0;
mVerticalAspectRatioThreshold = 0;
if (videoWidth == viewWidth) {
int newWidth = (int) ((float) videoWidth / videoHeight * viewHeight);
setMeasuredDimension(newWidth, viewHeight);
mHorizontalAspectRatioThreshold = -(newWidth - viewWidth) / 2;
} else {
int newHeight = (int) ((float) videoHeight / videoWidth * viewWidth);
setMeasuredDimension(viewWidth, newHeight);
mVerticalAspectRatioThreshold = -(newHeight - viewHeight) / 2;
}
}
private void applyFillMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
'Android' 카테고리의 다른 글
Android buildSrc 로 Dependency 관리하기 (0) | 2022.02.09 |
---|---|
안드로이드 코루틴 - async와 await, LifecycleScope과 ViewModelScope (0) | 2022.01.11 |
안드로이드 edittext 최대길이(maxlength) 조절 (0) | 2021.10.21 |
MVVM 을 왜 써야할까? MVP와 다른점이 뭘까 (0) | 2021.09.16 |
Fragment 에서 Activity의 finsh() 기능 만들기 (0) | 2021.08.31 |
android 10 대비하여 scopedStorage 구현 (0) | 2021.08.26 |
Drawable Shape 코드로 색상 동적 변경 (0) | 2021.07.31 |
안드로이드 Dialog 만들기 (kotiln) (0) | 2021.07.30 |