【发布时间】:2019-05-14 06:14:54
【问题描述】:
我目前正在使用 Camera2 API 制作一个 android 应用程序。我遇到了 AutoFitTextureView 的问题,图像显示为拉伸。 我的 AutoFitTextureView 代码如下
public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
private boolean mWithMargin = false;
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);
}
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
XML 代码-
拉伸图像:
我正在使用的 XML 代码如下所示,预览即将拉长-
<com.qopius.AutoFitTextureViewNew
android:id="@+id/texture"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
【问题讨论】:
-
仍在寻找有效的答案
标签: android android-camera2 textureview