tongmengxiao преди 5 месеца
родител
ревизия
84a350f39f
променени са 2 файла, в които са добавени 108 реда и са изтрити 0 реда
  1. 61 0
      baseswago/src/main/java/com/swago/baseswago/util/NinePatchChunk.kt
  2. 47 0
      baseswago/src/main/java/com/swago/glide.kt

+ 61 - 0
baseswago/src/main/java/com/swago/baseswago/util/NinePatchChunk.kt

@@ -0,0 +1,61 @@
+package com.swago.baseswago.util
+
+import android.graphics.Rect
+import java.nio.ByteBuffer
+import java.nio.ByteOrder
+
+class NinePatchChunk {
+    companion object {
+        const val NO_COLOR = 0x00000001
+        const val TRANSPARENT_COLOR = 0x00000000
+
+        private fun readIntArray(data: IntArray, buffer: ByteBuffer) {
+            for (i in data.indices) {
+                data[i] = buffer.int
+            }
+        }
+
+        private fun checkDivCount(length: Int) {
+            if (length == 0 || length and 0x01 != 0) {
+                throw RuntimeException("invalid nine-patch: $length")
+            }
+        }
+
+        fun deserialize(data: ByteArray): NinePatchChunk? {
+            val byteBuffer = ByteBuffer.wrap(data).order(ByteOrder.nativeOrder())
+
+            if (byteBuffer.get() == 0.toByte()) return null // is not serialized
+
+            val chunk = NinePatchChunk()
+            chunk.mDivX = IntArray(byteBuffer.get().toInt())
+            chunk.mDivY = IntArray(byteBuffer.get().toInt())
+            chunk.mColor = IntArray(byteBuffer.get().toInt())
+
+            checkDivCount(chunk.mDivX.size)
+            checkDivCount(chunk.mDivY.size)
+
+            // skip 8 bytes
+            byteBuffer.int
+            byteBuffer.int
+
+            chunk.mPaddings.left = byteBuffer.int
+            chunk.mPaddings.right = byteBuffer.int
+            chunk.mPaddings.top = byteBuffer.int
+            chunk.mPaddings.bottom = byteBuffer.int
+
+            // skip 4 bytes
+            byteBuffer.int
+
+            readIntArray(chunk.mDivX, byteBuffer)
+            readIntArray(chunk.mDivY, byteBuffer)
+            readIntArray(chunk.mColor, byteBuffer)
+
+            return chunk
+        }
+    }
+
+    val mPaddings = Rect()
+    lateinit var mDivX: IntArray
+    lateinit var mDivY: IntArray
+    lateinit var mColor: IntArray
+}

+ 47 - 0
baseswago/src/main/java/com/swago/glide.kt

@@ -1,6 +1,12 @@
 package com.swago
 
+import android.app.Activity
 import android.content.Context
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.graphics.NinePatch
+import android.graphics.drawable.Drawable
+import android.graphics.drawable.NinePatchDrawable
 import android.os.Build
 import android.text.BidiFormatter
 import android.text.TextDirectionHeuristics
@@ -9,8 +15,14 @@ import androidx.annotation.DrawableRes
 import androidx.annotation.RequiresApi
 import com.bumptech.glide.Glide
 import com.bumptech.glide.request.RequestOptions
+import com.bumptech.glide.request.target.CustomTarget
+import com.bumptech.glide.request.transition.Transition
 import com.swago.baseswago.R
 import jp.wasabeef.glide.transformations.BlurTransformation
+import java.io.File
+import java.io.FileInputStream
+import java.io.FileNotFoundException
+import java.io.IOException
 import java.util.*
 
 /**
@@ -72,4 +84,39 @@ fun CharSequence.formatAr(): CharSequence {
         this,
         TextDirectionHeuristics.LOCALE
     )
+}
+
+fun loadDian9Tu(context: Context, imageView: ImageView, url: String){
+    if (context is Activity){
+        if (context.isDestroyed)
+            return
+    }
+    Glide.with(context)
+        .asFile()
+        .load(url)
+        .into(object : CustomTarget<File>() {
+            override fun onResourceReady(resource: File, transition: Transition<in File>?) {
+                try {
+                    FileInputStream(resource).use { inputStream ->
+                        val bitmap = BitmapFactory.decodeStream(inputStream)
+                        imageView.setNinePatchImage(context , bitmap)
+                    }
+                } catch (e: FileNotFoundException) {
+                    e.printStackTrace()
+                } catch (e: IOException) {
+                    e.printStackTrace()
+                }
+            }
+
+            override fun onLoadCleared(placeholder: Drawable?) {
+
+            }
+        })
+}
+
+fun ImageView.setNinePatchImage(context: Context, bitmap: Bitmap) {
+    val chunk = bitmap.ninePatchChunk
+    val ninePatch = NinePatch(bitmap, chunk, null)
+    val ninePatchDrawable = NinePatchDrawable(context.resources, ninePatch)
+    this.setImageDrawable(ninePatchDrawable)
 }