android resolvesize方法,Java View.resolveSizeAndState方法代码示例

import android.view.View; //导入方法依赖的package包/类

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int width = MeasureSpec.getSize(widthMeasureSpec);

int height = MeasureSpec.getSize(heightMeasureSpec);

// General goal: Adjust dimensions to maintain the requested aspect ratio as much

// as possible. Depending on the measure specs handed down, this may not be possible

// Only set one of these to true

boolean scaleWidth = false;

boolean scaleHeight = false;

// Sort out which dimension to scale, if either can be. There are 9 combinations of

// possible measure specs; a few cases below handle multiple combinations

if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {

// Can't adjust sizes at all, do nothing

} else if (widthMode == MeasureSpec.EXACTLY) {

// Width is fixed, heightMode either AT_MOST or UNSPECIFIED, so adjust height

scaleHeight = true;

} else if (heightMode == MeasureSpec.EXACTLY) {

// Height is fixed, widthMode either AT_MOST or UNSPECIFIED, so adjust width

scaleWidth = true;

} else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {

// Need to fit into box <= [width, height] in size.

// Maximize the View's area while maintaining aspect ratio

// This means keeping one dimension as large as possible and shrinking the other

float boxAspectRatio = width / (float) height;

if (boxAspectRatio > mAspectRatio) {

// Box is wider than requested aspect; pillarbox

scaleWidth = true;

} else {

// Box is narrower than requested aspect; letterbox

scaleHeight = true;

}

} else if (widthMode == MeasureSpec.AT_MOST) {

// Maximize width, heightSpec is UNSPECIFIED

scaleHeight = true;

} else if (heightMode == MeasureSpec.AT_MOST) {

// Maximize height, widthSpec is UNSPECIFIED

scaleWidth = true;

} else {

// Both MeasureSpecs are UNSPECIFIED. This is probably a pathological layout,

// with width == height == 0

// but arbitrarily scale height anyway

scaleHeight = true;

}

// Do the scaling

if (scaleWidth) {

width = (int) (height * mAspectRatio);

} else if (scaleHeight) {

height = (int) (width / mAspectRatio);

}

// Override width/height if needed for EXACTLY and AT_MOST specs

width = View.resolveSizeAndState(width, widthMeasureSpec, 0);

height = View.resolveSizeAndState(height, heightMeasureSpec, 0);

// Finally set the calculated dimensions

setMeasuredDimension(width, height);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值