From 675c8cfbb80eb8e3b9bb0854dc676eb7456696c8 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Fri, 3 Apr 2020 16:58:09 +0200 Subject: [PATCH] add support for animated images in more places --- Habitica/res/layout/item_image_row.xml | 2 +- .../android/habitica/ui/AvatarView.kt | 57 +------------ .../habitica/ui/helpers/DataBindingUtils.kt | 81 +++++++++++++++++-- 3 files changed, 77 insertions(+), 63 deletions(-) diff --git a/Habitica/res/layout/item_image_row.xml b/Habitica/res/layout/item_image_row.xml index 7e8fd90d3..7c1a85b17 100644 --- a/Habitica/res/layout/item_image_row.xml +++ b/Habitica/res/layout/item_image_row.xml @@ -13,7 +13,7 @@ android:layout_width="@dimen/gear_image_size" android:layout_height="@dimen/gear_image_size" actualImageScaleType="fitCenter" - android:layout_marginRight="@dimen/row_padding"/> + android:layout_marginEnd="@dimen/row_padding"/> () { override fun onFinalImageSet( id: String?, @@ -382,18 +383,7 @@ class AvatarView : View { return bounds } - private fun getFileName(imageName: String): String { - val name = when { - FILENAME_MAP.containsKey(imageName) -> FILENAME_MAP[imageName] - imageName.startsWith("handleless") -> "chair_$imageName" - else -> imageName - } - return name + if (FILEFORMAT_MAP.containsKey(imageName)) { - "." + FILEFORMAT_MAP[imageName] - } else { - ".png" - } - } + private fun onLayerComplete() { if (numberLayersInProcess.decrementAndGet() == 0) { @@ -528,50 +518,9 @@ class AvatarView : View { companion object { const val IMAGE_URI_ROOT = "https://habitica-assets.s3.amazonaws.com/mobileApp/images/" private const val TAG = "AvatarView" - val FILEFORMAT_MAP: Map - val FILENAME_MAP: Map private val FULL_HERO_RECT = Rect(0, 0, 140, 147) private val COMPACT_HERO_RECT = Rect(0, 0, 114, 114) private val HERO_ONLY_RECT = Rect(0, 0, 90, 90) - init { - val tempMap = HashMap() - tempMap["head_special_1"] = "gif" - tempMap["broad_armor_special_1"] = "gif" - tempMap["slim_armor_special_1"] = "gif" - tempMap["head_special_0"] = "gif" - tempMap["slim_armor_special_0"] = "gif" - tempMap["broad_armor_special_0"] = "gif" - tempMap["weapon_special_critical"] = "gif" - tempMap["weapon_special_0"] = "gif" - tempMap["shield_special_0"] = "gif" - tempMap["Pet-Wolf-Cerberus"] = "gif" - tempMap["armor_special_ks2019"] = "gif" - tempMap["slim_armor_special_ks2019"] = "gif" - tempMap["broad_armor_special_ks2019"] = "gif" - tempMap["eyewear_special_ks2019"] = "gif" - tempMap["head_special_ks2019"] = "gif" - tempMap["shield_special_ks2019"] = "gif" - tempMap["weapon_special_ks2019"] = "gif" - tempMap["Pet-Gryphon-Gryphatrice"] = "gif" - tempMap["Mount_Head_Gryphon-Gryphatrice"] = "gif" - tempMap["Mount_Body_Gryphon-Gryphatrice"] = "gif" - tempMap["background_clocktower"] = "gif" - tempMap["background_airship"] = "gif" - tempMap["background_steamworks"] = "gif" - FILEFORMAT_MAP = Collections.unmodifiableMap(tempMap) - - - val tempNameMap = HashMap() - tempNameMap["head_special_1"] = "ContributorOnly-Equip-CrystalHelmet" - tempNameMap["armor_special_1"] = "ContributorOnly-Equip-CrystalArmor" - tempNameMap["head_special_0"] = "BackerOnly-Equip-ShadeHelmet" - tempNameMap["armor_special_0"] = "BackerOnly-Equip-ShadeArmor" - tempNameMap["shield_special_0"] = "BackerOnly-Shield-TormentedSkull" - tempNameMap["weapon_special_0"] = "BackerOnly-Weapon-DarkSoulsBlade" - tempNameMap["weapon_special_critical"] = "weapon_special_critical" - tempNameMap["Pet-Wolf-Cerberus"] = "Pet-Wolf-Cerberus" - FILENAME_MAP = Collections.unmodifiableMap(tempNameMap) - } } } diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/helpers/DataBindingUtils.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/helpers/DataBindingUtils.kt index 44998cad1..641b8fb89 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/helpers/DataBindingUtils.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/helpers/DataBindingUtils.kt @@ -14,41 +14,48 @@ import com.facebook.common.executors.CallerThreadExecutor import com.facebook.common.references.CloseableReference import com.facebook.datasource.DataSource import com.facebook.drawee.backends.pipeline.Fresco +import com.facebook.drawee.interfaces.DraweeController import com.facebook.drawee.view.SimpleDraweeView import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber import com.facebook.imagepipeline.image.CloseableImage import com.facebook.imagepipeline.request.ImageRequestBuilder import com.habitrpg.android.habitica.R +import java.util.* -fun SimpleDraweeView.loadImage(imageName: String, imageFormat: String = "png") { +fun SimpleDraweeView.loadImage(imageName: String, imageFormat: String? = null) { DataBindingUtils.loadImage(this, imageName, imageFormat) } object DataBindingUtils { fun loadImage(view: SimpleDraweeView?, imageName: String) { - loadImage(view, imageName, "png") + loadImage(view, imageName, null) } - fun loadImage(view: SimpleDraweeView?, imageName: String?, imageFormat: String = "png") { + fun loadImage(view: SimpleDraweeView?, imageName: String?, imageFormat: String? = null) { if (view != null && imageName != null && view.visibility == View.VISIBLE) { - val fullname = "$imageName.$imageFormat" + val fullname = getFullFilename(imageName, imageFormat) if (view.tag == fullname) { return } view.tag = fullname - view.setImageURI("https://habitica-assets.s3.amazonaws.com/mobileApp/images/$fullname") + val controller: DraweeController = Fresco.newDraweeControllerBuilder() + .setUri("https://habitica-assets.s3.amazonaws.com/mobileApp/images/$fullname") + .setAutoPlayAnimations(true) + .setOldController(view.controller) + .build() + view.controller = controller } } fun loadImage(imageName: String, imageResult: (Bitmap) -> Unit) { - loadImage(imageName, "png", imageResult) + loadImage(imageName, null, imageResult) } - fun loadImage(imageName: String, imageFormat: String = "png", imageResult: (Bitmap) -> Unit) { + fun loadImage(imageName: String, imageFormat: String?, imageResult: (Bitmap) -> Unit) { val imageRequest = ImageRequestBuilder - .newBuilderWithSource("https://habitica-assets.s3.amazonaws.com/mobileApp/images/$imageName.$imageFormat".toUri()) + .newBuilderWithSource("https://habitica-assets.s3.amazonaws.com/mobileApp/images/${getFullFilename(imageName, imageFormat)}".toUri()) .build() val imagePipeline = Fresco.getImagePipeline() @@ -68,6 +75,19 @@ object DataBindingUtils { }, CallerThreadExecutor.getInstance()) } + fun getFullFilename(imageName: String, imageFormat: String?): String { + val name = when { + FILENAME_MAP.containsKey(imageName) -> FILENAME_MAP[imageName] + imageName.startsWith("handleless") -> "chair_$imageName" + else -> imageName + } + return name + if (imageFormat == null && FILEFORMAT_MAP.containsKey(imageName)) { + "." + FILEFORMAT_MAP[imageName] + } else { + ".${imageFormat ?: "png"}" + } + } + fun setForegroundTintColor(view: TextView, color: Int) { var thisColor = color if (thisColor > 0) { @@ -105,4 +125,49 @@ object DataBindingUtils { override fun willChangeBounds(): Boolean = true } + + private val FILEFORMAT_MAP: Map + private val FILENAME_MAP: Map + + init { + val tempMap = HashMap() + tempMap["head_special_1"] = "gif" + tempMap["broad_armor_special_1"] = "gif" + tempMap["slim_armor_special_1"] = "gif" + tempMap["head_special_0"] = "gif" + tempMap["slim_armor_special_0"] = "gif" + tempMap["broad_armor_special_0"] = "gif" + tempMap["weapon_special_critical"] = "gif" + tempMap["weapon_special_0"] = "gif" + tempMap["shield_special_0"] = "gif" + tempMap["Pet-Wolf-Cerberus"] = "gif" + tempMap["armor_special_ks2019"] = "gif" + tempMap["slim_armor_special_ks2019"] = "gif" + tempMap["broad_armor_special_ks2019"] = "gif" + tempMap["eyewear_special_ks2019"] = "gif" + tempMap["head_special_ks2019"] = "gif" + tempMap["shield_special_ks2019"] = "gif" + tempMap["weapon_special_ks2019"] = "gif" + tempMap["Pet-Gryphon-Gryphatrice"] = "gif" + tempMap["Mount_Head_Gryphon-Gryphatrice"] = "gif" + tempMap["Mount_Body_Gryphon-Gryphatrice"] = "gif" + tempMap["background_clocktower"] = "gif" + tempMap["background_airship"] = "gif" + tempMap["Pet_HatchingPotion_Veggie"] = "gif" + tempMap["Pet_HatchingPotion_Dessert"] = "gif" + tempMap["Pet-HatchingPotion-Dessert"] = "gif" + FILEFORMAT_MAP = Collections.unmodifiableMap(tempMap) + + + val tempNameMap = HashMap() + tempNameMap["head_special_1"] = "ContributorOnly-Equip-CrystalHelmet" + tempNameMap["armor_special_1"] = "ContributorOnly-Equip-CrystalArmor" + tempNameMap["head_special_0"] = "BackerOnly-Equip-ShadeHelmet" + tempNameMap["armor_special_0"] = "BackerOnly-Equip-ShadeArmor" + tempNameMap["shield_special_0"] = "BackerOnly-Shield-TormentedSkull" + tempNameMap["weapon_special_0"] = "BackerOnly-Weapon-DarkSoulsBlade" + tempNameMap["weapon_special_critical"] = "weapon_special_critical" + tempNameMap["Pet-Wolf-Cerberus"] = "Pet-Wolf-Cerberus" + FILENAME_MAP = Collections.unmodifiableMap(tempNameMap) + } }