Created
February 16, 2019 06:34
-
-
Save CoXier/e33a36e4eaff315a19d190d6a740db9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.Build; | |
import android.support.annotation.IntDef; | |
import android.support.annotation.RequiresApi; | |
import android.util.AttributeSet; | |
import android.view.TextureView; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
/** | |
* Created by LiJianxin on 2019/2/16. | |
*/ | |
public class AspectRatioTextureView extends TextureView { | |
private float videoAspectRatio; | |
private @ResizeMode int resizeMode; | |
@Documented | |
@Retention(RetentionPolicy.SOURCE) | |
@IntDef({ | |
RESIZE_FILL, | |
RESIZE_FIT, | |
RESIZE_CROP | |
}) | |
public @interface ResizeMode {} | |
public static final int RESIZE_FILL = 0; | |
public static final int RESIZE_FIT = 1; | |
public static final int RESIZE_CROP = 2; | |
public AspectRatioTextureView(Context context) { | |
super(context); | |
} | |
public AspectRatioTextureView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public AspectRatioTextureView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
public AspectRatioTextureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
public void setAspectRatio(float widthHeightRatio) { | |
if (this.videoAspectRatio != widthHeightRatio) { | |
this.videoAspectRatio = widthHeightRatio; | |
requestLayout(); | |
} | |
} | |
/** | |
* Sets the {@link ResizeMode} | |
* | |
* @param resizeMode The {@link ResizeMode}. | |
*/ | |
public void setResizeMode(@ResizeMode int resizeMode) { | |
if (this.resizeMode != resizeMode) { | |
this.resizeMode = resizeMode; | |
requestLayout(); | |
} | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
if (videoAspectRatio <= 0) { | |
// Aspect ratio not set. | |
return; | |
} | |
int width = getMeasuredWidth(); | |
int height = getMeasuredHeight(); | |
float viewAspectRatio = (float) width / height; | |
switch (resizeMode) { | |
case RESIZE_FIT: | |
if (videoAspectRatio > viewAspectRatio) { | |
height = (int) (width / videoAspectRatio); | |
} else { | |
width = (int) (height * videoAspectRatio); | |
} | |
break; | |
case RESIZE_CROP: | |
if (videoAspectRatio > viewAspectRatio) { | |
width = (int) (height * videoAspectRatio); | |
} else { | |
height = (int) (width / videoAspectRatio); | |
} | |
default: | |
case RESIZE_FILL: | |
break; | |
} | |
super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment