-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add per-widget configuration (#333)
* feat: add per-widget configuration * fix: no need to overengineer it * feat: add cache to bitmap download dfgdfg * fix: elvis has left the operation
- Loading branch information
TwistedUmbrellaX
committed
Apr 8, 2024
1 parent
47d05e7
commit f96d2ff
Showing
5 changed files
with
74 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,62 @@ | ||
package ani.dantotsu.util | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.graphics.BitmapShader | ||
import android.graphics.Canvas | ||
import android.graphics.Paint | ||
import android.graphics.RectF | ||
import android.graphics.Shader | ||
import android.graphics.drawable.Drawable | ||
import androidx.collection.LruCache | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.runBlocking | ||
import java.io.InputStream | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
class BitmapUtil { | ||
companion object { | ||
fun roundCorners(bitmap: Bitmap, cornerRadius: Float = 20f): Bitmap { | ||
val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) | ||
val canvas = Canvas(output) | ||
val paint = Paint() | ||
paint.isAntiAlias = true | ||
paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) | ||
val rect = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat()) | ||
canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint) | ||
object BitmapUtil { | ||
private fun roundCorners(bitmap: Bitmap, cornerRadius: Float = 20f): Bitmap { | ||
val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) | ||
val canvas = Canvas(output) | ||
val paint = Paint() | ||
paint.isAntiAlias = true | ||
paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) | ||
val rect = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat()) | ||
canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint) | ||
|
||
return output | ||
} | ||
return output | ||
} | ||
|
||
private val cacheSize = (Runtime.getRuntime().maxMemory() / 1024 / 16).toInt() | ||
private val bitmapCache = LruCache<String, Bitmap>(cacheSize) | ||
|
||
fun downloadImageAsBitmap(imageUrl: String): Bitmap? { | ||
var bitmap: Bitmap? = null | ||
|
||
runBlocking(Dispatchers.IO) { | ||
val cacheName = imageUrl.substringAfterLast("/") | ||
bitmap = bitmapCache[cacheName] | ||
if (bitmap != null) return@runBlocking | ||
var inputStream: InputStream? = null | ||
var urlConnection: HttpURLConnection? = null | ||
try { | ||
val url = URL(imageUrl) | ||
urlConnection = url.openConnection() as HttpURLConnection | ||
urlConnection.requestMethod = "GET" | ||
urlConnection.connect() | ||
|
||
fun convertDrawableToBitmap(drawable: Drawable, width: Int, height: Int): Bitmap { | ||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | ||
val canvas = Canvas(bitmap) | ||
drawable.setBounds(0, 0, canvas.width, canvas.height) | ||
drawable.draw(canvas) | ||
return bitmap | ||
if (urlConnection.responseCode == HttpURLConnection.HTTP_OK) { | ||
inputStream = urlConnection.inputStream | ||
bitmap = BitmapFactory.decodeStream(inputStream) | ||
bitmap?.let { bitmapCache.put(cacheName, it) } | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} finally { | ||
inputStream?.close() | ||
urlConnection?.disconnect() | ||
} | ||
} | ||
return bitmap?.let { roundCorners(it) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f96d2ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the optimize imports didn't carry over