我目前正在使用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"
/>
答案 0 :(得分:0)
尝试此代码
@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) {
if (width > height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
答案 1 :(得分:0)
尝试这个
@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);
}
}
}
答案 2 :(得分:0)
您需要将AutoFitTextureView放置在父视图/容器中,以允许其调整大小。如果父视图不允许这样做,则无论您在onMeasure中执行什么操作,它都会调整Textureview的大小。
例如,Camera2Basic示例将AutoFitTextureView放置在RelativeLayout中:
答案 3 :(得分:0)
1。)您的肖像分红和除数被错误地逆转了:
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
2。)AutoFitTextureView样本在onMeasure(..)中区分纹理方向的逻辑具有误导性:
if (width > height * mRatioWidth / mRatioHeight)
虽然它在4:3(仿真器,S2,S4)附近可正常工作,但在更大的宽高比(基本PH-1)下却无法工作。 将纹理方向添加到setAspectRatio(..)方法可提供所需的信息。
注意:下面的示例忽略了180°和270°方向。
import android.util.AttributeSet;
class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
private int mTextureOrientation = 0;
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, int textureOrientation) {
if ( width < 0 || height < 0 ) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
mTextureOrientation = textureOrientation;
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 ( mTextureOrientation == 0 ) { // 0 == portrait, 1 == landscape
setMeasuredDimension(width, width * mRatioWidth/mRatioHeight);
} else {
setMeasuredDimension(height * mRatioWidth/mRatioHeight, height);
}
}
}
}