在google的Demo中是以相机屏幕的短边为基准的,如果要以长边为基准则需要调整一下onMeasure方法中的比较代码。
可参考如下:
// width < height * mRatioWidth / mRatioHeight 以短边为基准进行缩放 // width > height * mRatioWidth / mRatioHeight 以长边为基准进行缩放
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
Log.d(TAG, "onMeasure 0 width=" + width + ",height=" + height);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
// Note
// width < height * mRatioWidth / mRatioHeight 以短边为基准进行缩放
// width > height * mRatioWidth / mRatioHeight 以长边为基准进行缩放
if (width > height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}