Browse Source

feat: glide

tongmengxiao 6 months ago
parent
commit
cefc28baa3

+ 10 - 0
baseswago/src/main/java/com/swago/baseswago/PersonDataDFragment.kt

@@ -17,6 +17,7 @@ import com.swago.baseswago.dialog.BaseXDFragment
 import com.swago.baseswago.dialog.HandleDialogFragment
 import com.swago.baseswago.dialog.ReportDialog
 import com.swago.baseswago.util.AppContext
+import com.swago.baseswago.util.BitmapUtils
 import com.swago.baseswago.util.IMUtil
 import com.swago.baseswago.util.NoDoubleClickListener
 import com.swago.baseswago.util.UserInfo
@@ -44,6 +45,9 @@ class PersonDataDFragment : BaseXDFragment<DialogPersonDataBinding>() {
     var openGiftDialog:((nickName:String,userId:String)->Unit)? = null
     var openGiftIconDialog:((nickName:String,userId:String,nickIcon:String)->Unit)? = null
     var sendMsgInRoom:((nickName:String)->Unit)? = null
+    private val badgeBitmap by lazy {
+        BitmapUtils.getBitmapFromPath(UserInfo.getUserInfo()?.badge_special)
+    }
 
     companion object{
         fun newInstance(uid:String,isAnchor:Boolean=false,inRoom:Boolean=false,roomId:String="",isMessage:Boolean=false): PersonDataDFragment {
@@ -237,6 +241,12 @@ class PersonDataDFragment : BaseXDFragment<DialogPersonDataBinding>() {
             isAdmin = it.is_room_admin == 1
         }
 
+        if (badgeBitmap == null){
+            binding.ivBadge.visibility = View.GONE
+        } else {
+            binding.ivBadge.setImageBitmap(badgeBitmap)
+            binding.ivBadge.visibility = View.VISIBLE
+        }
 
         userVm.followStateLiveData.observe(this) {
             if (isFollow == 0) {

+ 1 - 0
baseswago/src/main/java/com/swago/baseswago/UserVm.kt

@@ -46,6 +46,7 @@ class UserVm(application: Application) : BaseViewModel(application) {
         requestData {
             val data = ApiManager.userApi.getUserInfo()
             userInfoLiveData.value = data
+            BitmapUtils.loadFile(AppContext.getContext() , data.badge_special)
         }
     }
 

+ 58 - 0
baseswago/src/main/java/com/swago/baseswago/util/BitmapUtils.kt

@@ -0,0 +1,58 @@
+package com.swago.baseswago.util
+
+import android.app.Activity
+import android.content.Context
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.graphics.drawable.Drawable
+import com.bumptech.glide.Glide
+import com.bumptech.glide.request.target.CustomTarget
+import com.bumptech.glide.request.transition.Transition
+import java.io.File
+import java.io.FileOutputStream
+import java.io.IOException
+
+object BitmapUtils {
+
+    fun getBitmapFromPath(url:String?):Bitmap?{
+            val path = "${AppContext.getContext().getExternalFilesDir(null)!!.path}/swagoFile"
+            val pathFile = File("$path/${url?.substring(url.lastIndexOf("/"))}")
+            return if (pathFile.exists()) {
+                BitmapFactory.decodeFile(pathFile.absolutePath)
+            } else {
+                null
+            }
+
+    }
+
+    fun loadFile(context: Context, url: String,) {
+        if (context is Activity){
+            if (context.isDestroyed)
+                return
+        }
+        Glide.with(context)
+            .asBitmap()
+            .load(url)
+            .override(207,60)
+            .into(object : CustomTarget<Bitmap>() {
+                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
+                    val path = "${AppContext.getContext().getExternalFilesDir(null)!!.path}/swagoFile"
+                    val pathFile = File("$path/${url.substring(url.lastIndexOf("/"))}")
+                    var outputStream: FileOutputStream? = null
+                    try {
+                        outputStream = FileOutputStream(pathFile)
+                        resource.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
+                        outputStream.flush()
+                    } catch (e: IOException) {
+                        e.printStackTrace()
+                    } finally {
+                        outputStream?.close()
+                    }
+                }
+
+                override fun onLoadCleared(placeholder: Drawable?) {
+
+                }
+            })
+    }
+}

+ 3 - 1
baseswago/src/main/java/com/swago/baseswago/util/DianJiuUtil.kt

@@ -10,9 +10,11 @@ import android.widget.Toast
 import com.bumptech.glide.Glide
 import com.bumptech.glide.request.target.CustomTarget
 import com.bumptech.glide.request.transition.Transition
+import java.io.File
+import java.io.FileOutputStream
+import java.io.IOException
 
 object DianJiuUtil {
-
     fun loadDian9Tu(context: Context, url: String, drawable: (e: Drawable?) -> Unit){
         if (context is Activity){
             if (context.isDestroyed)

+ 13 - 2
baseswago/src/main/res/layout/dialog_person_data.xml

@@ -176,11 +176,22 @@
                 app:layout_constraintBottom_toBottomOf="@+id/tvLevelView"
                 app:layout_constraintTop_toTopOf="@+id/tvLevelView"
                 app:layout_constraintStart_toEndOf="@+id/tvAgent"
-               app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintEnd_toStartOf="@+id/ivBadge"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"/>
 
-
+            <ImageView
+                android:id="@+id/ivBadge"
+                android:textColor="#fff"
+                android:layout_marginStart="5dp"
+                android:visibility="gone"
+                tools:visibility="visible"
+                app:layout_constraintBottom_toBottomOf="@+id/tvLevelView"
+                app:layout_constraintTop_toTopOf="@+id/tvLevelView"
+                app:layout_constraintStart_toEndOf="@+id/tvOfficial"
+                app:layout_constraintEnd_toEndOf="parent"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"/>
             <LinearLayout
                 android:id="@+id/llFollow"
                 android:layout_width="wrap_content"

+ 28 - 1
room/src/main/java/com/swago/room/adapter/RoomChatAdapter.kt

@@ -11,6 +11,7 @@ import android.text.style.ImageSpan
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.ImageView
 import android.widget.TextView
 import androidx.constraintlayout.widget.ConstraintLayout
 import androidx.core.content.ContextCompat
@@ -25,6 +26,8 @@ import com.swago.baseswago.model.im.FollowSuccessMsgBean
 import com.swago.baseswago.model.im.IMLevelUpMsgBean
 import com.swago.baseswago.model.im.RoomChatMsgBean
 import com.swago.baseswago.model.im.UserJoinRoomBean
+import com.swago.baseswago.util.BitmapUtils
+import com.swago.baseswago.util.UserInfo
 import com.swago.formatAr
 import com.swago.room.R
 import com.swago.room.adapter.bitmap.CenterAlignImageSpan
@@ -45,9 +48,13 @@ class RoomChatAdapter :
      */
     private val iconBitmapCachePool: HashMap<String, Bitmap> = HashMap()
     private val localImageKey = ArrayList<String>()
+    private val badgeBitmap by lazy {
+        BitmapUtils.getBitmapFromPath(UserInfo.getUserInfo()?.badge_special)
+    }
 
     companion object {
         const val LEVEL_KEY = "level"
+        const val BADGE_KEY = "badge"
         const val PRETTY_KEY = "pretty"//靓号
         const val VIP_KEY = "vip"//每日首冲
         const val IS_OFFICIAL = "official"
@@ -60,6 +67,7 @@ class RoomChatAdapter :
 
     init {
         localImageKey.add(LEVEL_KEY)
+        localImageKey.add(BADGE_KEY)
         localImageKey.add(PRETTY_KEY)
         localImageKey.add(VIP_KEY)
         localImageKey.add(IS_OFFICIAL)
@@ -366,6 +374,13 @@ class RoomChatAdapter :
                             .append("'> ")
                     }
 
+                    if (item is UserJoinRoomBean && badgeBitmap != null) {
+                        count++
+                        sb.append("<img src='" + "file:///xx/")
+                            .append(BADGE_KEY).append(".png")
+                            .append("'> ")
+                    }
+
                     count++
                     sb.append("<img src='" + "file:///xx/")
                         .append(LEVEL_KEY).append(".png")
@@ -526,7 +541,14 @@ class RoomChatAdapter :
                         end,
                         Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                     )
-                }else {
+                }else if (imageSpan.source != null && imageSpan!!.source!!.contains(BADGE_KEY)) {
+                span.setSpan(
+                    setBadge(helper.itemView as ViewGroup),
+                    start,
+                    end,
+                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
+                )
+            }else {
                     span.setSpan(newImageSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                 }
                 span.removeSpan(imageSpan)
@@ -723,6 +745,11 @@ class RoomChatAdapter :
         return CustomImageSpan(mContext, bm, CustomImageSpan.ALIGN_FONTCENTER)
     }
 
+    private fun setBadge(viewGroup: ViewGroup): CustomImageSpan {
+        val imageView = ImageView(mContext)
+        viewGroup.addView(imageView)
+        return CustomImageSpan(mContext, badgeBitmap, CustomImageSpan.ALIGN_FONTCENTER)
+    }
 
     private fun setLevel(iSenderLevel: Int, viewGroup: ViewGroup): CustomImageSpan {
         var level = iSenderLevel.toString()