Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
AndroidDeveloperLB committed Feb 11, 2019
1 parent e376cd4 commit e23a5fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
12 changes: 6 additions & 6 deletions app/src/main/res/layout/video_trimmer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@

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

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

<FrameLayout
android:id="@+id/timeTextContainer" android:layout_width="match_parent" android:layout_height="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import android.net.Uri
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.util.AttributeSet
import android.util.LongSparseArray
import android.view.View
import com.lb.video_trimmer_library.utils.BackgroundExecutor
import com.lb.video_trimmer_library.utils.UiThreadExecutor
Expand All @@ -43,7 +42,8 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
View(context, attrs, defStyleAttr) {
private var videoUri: Uri? = null
@Suppress("LeakingThis")
private var bitmapList: LongSparseArray<Bitmap>? = null
// private var bitmapList: LongSparseArray<Bitmap>? = null
private val bitmapList = ArrayList<Bitmap?>()

override fun onSizeChanged(w: Int, h: Int, oldW: Int, oldH: Int) {
super.onSizeChanged(w, h, oldW, oldH)
Expand All @@ -52,25 +52,24 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
}

private fun getBitmap(viewWidth: Int, viewHeight: Int) {
// if (isInEditMode)
// return
// Set thumbnail properties (Thumbs are squares)
@Suppress("UnnecessaryVariable")
val thumbSize = viewHeight
val numThumbs = Math.ceil((viewWidth.toFloat() / thumbSize).toDouble()).toInt()
bitmapList.clear()
if (isInEditMode) {
val bitmap = ThumbnailUtils.extractThumbnail(
BitmapFactory.decodeResource(resources, android.R.drawable.sym_def_app_icon)!!, thumbSize, thumbSize
)
bitmapList = LongSparseArray()
for (i in 0 until numThumbs)
bitmapList!!.put(i.toLong(), bitmap)
bitmapList.add(bitmap)
return
}
BackgroundExecutor.cancelAll("", true)
BackgroundExecutor.execute(object : BackgroundExecutor.Task("", 0L, "") {
override fun execute() {
try {
val thumbnailList = LongSparseArray<Bitmap>()
val thumbnailList = ArrayList<Bitmap?>()
val mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(context, videoUri)
// Retrieve media data
Expand All @@ -88,7 +87,7 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
)
if (bitmap != null)
bitmap = ThumbnailUtils.extractThumbnail(bitmap, thumbSize, thumbSize)
thumbnailList.put(i.toLong(), bitmap)
thumbnailList.add(bitmap)
}
mediaMetadataRetriever.release()
returnBitmaps(thumbnailList)
Expand All @@ -101,26 +100,24 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
)
}

private fun returnBitmaps(thumbnailList: LongSparseArray<Bitmap>) {
private fun returnBitmaps(thumbnailList: ArrayList<Bitmap?>) {
UiThreadExecutor.runTask("", Runnable {
bitmapList = thumbnailList
bitmapList.clear()
bitmapList.addAll(thumbnailList)
invalidate()
}, 0L)
}

@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (bitmapList != null) {
canvas.save()
var x = 0
for (i in 0L until bitmapList!!.size()) {
val bitmap = bitmapList!!.get(i)
if (bitmap != null) {
canvas.drawBitmap(bitmap, x.toFloat(), 0f, null)
x += bitmap.width
}
}
canvas.save()
var x = 0
val thumbSize = height
for (bitmap in bitmapList) {
if (bitmap != null)
canvas.drawBitmap(bitmap, x.toFloat(), 0f, null)
x += thumbSize
}
}

Expand Down

0 comments on commit e23a5fa

Please sign in to comment.