From ba79d009e6459afb02f59c31b2ebe281a33cc905 Mon Sep 17 00:00:00 2001 From: juntao Date: Fri, 18 Sep 2020 09:56:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=80=E8=88=AC=E9=9D=99?= =?UTF-8?q?=E6=80=81=E5=9B=BE=E7=89=87=E7=9A=84=E5=8A=A0=E8=BD=BD=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../structure/FixedSizeLinkedHashSet.kt | 16 +++++ .../java/com/gh/common/util/DeviceUtils.java | 1 - .../java/com/gh/common/util/ImageUtils.kt | 68 +++++++++++++------ .../java/com/gh/common/view/GameIconView.kt | 6 ++ .../main/java/com/halo/assistant/HaloApp.java | 3 + app/src/main/res/layout/gamedetail_body.xml | 3 +- app/src/main/res/values/attrs.xml | 7 +- 7 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/gh/common/structure/FixedSizeLinkedHashSet.kt diff --git a/app/src/main/java/com/gh/common/structure/FixedSizeLinkedHashSet.kt b/app/src/main/java/com/gh/common/structure/FixedSizeLinkedHashSet.kt new file mode 100644 index 0000000000..f7bd575bcc --- /dev/null +++ b/app/src/main/java/com/gh/common/structure/FixedSizeLinkedHashSet.kt @@ -0,0 +1,16 @@ +package com.gh.common.structure + +class FixedSizeLinkedHashSet(var maxSize: Int) : LinkedHashSet() { + override fun add(element: T): Boolean { + if (size == maxSize) { + pop() + } + return super.add(element); + } + + private fun pop() { + if (size > 0) { + remove(iterator().next()) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/gh/common/util/DeviceUtils.java b/app/src/main/java/com/gh/common/util/DeviceUtils.java index 2822eee40b..d3c25e8164 100644 --- a/app/src/main/java/com/gh/common/util/DeviceUtils.java +++ b/app/src/main/java/com/gh/common/util/DeviceUtils.java @@ -273,7 +273,6 @@ public class DeviceUtils { return memInfo.totalMem / (1024 * 1024); } - // 只能获取WiFi的IpAddress public static String getCurrentIpAddress() { String ipAddress = "0.0.0.0"; diff --git a/app/src/main/java/com/gh/common/util/ImageUtils.kt b/app/src/main/java/com/gh/common/util/ImageUtils.kt index 16acbc0d4e..ccf4ecc5ce 100644 --- a/app/src/main/java/com/gh/common/util/ImageUtils.kt +++ b/app/src/main/java/com/gh/common/util/ImageUtils.kt @@ -19,6 +19,7 @@ import com.facebook.drawee.view.SimpleDraweeView import com.facebook.imagepipeline.image.ImageInfo import com.facebook.imagepipeline.request.ImageRequest import com.gh.common.constant.Config +import com.gh.common.structure.FixedSizeLinkedHashSet import com.gh.gamecenter.R import com.halo.assistant.HaloApp import com.squareup.picasso.Picasso @@ -27,7 +28,6 @@ import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers import java.io.ByteArrayOutputStream - object ImageUtils { private const val PIC_MAX_FILE_SIZE: Long = 10 * 1024 * 1024 @@ -36,6 +36,8 @@ object ImageUtils { private val LARGE_GIF_SIZE = 80F.dip2px() private val STANDARD_GIF_SIZE = 60F.dip2px() + private val mImageUrlCacheSet by lazy { FixedSizeLinkedHashSet(maxSize = 200) } + @JvmStatic fun getUploadFileMaxSize(): Long { val uploadLimitSize = Config.getSettings()?.image?.uploadLimitSize @@ -246,33 +248,53 @@ object ImageUtils { @JvmStatic fun display(view: SimpleDraweeView?, url: String?) { url?.let { - // 图片是以 gif 结尾的就 + + val width = view?.layoutParams?.width + val height = view?.layoutParams?.height + + // 是否加载 gif if (it.endsWith(".gif") && view?.getTag(R.id.tag_show_gif) != false) { if (view?.tag == url) return@let - val width = view?.layoutParams?.width - val height = view?.layoutParams?.height - if (width != null && width > 0) { + + val loadGifClosure: (url: String) -> Unit = { u -> val controller = Fresco.newDraweeControllerBuilder() - .setUri(resizeGif(url, width, height ?: 0)) + .setUri(u) .setAutoPlayAnimations(true) .build() - view.controller = controller + view?.controller = controller + } + if (width != null && width > 0) { + loadGifClosure.invoke(resizeGif(url, width, height ?: 0)) } else { - view?.post { - val controller = Fresco.newDraweeControllerBuilder() - .setUri(resizeGif(url, view.width, view.height)) - .setAutoPlayAnimations(true) - .build() - view.controller = controller - } + view?.post { loadGifClosure.invoke(resizeGif(url, view.width, view.height)) } } } else { - val width = view?.layoutParams?.width + var highResUrl = "" + var lowResUrl = "" + + // 找同一图片地址已加载过的图片作为低质量预览图 + // TODO 根据实际请求大小(w_width)来避免小图用大图作为低质量图片 + for (cachedImageUrl in mImageUrlCacheSet) { + if (cachedImageUrl.contains(url)) { + lowResUrl = cachedImageUrl + break + } + } + + val loadStaticPicClosure: (highResUrl: String, lowResUrl: String) -> Unit = { hUrl, lUrl -> + view?.controller = Fresco.newDraweeControllerBuilder() + .setImageRequest(ImageRequest.fromUri(hUrl)) + .apply { if (lUrl.isNotEmpty() && lUrl != hUrl) lowResImageRequest = ImageRequest.fromUri(lUrl) } + .build() + } + if (width != null && width > 0) { - view.setImageURI(getTransformLimitUrl(url, width, view.context)) + highResUrl = getTransformLimitUrl(url, width, view.context) ?: "" + loadStaticPicClosure.invoke(highResUrl, lowResUrl) } else { view?.post { - view.setImageURI(getTransformLimitUrl(url, view.width, view.context)) + highResUrl = getTransformLimitUrl(url, view.width, view.context) ?: "" + loadStaticPicClosure.invoke(highResUrl, lowResUrl) } } } @@ -290,7 +312,7 @@ object ImageUtils { // 当网络为 WIFI 或 4G, 且系统版本大于 5.0 && 手机内存大于 1G 才用高清图片 if (NetworkUtils.isWifiOr4GConnected(context) && Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP - && DeviceUtils.getTotalRamSizeOfDevice(context) > 1000) { + && HaloApp.getInstance().deviceRamSize > 1024) { transformUrl = transformUrlX2 } else { // 检查X2大图是否被缓存 @@ -302,7 +324,7 @@ object ImageUtils { } } } -// Utils.log("displayPost::viewWidth->$width----transformUrl->$transformUrl") + addCachedUrl(transformUrl ?: "") return transformUrl } @@ -312,15 +334,19 @@ object ImageUtils { val width = view?.layoutParams?.width if (width != null && width > 0) { view.setImageURI(addLimitWidth(url, width * 2)) -// Utils.log("displayIcon::viewWidth->" + view.width + "---transformUrl->" + transformUrl) } else { view?.post { view.setImageURI(addLimitWidth(url, view.width * 2)) -// Utils.log("displayIcon::viewWidth->" + view.width + "---transformUrl->" + transformUrl) } } } + private fun addCachedUrl(url: String) { + if (url.startsWith("http")) { + mImageUrlCacheSet.add(url) + } + } + @JvmStatic fun bmpToByteArray(bmp: Bitmap, needRecycle: Boolean): ByteArray { val output = ByteArrayOutputStream() diff --git a/app/src/main/java/com/gh/common/view/GameIconView.kt b/app/src/main/java/com/gh/common/view/GameIconView.kt index e3d1f19f1c..3f1c207b0d 100644 --- a/app/src/main/java/com/gh/common/view/GameIconView.kt +++ b/app/src/main/java/com/gh/common/view/GameIconView.kt @@ -19,6 +19,7 @@ class GameIconView : ConstraintLayout { private var mCornerRadius = 10 private var mBorderColor = 0 private var mBorderWidth = 1 + private var mFadeDuration = -1 constructor(context: Context) : super(context, null) constructor(context: Context, attrs: AttributeSet) : super(context, attrs, 0) { @@ -36,6 +37,7 @@ class GameIconView : ConstraintLayout { mCornerRadius = ta.getDimensionPixelSize(R.styleable.GameIconView_gameIconCornerRadius, DisplayUtils.dip2px(10F)) mBorderColor = ta.getColor(R.styleable.GameIconView_gameIconBorderColor, 0) mBorderWidth = ta.getDimensionPixelSize(R.styleable.GameIconView_gameIconBorderWidth, 1) + mFadeDuration = ta.getInt(R.styleable.GameIconView_gameIconFadeDuration, -1) ta.recycle() val roundingParams = RoundingParams.fromCornersRadius(mCornerRadius.toFloat()) @@ -45,6 +47,10 @@ class GameIconView : ConstraintLayout { } gameIconIv.hierarchy.roundingParams = roundingParams gameIconDecoratorIv.hierarchy.roundingParams = roundingParams + + if (mFadeDuration != -1) { + gameIconIv.hierarchy.fadeDuration = mFadeDuration + } } fun displayGameIcon(game: GameEntity) { diff --git a/app/src/main/java/com/halo/assistant/HaloApp.java b/app/src/main/java/com/halo/assistant/HaloApp.java index 0a3792652b..2f76fab0b9 100644 --- a/app/src/main/java/com/halo/assistant/HaloApp.java +++ b/app/src/main/java/com/halo/assistant/HaloApp.java @@ -66,6 +66,7 @@ public class HaloApp extends TinkerAppLike { private String mUA; private String mOAID = ""; + public long deviceRamSize = 0; public boolean isBrandNewInstall = false; // 当前用户是否是安装光环后第一次打开 public boolean isRunningForeground = false; // 标记当前 APP 是否处于前台运行中 @@ -132,6 +133,8 @@ public class HaloApp extends TinkerAppLike { mInstance = this; + deviceRamSize = DeviceUtils.getTotalRamSizeOfDevice(getApplication()); + mChannel = ChannelReaderUtil.getChannel(getApplication()); if (mChannel == null || TextUtils.isEmpty(mChannel.trim())) { mChannel = Config.DEFAULT_CHANNEL; diff --git a/app/src/main/res/layout/gamedetail_body.xml b/app/src/main/res/layout/gamedetail_body.xml index 4277730ce9..c7d7fe2387 100644 --- a/app/src/main/res/layout/gamedetail_body.xml +++ b/app/src/main/res/layout/gamedetail_body.xml @@ -56,7 +56,8 @@ android:layout_width="88dp" android:layout_height="88dp" android:layout_centerHorizontal="true" - app:gameIconCornerRadius="20dp" /> + app:gameIconCornerRadius="20dp" + app:gameIconFadeDuration="0" /> + @@ -130,12 +131,12 @@ - - + + - + \ No newline at end of file