Open loyilin opened 8 months ago
原来是获取到图片宽高不对,导致缩放比例也不对,只要把getDrawable改成getBitmap就行了,至于为啥不能获取到正确宽高就不知道了,有了解的可以解解惑
private float calcScale(int viewWidth, int viewHeight, float angle) { mImgWidth = getBitmap().getWidth(); mImgHeight = getBitmap().getHeight(); if (mImgWidth <= 0) mImgWidth = viewWidth; if (mImgHeight <= 0) mImgHeight = viewHeight; float viewRatio = (float) viewWidth / (float) viewHeight; float imgRatio = getRotatedWidth(angle) / getRotatedHeight(angle); float scale = 1.0f; if (imgRatio >= viewRatio) { scale = viewWidth / getRotatedWidth(angle); } else if (imgRatio < viewRatio) { scale = viewHeight / getRotatedHeight(angle); } return scale; }
因为在android.widget.ImageView#configureBounds里面会对Drawable进行修改bounds进行缩放,但是CropView没有用ImageView缩放后的drawable,而是自己处理了缩放逻辑,同时计算宽高又用了ImageView缩放后的drawable宽高进行处理,所以有这个问题
原来是获取到图片宽高不对,导致缩放比例也不对,只要把getDrawable改成getBitmap就行了,至于为啥不能获取到正确宽高就不知道了,有了解的可以解解惑
private float calcScale(int viewWidth, int viewHeight, float angle) { mImgWidth = getBitmap().getWidth(); mImgHeight = getBitmap().getHeight(); if (mImgWidth <= 0) mImgWidth = viewWidth; if (mImgHeight <= 0) mImgHeight = viewHeight; float viewRatio = (float) viewWidth / (float) viewHeight; float imgRatio = getRotatedWidth(angle) / getRotatedHeight(angle); float scale = 1.0f; if (imgRatio >= viewRatio) { scale = viewWidth / getRotatedWidth(angle); } else if (imgRatio < viewRatio) { scale = viewHeight / getRotatedHeight(angle); } return scale; }