欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > Android Glide批量加载Bitmap,拼接组装大Bitmap,更新单个AppCompatImageView,Kotlin(3)

Android Glide批量加载Bitmap,拼接组装大Bitmap,更新单个AppCompatImageView,Kotlin(3)

2024/11/30 6:55:58 来源:https://blog.csdn.net/zhangphil/article/details/144120564  浏览:    关键词:Android Glide批量加载Bitmap,拼接组装大Bitmap,更新单个AppCompatImageView,Kotlin(3)

Android Glide批量加载Bitmap,拼接组装大Bitmap,更新单个AppCompatImageView,Kotlin(3)

这篇 Android Glide批量加载Bitmap,拼接组装大Bitmap,更新单个AppCompatImageView,Kotlin(2)-CSDN博客

遗留两个问题:

(1)当最后一行不满ROW_SIZE时候,根本就不会显示。

(2)即便显示出来,因为绘制是按照ROW_SIZE绘满一行,导致实际不满一行的位置显示为灰色占位颜色块。

以上问题均是在 BatchBitmapView,改造:

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Targetclass BatchBitmapView @JvmOverloads constructor(val mCtx: Context,attributeSet: AttributeSet? = null,defStyleAttr: Int = 0
) : AppCompatImageView(mCtx, attributeSet, defStyleAttr) {private val mData = mutableListOf<DataBean>()private val mTargets = mutableListOf<Target<Bitmap>>()private var mRealSize: Int = 0companion object {const val TAG = "fly/BatchBitmapView"const val ROW_SIZE = 16 //一行多少个bitmapconst val IMAGE_SIZE = 200 //每个小格子图片的尺寸var mScreenWidth: Int = 0var mScreenHeight: Int = 0//整数相除,精度损失的平衡因子const val BALANCE_FACTOR = 1}init {//scaleType = ScaleType.FIT_CENTERif (mScreenWidth == 0) {mScreenWidth = resources.displayMetrics.widthPixels}if (mScreenHeight == 0) {mScreenHeight = resources.displayMetrics.widthPixels / ROW_SIZE + BALANCE_FACTOR}}fun setRowBitmapData(rows: ArrayList<MainActivity.MyData>?, position: Int) {mData.clear()mTargets.forEach {//如果不清除,会发生有些图错放位置。Glide.with(mCtx).clear(it)}mTargets.clear()var loadCount = 0mRealSize = rows?.size!!rows?.forEachIndexed { _, myData ->val target = Glide.with(mCtx).asBitmap().centerCrop().override(IMAGE_SIZE).load(myData.path).addListener(object : RequestListener<Bitmap> {override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>, isFirstResource: Boolean): Boolean {loadCount++val errorBmp = BitmapFactory.decodeResource(mCtx.resources, android.R.drawable.stat_notify_error)refresh(loadCount, errorBmp)return false}override fun onResourceReady(resource: Bitmap,model: Any,target: Target<Bitmap>?,dataSource: DataSource,isFirstResource: Boolean): Boolean {loadCount++refresh(loadCount, resource)return false}}).preload(IMAGE_SIZE, IMAGE_SIZE)mTargets.add(target)}}fun refresh(loadCount: Int, bmp: Bitmap) {val bean = DataBean(bmp)mData.add(bean)if (loadCount == mRealSize) {val jBmp = joinBitmap()(mCtx as AppCompatActivity).runOnUiThread {this@BatchBitmapView.setImageBitmap(jBmp)}}}private fun joinBitmap(): Bitmap {val bmp = Bitmap.createBitmap(IMAGE_SIZE * mRealSize, IMAGE_SIZE, Bitmap.Config.ARGB_8888)val canvas = Canvas(bmp)canvas.drawColor(Color.LTGRAY)mData.forEachIndexed { idx, dataBean ->canvas.drawBitmap(dataBean.bitmap, IMAGE_SIZE * idx.toFloat(), 0f, null)}return bmp}data class DataBean(val bitmap: Bitmap)override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)if (mRealSize == ROW_SIZE) {setMeasuredDimension(mScreenWidth, mScreenHeight)} else {val w = (mScreenWidth / ROW_SIZE) * mRealSizesetMeasuredDimension(w, mScreenHeight)}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com