当前位置: 首页 > wzjs >正文

苏州网站维护济南网络优化网址

苏州网站维护,济南网络优化网址,建立自己的网站需要多少钱,重庆建设信息网站查询Android Coil3 Fetcher preload批量Bitmap拼接扁平宽图,Kotlin 在这一篇文章基础上改进: Android Coil3阶梯preload批量Bitmap拼接扁平宽图,Kotlin-CSDN博客文章浏览阅读854次,点赞18次,收藏5次。遗留问题&#xff0c…

Android Coil3 Fetcher preload批量Bitmap拼接扁平宽图,Kotlin

在这一篇文章基础上改进:

Android Coil3阶梯preload批量Bitmap拼接扁平宽图,Kotlin-CSDN博客文章浏览阅读854次,点赞18次,收藏5次。遗留问题,配置的disk cache似乎没有work,指定的磁盘缓存文件路径生成是生成了,但是app跑起来运行后(图正常显示),里面是空的。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。 https://blog.csdn.net/zhangphil/article/details/146353189

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /><uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

    implementation("io.coil-kt.coil3:coil:3.1.0")implementation("io.coil-kt.coil3:coil-gif:3.1.0")implementation("io.coil-kt.coil3:coil-core:3.1.0")

import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil3.ImageLoader
import coil3.memory.MemoryCache
import coil3.request.ImageRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launchclass MainActivity : AppCompatActivity() {companion object {const val THUMB_WIDTH = 150const val THUMB_HEIGHT = 150const val ROW_SIZE = 16const val TAG = "fly/MainActivity"}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val rv = findViewById<RecyclerView>(R.id.rv)val layoutManager = LinearLayoutManager(this)layoutManager.orientation = LinearLayoutManager.VERTICALval imageLoader = MyCoilManager.INSTANCE.getImageLoader(applicationContext)val adapter = MyAdapter(this, imageLoader)rv.adapter = adapterrv.layoutManager = layoutManagerrv.setItemViewCacheSize(ROW_SIZE)rv.recycledViewPool.setMaxRecycledViews(0, ROW_SIZE)val ctx = thislifecycleScope.launch(Dispatchers.IO) {val imgList = readAllImage(ctx)val videoList = readAllVideo(ctx)Log.d(TAG, "readAllImage size=${imgList.size}")Log.d(TAG, "readAllVideo size=${videoList.size}")val lists = arrayListOf<MyData>()lists.addAll(imgList)lists.addAll(videoList)val total = lists.sizeLog.d(TAG, "总数量=$total")lists.shuffle()val items = sliceDataList(lists)lifecycleScope.launch(Dispatchers.Main) {adapter.dataChanged(items)}val PRELOAD = trueif (PRELOAD) {val probability = 0.85fval from = 500lists.forEachIndexed { idx, myData ->if (idx > from && (Math.random() <= probability)) {Log.d(TAG, "$idx/$total preload")preload(imageLoader, myData)}}}}}private fun preload(imageLoader: ImageLoader, myData: MyData) {val thumbItem = Item(uri = myData.uri, path = myData.path)thumbItem.type = Item.THUMBval thumbMemoryCacheKey = MemoryCache.Key(thumbItem.toString())val thumbMemoryCache = MyCoilManager.INSTANCE.getMemoryCache(thumbMemoryCacheKey)if (thumbMemoryCache == null) {val thumbReq = ImageRequest.Builder(this).data(thumbItem).size(THUMB_WIDTH, THUMB_HEIGHT).memoryCacheKey(thumbMemoryCacheKey).build()imageLoader.enqueue(thumbReq)}}class MyData(var path: String, var uri: Uri)private fun sliceDataList(data: ArrayList<MyData>): ArrayList<ArrayList<MyData>> {var k: Intval lists = ArrayList<ArrayList<MyData>>()for (i in data.indices step ROW_SIZE) {val temp = ArrayList<MyData>()k = 0for (j in 0 until ROW_SIZE) {k = i + jif (k >= data.size) {break}temp.add(data[k])}lists.add(temp)}return lists}private fun readAllImage(ctx: Context): ArrayList<MyData> {val photos = ArrayList<MyData>()//读取所有图val cursor = ctx.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null)while (cursor!!.moveToNext()) {//路径val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))val id = cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)val imageUri: Uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(id))//名称//val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))//大小//val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))photos.add(MyData(path, imageUri))}cursor.close()return photos}private fun readAllVideo(context: Context): ArrayList<MyData> {val videos = ArrayList<MyData>()//读取视频Videoval cursor = context.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,null,null,null,null)while (cursor!!.moveToNext()) {//路径val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))val id = cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)val videoUri: Uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(id))//名称//val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME))//大小//val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE))videos.add(MyData(path, videoUri))}cursor.close()return videos}
}

import android.net.Uriclass Item {companion object {const val THUMB = 0const val IMG = 1}var uri: Uri? = nullvar path: String? = nullvar lastModified = 0Lvar width = 0var height = 0var position = -1var type = -1  //0,缩略图。 1,正图image。-1,未知。constructor(uri: Uri, path: String) {this.uri = urithis.path = path}override fun toString(): String {return "Item(uri=$uri, path=$path, lastModified=$lastModified, width=$width, height=$height, position=$position, type=$type)"}
}

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil3.ImageLoader
import com.appdemo.MainActivity.MyDataclass MyAdapter : RecyclerView.Adapter<MyAdapter.ImageHolder> {private var mCtx: Context? = nullprivate var mImageLoader: ImageLoader? = nullprivate var mItems = ArrayList<ArrayList<MyData>>()private var mScreenWidth = 0companion object {const val TAG = "fly/ImageAdapter"}constructor(ctx: Context, il: ImageLoader?) : super() {mCtx = ctxmScreenWidth = mCtx?.resources?.displayMetrics?.widthPixels!!mImageLoader = il}fun dataChanged(items: ArrayList<ArrayList<MyData>>) {this.mItems = itemsnotifyDataSetChanged()}override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageHolder {val view = MyImgView(mCtx!!, mImageLoader, mScreenWidth)return ImageHolder(view)}override fun onBindViewHolder(holder: ImageHolder, position: Int) {holder.image.setData(mItems[position])}override fun getItemCount(): Int {return mItems.size}class ImageHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {var image = itemView as MyImgView}
}

import android.app.Application
import android.util.Log
import coil3.ImageLoader
import coil3.PlatformContext
import coil3.SingletonImageLoaderclass MyApp : Application(), SingletonImageLoader.Factory {companion object {const val TAG = "fly/MyApp"}override fun newImageLoader(context: PlatformContext): ImageLoader {Log.d(TAG, "newImageLoader")return MyCoilManager.INSTANCE.getImageLoader(this)}
}

import android.content.Context
import android.os.Environment
import android.util.Log
import coil3.ImageLoader
import coil3.disk.DiskCache
import coil3.disk.directory
import coil3.gif.AnimatedImageDecoder
import coil3.memory.MemoryCache
import coil3.request.CachePolicy
import java.io.Fileclass MyCoilManager {companion object {const val TAG = "fly/MyCoilManager"val INSTANCE by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { MyCoilManager() }}private var mImageLoader: ImageLoader? = nullprivate var memoryCacheMaxSize = 0Lfun getImageLoader(ctx: Context): ImageLoader {if (mImageLoader != null) {Log.w(TAG, "ImageLoader已经初始化")return mImageLoader!!}Log.d(TAG, "初始化ImageLoader")//初始化加载器。mImageLoader = ImageLoader.Builder(ctx).memoryCachePolicy(CachePolicy.ENABLED).memoryCache(initMemoryCache()).diskCachePolicy(CachePolicy.ENABLED).diskCache(initDiskCache()).components {add(AnimatedImageDecoder.Factory())add(ThumbFetcher.Factory(ctx))}.build()Log.d(TAG, "memoryCache.maxSize=${mImageLoader!!.memoryCache?.maxSize}")return mImageLoader!!}private fun initMemoryCache(): MemoryCache {//内存缓存。val memoryCache = MemoryCache.Builder().maxSizeBytes(1024 * 1024 * 1024 * 2L) //2GB.build()memoryCacheMaxSize = memoryCache.maxSizereturn memoryCache}private fun initDiskCache(): DiskCache {//磁盘缓存。val diskCacheFolder = Environment.getExternalStorageDirectory()val diskCacheName = "coil_disk_cache"val cacheFolder = File(diskCacheFolder, diskCacheName)if (cacheFolder.exists()) {Log.d(TAG, "${cacheFolder.absolutePath} exists")} else {if (cacheFolder.mkdir()) {Log.d(TAG, "${cacheFolder.absolutePath} create OK")} else {Log.e(TAG, "${cacheFolder.absolutePath} create fail")}}val diskCache = DiskCache.Builder().maxSizeBytes(1024 * 1024 * 1024 * 2L) //2GB.directory(cacheFolder).build()Log.d(TAG, "cache folder = ${diskCache.directory.toFile().absolutePath}")return diskCache}fun getMemoryCache(key: MemoryCache.Key): MemoryCache.Value? {return mImageLoader?.memoryCache?.get(key)}fun calMemoryCache(): String {val sz = mImageLoader?.memoryCache?.sizereturn "${sz?.toFloat()!! / memoryCacheMaxSize.toFloat()} , $sz / $memoryCacheMaxSize"}
}

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Picture
import android.graphics.RectF
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.graphics.toRect
import androidx.lifecycle.lifecycleScope
import coil3.ImageLoader
import coil3.memory.MemoryCache
import coil3.request.ErrorResult
import coil3.request.ImageRequest
import coil3.request.SuccessResult
import coil3.toBitmap
import com.appdemo.MainActivity.MyData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launchclass MyImgView : AppCompatImageView {companion object {const val TAG = "fly/MyImgView"//整数相除,精度损失的平衡因子const val BALANCE_FACTOR = 1}private var mCtx: Context? = nullprivate var mImageLoader: ImageLoader? = nullprivate var mScreenWidth: Int = 0private var mHeight: Int = 0private var mRealSize: Int = 0private var mBmp = mutableListOf<DataBean>()constructor(ctx: Context, il: ImageLoader?, screenWidth: Int) : super(ctx) {mCtx = ctxmImageLoader = ilmScreenWidth = screenWidthmHeight = mScreenWidth / MainActivity.ROW_SIZE + BALANCE_FACTORscaleType = ScaleType.CENTER_CROP}fun setData(data: ArrayList<MyData>) {mRealSize = data.size(mCtx as AppCompatActivity).lifecycleScope.launch(Dispatchers.IO) {var loadCount = 0// this for-loop will cost some time// if no memory cache, for-loop more , time cost moredata.forEachIndexed { _, myData ->val thumbItem = Item(uri = myData.uri, path = myData.path)thumbItem.type = Item.THUMBval thumbMemoryCacheKey = MemoryCache.Key(thumbItem.toString())val thumbMemoryCache = MyCoilManager.INSTANCE.getMemoryCache(thumbMemoryCacheKey)// be careful, this block will cost time// time cost pointif (thumbMemoryCache == null) {val t = System.currentTimeMillis()val thumbReq = ImageRequest.Builder(mCtx!!).data(thumbItem).size(MainActivity.THUMB_WIDTH, MainActivity.THUMB_HEIGHT).memoryCacheKey(thumbMemoryCacheKey).listener(object : ImageRequest.Listener {override fun onSuccess(request: ImageRequest, result: SuccessResult) {loadCount++refresh(loadCount, result.image.toBitmap())}override fun onCancel(request: ImageRequest) {Log.w(TAG, "onCancel")loadCount++refresh(loadCount, null)}override fun onError(request: ImageRequest, result: ErrorResult) {Log.e(TAG, "onError")loadCount++refresh(loadCount, null)}}).build()mImageLoader?.enqueue(thumbReq)Log.d(TAG, "加载... 耗时=${System.currentTimeMillis() - t}")} else {Log.d(TAG, "命中缓存 ${MyCoilManager.INSTANCE.calMemoryCache()}")loadCount++refresh(loadCount, thumbMemoryCache.image.toBitmap())}}}}private fun refresh(loadCount: Int, bmp: Bitmap?) {val bean = DataBean(bmp)mBmp.add(bean)if (loadCount == mRealSize) {val jBmp = joinBitmap()(mCtx as AppCompatActivity).lifecycleScope.launch(Dispatchers.Main) {this@MyImgView.setImageBitmap(jBmp)}mBmp.clear()}}data class DataBean(var bitmap: Bitmap?)private fun joinBitmap(): Bitmap {val bmp = Bitmap.createBitmap(mHeight * mRealSize, mHeight, Bitmap.Config.RGB_565)val canvas = Canvas(bmp)canvas.drawColor(Color.LTGRAY)val bitmaps = mBmp.toMutableList()bitmaps.forEachIndexed { idx, dataBean ->if (dataBean.bitmap != null) {if (Bitmap.Config.HARDWARE == dataBean.bitmap!!.config) {Log.d(TAG, "Bitmap.Config.HARDWARE")dataBean.bitmap = convert(dataBean.bitmap)}val w = dataBean.bitmap!!.widthval h = dataBean.bitmap!!.heightval mini = Math.min(w, h)val left = (w - mini) / 2fval top = (h - mini) / 2fval right = (w + mini) / 2fval bottom = (h + mini) / 2fval srcRct = RectF(left, top, right, bottom)val dstRctLeft = idx * mHeight.toFloat()val dstRct = RectF(dstRctLeft, 0f, dstRctLeft + mHeight, mHeight.toFloat())canvas.drawBitmap(dataBean.bitmap!!, srcRct.toRect(), dstRct.toRect(), null)}}bitmaps.clear()return bmp}private fun convert(src: Bitmap?): Bitmap {val w = src?.widthval h = src?.heightval picture = Picture()val canvas = picture.beginRecording(w!!, h!!)canvas.drawBitmap(src, 0f, 0f, null)picture.endRecording()return Bitmap.createBitmap(picture, w, h, Bitmap.Config.RGB_565)}override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)if (mHeight == 0) {setMeasuredDimension(mScreenWidth, MainActivity.THUMB_HEIGHT)} else {setMeasuredDimension(mHeight * mRealSize, mHeight)}}
}

import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import android.util.Size
import coil3.ImageLoader
import coil3.asImage
import coil3.decode.DataSource
import coil3.fetch.FetchResult
import coil3.fetch.Fetcher
import coil3.fetch.ImageFetchResult
import coil3.request.Options/*** 例如 FileUriFetcher*/
class ThumbFetcher(private val ctx: Context, private val thumbItem: Item, private val options: Options) : Fetcher {companion object {const val TAG = "fly/ThumbFetcher"}override suspend fun fetch(): FetchResult {var bmp: Bitmap? = nullval t = System.currentTimeMillis()try {bmp = ctx.contentResolver.loadThumbnail(thumbItem.uri!!, Size(MainActivity.THUMB_WIDTH, MainActivity.THUMB_HEIGHT), null)Log.d(TAG, "loadThumbnail time cost=${System.currentTimeMillis() - t} $thumbItem ${MyCoilManager.INSTANCE.calMemoryCache()}")} catch (e: Exception) {Log.e(TAG, "e=$e ThumbItem=$thumbItem")}return ImageFetchResult(bmp?.asImage()!!,true,dataSource = DataSource.DISK)}class Factory(private val ctx: Context) : Fetcher.Factory<Item> {override fun create(data: Item,options: Options,imageLoader: ImageLoader,): Fetcher {return ThumbFetcher(ctx, data, options)}}
}

http://www.dtcms.com/wzjs/524235.html

相关文章:

  • 做营销网站设计互联网营销师培训机构哪家好
  • 在青岛建网站seo知识培训
  • 福州做网站改版哪里比较好网站优化
  • 梅江区住房和城乡建设局官方网站关键词热度
  • 公司做免费网站建设网络建站
  • 厦门有没有做网站的在线培训
  • 做网站流程 优帮云百度权重高的发帖网站
  • 宝安网站建设公司968接单平台
  • 小程序定义武汉好的seo优化网
  • 柳州网站建设哪家公司好硬件优化大师下载
  • 个人网站 公安备案福建搜索引擎优化
  • 网站建设及推广预算表seo搜索优化技术
  • wordpress如何添加广告代码网站排名优化技巧
  • 免费空间怎么搞网站抖音代运营
  • 网站制作案例效果网络营销专业代码
  • 有没有可以做各种字体的网站微信营销软件手机版
  • 技术合同 网站开发厦门百度整站优化服务
  • 免费网站源码大全下载合肥今日头条最新消息
  • 厦门网站建设公司哪家好seo百度关键词优化
  • WordPress电影主题MIBT关键词优化需要从哪些方面开展
  • c 做网站 知乎免费推广app
  • 专门做装修的网站百度软件中心下载安装
  • 福泉网站制作seo3
  • 网站横幅js代码大二网络营销实训报告
  • php做网站会遇到的问题百度经验官网入口
  • 用电脑做服务器的建一个网站天天自学网网址
  • 用extjs做的网站杭州排名优化公司
  • 乌鲁木齐网站建设公司怎么推广自己的微信
  • 网络销售是做网站推广大数据比较好的培训机构
  • 白银市城县建设局网站seo价格是多少