@@ -34,7 +34,6 @@ import android.net.Uri
3434import android.os.Build.VERSION
3535import android.os.Build.VERSION_CODES
3636import android.util.AttributeSet
37- import android.util.LongSparseArray
3837import android.view.View
3938import com.lb.video_trimmer_library.utils.BackgroundExecutor
4039import com.lb.video_trimmer_library.utils.UiThreadExecutor
@@ -43,7 +42,8 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
4342 View (context, attrs, defStyleAttr) {
4443 private var videoUri: Uri ? = null
4544 @Suppress(" LeakingThis" )
46- private var bitmapList: LongSparseArray <Bitmap >? = null
45+ // private var bitmapList: LongSparseArray<Bitmap>? = null
46+ private val bitmapList = ArrayList <Bitmap ?>()
4747
4848 override fun onSizeChanged (w : Int , h : Int , oldW : Int , oldH : Int ) {
4949 super .onSizeChanged(w, h, oldW, oldH)
@@ -52,25 +52,24 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
5252 }
5353
5454 private fun getBitmap (viewWidth : Int , viewHeight : Int ) {
55- // if (isInEditMode)
56- // return
5755 // Set thumbnail properties (Thumbs are squares)
56+ @Suppress(" UnnecessaryVariable" )
5857 val thumbSize = viewHeight
5958 val numThumbs = Math .ceil((viewWidth.toFloat() / thumbSize).toDouble()).toInt()
59+ bitmapList.clear()
6060 if (isInEditMode) {
6161 val bitmap = ThumbnailUtils .extractThumbnail(
6262 BitmapFactory .decodeResource(resources, android.R .drawable.sym_def_app_icon)!! , thumbSize, thumbSize
6363 )
64- bitmapList = LongSparseArray ()
6564 for (i in 0 until numThumbs)
66- bitmapList!! .put(i.toLong(), bitmap)
65+ bitmapList.add( bitmap)
6766 return
6867 }
6968 BackgroundExecutor .cancelAll(" " , true )
7069 BackgroundExecutor .execute(object : BackgroundExecutor .Task (" " , 0L , " " ) {
7170 override fun execute () {
7271 try {
73- val thumbnailList = LongSparseArray <Bitmap >()
72+ val thumbnailList = ArrayList <Bitmap ? >()
7473 val mediaMetadataRetriever = MediaMetadataRetriever ()
7574 mediaMetadataRetriever.setDataSource(context, videoUri)
7675 // Retrieve media data
@@ -88,7 +87,7 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
8887 )
8988 if (bitmap != null )
9089 bitmap = ThumbnailUtils .extractThumbnail(bitmap, thumbSize, thumbSize)
91- thumbnailList.put(i.toLong(), bitmap)
90+ thumbnailList.add( bitmap)
9291 }
9392 mediaMetadataRetriever.release()
9493 returnBitmaps(thumbnailList)
@@ -101,26 +100,24 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
101100 )
102101 }
103102
104- private fun returnBitmaps (thumbnailList : LongSparseArray <Bitmap >) {
103+ private fun returnBitmaps (thumbnailList : ArrayList <Bitmap ? >) {
105104 UiThreadExecutor .runTask(" " , Runnable {
106- bitmapList = thumbnailList
105+ bitmapList.clear()
106+ bitmapList.addAll(thumbnailList)
107107 invalidate()
108108 }, 0L )
109109 }
110110
111111 @SuppressLint(" DrawAllocation" )
112112 override fun onDraw (canvas : Canvas ) {
113113 super .onDraw(canvas)
114- if (bitmapList != null ) {
115- canvas.save()
116- var x = 0
117- for (i in 0L until bitmapList!! .size()) {
118- val bitmap = bitmapList!! .get(i)
119- if (bitmap != null ) {
120- canvas.drawBitmap(bitmap, x.toFloat(), 0f , null )
121- x + = bitmap.width
122- }
123- }
114+ canvas.save()
115+ var x = 0
116+ val thumbSize = height
117+ for (bitmap in bitmapList) {
118+ if (bitmap != null )
119+ canvas.drawBitmap(bitmap, x.toFloat(), 0f , null )
120+ x + = thumbSize
124121 }
125122 }
126123
0 commit comments