배경화면 이미지 디코딩 시 다운샘플링(inSampleSize) 및 비트맵 해제 적용
- ForeGroundService, Appearances: 원본 해상도 전체 디코딩 대신 목표 화면 해상도 기준 inSampleSize 계산 후 디코딩 - WallpaperManager.setBitmap() 적용 완료 후 임시 비트맵 즉시 recycle() 처리
This commit is contained in:
@@ -204,13 +204,25 @@ class WallpaperAutoChangeWorker(private val context: Context, params: WorkerPara
|
||||
return Result.failure()
|
||||
}
|
||||
|
||||
// 2. 필터링된 이미지 중 랜덤 선택 및 비트맵 로드
|
||||
// 2. 필터링된 이미지 중 랜덤 선택 및 다운샘플링 비트맵 로드
|
||||
val randomImage = validImages.random()
|
||||
val originalBitmap = BitmapFactory.decodeFile(randomImage.absolutePath) ?: return Result.failure()
|
||||
val boundsOptions = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeFile(randomImage.absolutePath, boundsOptions)
|
||||
var inSampleSize = 1
|
||||
if (boundsOptions.outHeight > minHeight || boundsOptions.outWidth > minWidth) {
|
||||
val halfHeight = boundsOptions.outHeight / 2
|
||||
val halfWidth = boundsOptions.outWidth / 2
|
||||
while ((halfHeight / inSampleSize) >= minHeight && (halfWidth / inSampleSize) >= minWidth) {
|
||||
inSampleSize *= 2
|
||||
}
|
||||
}
|
||||
val decodeOptions = BitmapFactory.Options().apply { this.inSampleSize = inSampleSize }
|
||||
val originalBitmap = BitmapFactory.decodeFile(randomImage.absolutePath, decodeOptions) ?: return Result.failure()
|
||||
|
||||
var finalBitmap: Bitmap? = null
|
||||
return try {
|
||||
// 3. 비율 유지하며 Center Crop 처리 (기존 로직 유지)
|
||||
val finalBitmap = Bitmap.createBitmap(minWidth, minHeight, Bitmap.Config.ARGB_8888)
|
||||
finalBitmap = Bitmap.createBitmap(minWidth, minHeight, Bitmap.Config.ARGB_8888)
|
||||
val canvas = Canvas(finalBitmap)
|
||||
|
||||
val scale = Math.max(minWidth.toFloat() / originalBitmap.width, minHeight.toFloat() / originalBitmap.height)
|
||||
@@ -227,6 +239,9 @@ class WallpaperAutoChangeWorker(private val context: Context, params: WorkerPara
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Result.retry()
|
||||
} finally {
|
||||
if (!originalBitmap.isRecycled) originalBitmap.recycle()
|
||||
if (finalBitmap != null && !finalBitmap.isRecycled) finalBitmap.recycle()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,12 +178,27 @@ internal class Appearances : BottomSheetDialogFragment() {
|
||||
val index = cursor!!.getColumnIndex(projection[0])
|
||||
val filePath = cursor.getString(index)
|
||||
cursor.close()
|
||||
val bitmap = BitmapFactory.decodeFile(filePath)
|
||||
val wm = WallpaperManager.getInstance(requireContext())
|
||||
val reqWidth = wm.desiredMinimumWidth.takeIf { it > 0 } ?: 1080
|
||||
val reqHeight = wm.desiredMinimumHeight.takeIf { it > 0 } ?: 1920
|
||||
val boundsOptions = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeFile(filePath, boundsOptions)
|
||||
var inSampleSize = 1
|
||||
if (boundsOptions.outHeight > reqHeight || boundsOptions.outWidth > reqWidth) {
|
||||
val halfHeight = boundsOptions.outHeight / 2
|
||||
val halfWidth = boundsOptions.outWidth / 2
|
||||
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
|
||||
inSampleSize *= 2
|
||||
}
|
||||
}
|
||||
val bitmap = BitmapFactory.decodeFile(filePath, BitmapFactory.Options().apply {
|
||||
this.inSampleSize = inSampleSize
|
||||
})
|
||||
val matrix = Matrix()
|
||||
matrix.postRotate(0F)
|
||||
try {
|
||||
if (bitmap != null) {
|
||||
WallpaperManager.getInstance(requireContext()).setBitmap(bitmap)
|
||||
wm.setBitmap(bitmap)
|
||||
Toast.makeText(requireContext(),
|
||||
requireActivity().getString(R.string.wallpaper_change_success), Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
@@ -194,6 +209,8 @@ internal class Appearances : BottomSheetDialogFragment() {
|
||||
e.printStackTrace()
|
||||
Toast.makeText(requireContext(),
|
||||
requireActivity().getString(R.string.something_went_wrong), Toast.LENGTH_SHORT).show()
|
||||
} finally {
|
||||
if (bitmap != null && !bitmap.isRecycled) bitmap.recycle()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
|
||||
Reference in New Issue
Block a user