我必须动态地在布局中插入图像。当图像的宽度或高度大于布局时,图像超出布局边界。 我不想显示超出布局的图像。请帮助我。 我的代码:
ComputeCarpetSize computeCarpetSize = new ComputeCarpetSize();
float[] size = computeCarpetSize.newSize(workSpaceWidth, workSpaceHeight, sizeWidth, sizeHeight);
int divScalex=ViewUtil.dpToPx((int) Math.round(workSpaceWidth * image.getScaleX()))/ViewUtil.dpToPx((int) Math.round(size[0]));
int divScaley=ViewUtil.dpToPx((int) Math.round(workSpaceHeight * image.getScaleY()))/ViewUtil.dpToPx((int) Math.round(size[1]));
ViewGroup.LayoutParams params = imageView.getLayoutParams();
if (params != null) {
if (sizeWidth > ViewUtil.dpToPx(workSpaceWidth)) {
params.width = ViewUtil.dpToPx((int) Math.round(size[0]));
imageView.setScaleX(divScalex);
} else {
params.width = ViewUtil.dpToPx((int) Math.round(workSpaceWidth * image.getScaleX()));
}
if (sizeHeight > ViewUtil.dpToPx(workSpaceHeight)) {
params.height = ViewUtil.dpToPx((int) Math.round(size[1]));
imageView.setScaleY(divScaley);
} else {
params.height = ViewUtil.dpToPx((int) Math.round(workSpaceHeight * image.getScaleY()));
}
imageView.setLayoutParams(params);
} else {
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (sizeWidth > ViewUtil.dpToPx(workSpaceWidth)) {
params.width = ViewUtil.dpToPx((int) Math.round(size[0]));
imageView.setScaleX(divScalex);
} else {
params.width = ViewUtil.dpToPx((int) Math.round(workSpaceWidth * image.getScaleX()));
}
if (sizeHeight > ViewUtil.dpToPx(workSpaceHeight)) {
params.height = ViewUtil.dpToPx((int) Math.round(size[1]));
imageView.setScaleY(divScaley);
} else {
params.height = ViewUtil.dpToPx((int) Math.round(workSpaceHeight * image.getScaleY()));
}
imageView.setLayoutParams(params);
}