This file contains hidden or 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
| class MainActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val asyncImageView = findViewById<ImageViewAsync>(R.id.asyncImageView) | |
| asyncImageView.setUrl( | |
| "Some image url" | |
| ) | |
| } |
This file contains hidden or 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
| var counter = 0 | |
| val bitmap = async { | |
| while (counter < 3) { | |
| try { | |
| return@async loadBitmap(url) | |
| } catch (e: Exception) { | |
| e.printStackTrace() | |
| } | |
| counter++ | |
| } |
This file contains hidden or 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
| val hashMap = ConcurrentHashMap<String, Deferred<Bitmap?>>() | |
| //... | |
| val bitmap = async { loadBitmap(url) } | |
| //... | |
| bitmap?.await()?.let { setImageBitmap(it) } |
This file contains hidden or 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
| val hashMap = ConcurrentHashMap<String, Bitmap?>() | |
| job = scope.launch { | |
| val bitmap = if (hashMap.containsKey(url)) { | |
| hashMap[url] | |
| } else { | |
| val bitmap = loadBitmap(url) | |
| if (!hashMap.containsKey(url)) { | |
| hashMap[url] = bitmap | |
| bitmap |
This file contains hidden or 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
| private var job: Job? = null | |
| fun setUrl(url: String) { | |
| job?.cancel() | |
| job = scope.launch { | |
| // ... | |
| } | |
| } |
This file contains hidden or 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
| private val scope = CoroutineScope(Dispatchers.IO) | |
| fun setUrl(url: String) { | |
| scope.launch { | |
| val bitmap = loadBitmap(url) | |
| withContext(Dispatchers.Main) { | |
| setImageBitmap(bitmap) | |
| } | |
| } |
This file contains hidden or 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
| class ImageViewAsync @JvmOverloads constructor( | |
| context: Context, | |
| attrs: AttributeSet? = null, | |
| defStyleAttr: Int = 0 | |
| ) : AppCompatImageView(context, attrs, defStyleAttr) { | |
| fun setUrl(url: String) { | |
| val bitmap = loadBitmap(url) | |
| setImageBitmap(bitmap) | |
| } |
This file contains hidden or 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
| onItemPicked = { | |
| scope.launch { | |
| rotationAnimation.animateTo( | |
| if (it.isDayTime) 0f else 180f, | |
| defaultSpringSpec | |
| ) | |
| } | |
| }, |
This file contains hidden or 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
| val defaultSpringSpec = remember { | |
| FloatSpringSpec( | |
| dampingRatio = Spring.DampingRatioNoBouncy, | |
| stiffness = Spring.StiffnessVeryLow, | |
| ) | |
| } | |
| val rotationAnimation = remember { | |
| Animatable(180f) | |
| } |
This file contains hidden or 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
| @Composable | |
| fun SunMoonComponent( | |
| rotation: Float, | |
| modifier: Modifier = Modifier, | |
| ) { | |
| Box( | |
| modifier = modifier | |
| ) { | |
| Icon( | |
| painter = painterResource(id = R.drawable.icon_house), |
NewerOlder