前言:
最近遇到一个bug,问题描述是这样的:启动页需要放置一张广告图,要使这张图在不变形的情况下(等比例缩放),宽度要占满屏幕宽,于是手动计算并设置ImageView需要的缩放比例来对图片进行缩放。该方法触发的时机引发了一些问题。
private void scaleView(ImageView iv) {
if (iv.getDrawable() == null) {
return;
}
Matrix matrix = iv.getImageMatrix();
float scaleWidth = (float) iv.getWidth() / (float) iv.getDrawable().getIntrinsicWidth();
float scaleHeight = (float) iv.getHeight() / (float) iv.getDrawable().getIntrinsicHeight();
float scaleFactor = scaleWidth > scaleHeight ? scaleWidth : scaleHeight;
matrix.setScale(scaleFactor, scaleFactor, 0.0F, 0.0F);
if (BigDecimal.valueOf((double) scaleFactor).compareTo(BigDecimal.valueOf((double) scaleHeight)) == 0) {
float translateX = ((float) iv.getDrawable().getIntrinsicWidth() * scaleFactor - (float) iv.getWidth())