Skip to content

Commit e23a5fa

Browse files
code cleaning
1 parent e376cd4 commit e23a5fa

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

app/src/main/res/layout/video_trimmer.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232

3333
<com.lb.video_trimmer_library.view.TimeLineView
3434
android:id="@+id/timeLineView" android:layout_width="match_parent" android:layout_height="40dp"
35-
app:layout_constraintBottom_toBottomOf="parent" />
35+
app:layout_constraintBottom_toBottomOf="parent"/>
3636

37-
<com.lb.video_trimmer_library.view.RangeSeekBarView app:layout_constraintTop_toTopOf="@id/timeLineView"
38-
android:id="@+id/rangeSeekBarView"
39-
android:layout_width="match_parent" android:layout_height="0px"
40-
app:layout_constraintBottom_toBottomOf="@id/timeLineView"
41-
tools:background="#3300ffff"/>
37+
<com.lb.video_trimmer_library.view.RangeSeekBarView
38+
app:layout_constraintTop_toTopOf="@id/timeLineView" android:id="@+id/rangeSeekBarView"
39+
android:layout_width="match_parent" android:layout_height="0px"
40+
app:layout_constraintBottom_toBottomOf="@id/timeLineView"
41+
tools:background="#3300ffff"/>
4242

4343
<FrameLayout
4444
android:id="@+id/timeTextContainer" android:layout_width="match_parent" android:layout_height="wrap_content"

video_trimmer_library/src/main/java/com/lb/video_trimmer_library/view/TimeLineView.kt

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import android.net.Uri
3434
import android.os.Build.VERSION
3535
import android.os.Build.VERSION_CODES
3636
import android.util.AttributeSet
37-
import android.util.LongSparseArray
3837
import android.view.View
3938
import com.lb.video_trimmer_library.utils.BackgroundExecutor
4039
import 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

Comments
 (0)