DownloadManager.getInstance().downloadImage(data[0].pictureUrl, object : OnDownloadListener() {
override fun onDownloadSuccess(filePath: String?) {
val screenWidth = this@LoadingActivity.resources.displayMetrics.widthPixels
val screenHeight = this@LoadingActivity.resources.displayMetrics.heightPixels
val bitmap = BitmapFactory.decodeFile(filePath)
val bitmapWith = bitmap.width
val bitmapHeight = bitmap.height
val screenRate = ((screenHeight.toFloat() / screenWidth.toFloat()) * 100) / 100
val bitmapRate = ((bitmapHeight.toFloat() / bitmapWith.toFloat()) * 100) / 100
if (bitmapRate > screenRate) {
val witdhRate = ((screenWidth.toFloat() / bitmapWith.toFloat()) * 100) / 100
val finalheight = (bitmapHeight * witdhRate).toInt()
var tempBimap = Bitmap.createScaledBitmap(bitmap, screenWidth, finalheight, true)
var finalBitmap = Bitmap.createBitmap(tempBimap, 0, 0, screenWidth.toInt(), screenHeight.toInt())
siv_Daily_Message.setImageBitmap(finalBitmap)
} else if (bitmapRate < screenRate) {
val heightRate = ((screenHeight.toFloat() / bitmapHeight.toFloat()) * 100) / 100
求出图片需要的宽度
val finalwidth = (bitmapWith * heightRate).toInt()
var tempBimap = Bitmap.createScaledBitmap(bitmap, finalwidth, screenHeight, true)
var startx = (finalwidth - screenWidth) / 2
、、用屏幕的宽高,截取图片的中间,获取到最终图片
var finalBitmap = Bitmap.createBitmap(tempBimap, startx, 0, screenWidth.toInt(), screenHeight.toInt())
siv_Daily_Message.setImageBitmap(finalBitmap)
} else {
siv_Daily_Message.setImageBitmap(bitmap)
}
}
override fun onDownloadFailed(e: Exception?) {
e?.printStackTrace()
}
})