当我同时将lockedWidth
和lockedHeight
都设置为 0 时,它将使应用程序崩溃并得到此错误阻止。
因此,如果我再次致电super.onMeasure(widthSpec, heightSpec);
然后它不会显示我想要的宽高比。
谁能帮我解决此问题?
我尝试了以下代码。
protected void onMeasure(int widthSpec, int heightSpec) {
double localRatio = aspectRatio;
Log.i(TAG, "[onMeasure] aspectRatio: " + aspectRatio);
Log.i(TAG, "[onMeasure] widthSpec: " + widthSpec);
Log.i(TAG, "[onMeasure] heightSpec: " + heightSpec);
if (localRatio == 0.0) {
Log.i(TAG, "onMeasure: If localRatio = 0");
super.onMeasure(widthSpec, heightSpec);
} else {
int lockedWidth = MeasureSpec.getSize(widthSpec);
int lockedHeight = MeasureSpec.getSize(heightSpec);
/* issue generated here is issue generated when lockedWidth &&
lockedHeight get 0 value. */
/*int lockedWidth = 0;
int lockedHeight = 0;*/
int widthMode = MeasureSpec.getMode(widthSpec);
int heightMode = MeasureSpec.getMode(heightSpec);
Log.i(TAG, "onMeasure: widthMode: " + widthMode);
Log.i(TAG, "onMeasure: heightMode: " + heightMode);
Log.i(TAG, "[onMeasure] else lockedWidth: " + lockedWidth);
Log.i(TAG, "[onMeasure] else lockedHeight: " + lockedHeight);
if (lockedWidth == 0 && lockedHeight == 0) {
Log.i(TAG, "[onMeasure] lockedWidth=0 && lockedHeight=0");
/* super.onMeasure(widthSpec, heightSpec);*/
return;
}
// Get the padding of the border background.
int hPadding = getPaddingLeft() + getPaddingRight();
int vPadding = getPaddingTop() + getPaddingBottom();
Log.i(TAG, "[onMeasure] else hPadding: " + hPadding);
Log.i(TAG, "[onMeasure] else vPadding: " + vPadding);
// Resize the preview frame with correct aspect ratio.
lockedWidth -= hPadding;
lockedHeight -= vPadding;
Log.i(TAG, "[onMeasure] else- lockedWidth: " + lockedWidth);
Log.i(TAG, "[onMeasure] else- lockedHeight: " + lockedHeight);
if (lockedHeight > 0 && (lockedWidth > lockedHeight * localRatio))
{
lockedWidth = (int) (lockedHeight * localRatio + .5);
Log.i(TAG, "[onMeasure] IF lockedWidth: " + lockedWidth);
} else {
lockedHeight = (int) (lockedWidth / localRatio + .5);
Log.i(TAG, "[onMeasure] ELSE lockedHeight: " + lockedHeight);
}
// Add the padding of the border.
lockedWidth += hPadding;
lockedHeight += vPadding;
Log.i(TAG, "[onMeasure] else +lockedWidth: " + lockedWidth);
Log.i(TAG, "[onMeasure] else +lockedHeight: " + lockedHeight);
// Ask children to follow the new preview dimension.
super.onMeasure(MeasureSpec.makeMeasureSpec(lockedWidth,
MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(lockedHeight,
MeasureSpec.EXACTLY));
}
}