Skip to content

Commit e376cd4

Browse files
removed need to specify height of views. Now you will have to set them the same on the layout itself.
1 parent f74a5ef commit e376cd4

File tree

3 files changed

+48
-51
lines changed

3 files changed

+48
-51
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
android:background="#33ffffff" app:layout_constraintBottom_toBottomOf="parent"/>
3232

3333
<com.lb.video_trimmer_library.view.TimeLineView
34-
android:id="@+id/timeLineView" android:layout_width="match_parent" android:layout_height="wrap_content"
35-
app:layout_constraintBottom_toBottomOf="parent" tools:background="@tools:sample/backgrounds/scenic"/>
34+
android:id="@+id/timeLineView" android:layout_width="match_parent" android:layout_height="40dp"
35+
app:layout_constraintBottom_toBottomOf="parent" />
3636

37-
<com.lb.video_trimmer_library.view.RangeSeekBarView
38-
android:id="@+id/rangeSeekBarView" android:layout_width="match_parent" android:layout_height="wrap_content"
39-
app:layout_constraintBottom_toBottomOf="@id/timeLineView" tools:background="#3300ffff"/>
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"/>
4042

4143
<FrameLayout
4244
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/RangeSeekBarView.kt

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ open class RangeSeekBarView @JvmOverloads constructor(context: Context, attrs: A
4343

4444
@Suppress("MemberVisibilityCanBePrivate")
4545
private val thumbTouchExtraMultiplier = initThumbTouchExtraMultiplier()
46-
private var timeLineHeight: Int = initTimeLineHeight(context)
4746
private val thumbs = arrayOf(Thumb(ThumbType.LEFT.index), Thumb(ThumbType.RIGHT.index))
4847
private var listeners = HashSet<OnRangeSeekBarListener>()
4948
private var maxWidth: Float = 0.toFloat()
@@ -85,14 +84,6 @@ open class RangeSeekBarView @JvmOverloads constructor(context: Context, attrs: A
8584
context.resources.displayMetrics
8685
).toInt().coerceAtLeast(1)
8786

88-
private fun initTimeLineHeight(context: Context) =
89-
TypedValue.applyDimension(
90-
TypedValue.COMPLEX_UNIT_DIP,
91-
40f,
92-
context.resources.displayMetrics
93-
).toInt().coerceAtLeast(1)
94-
95-
9687
fun initMaxWidth() {
9788
maxWidth = thumbs[ThumbType.RIGHT.index].pos - thumbs[ThumbType.LEFT.index].pos
9889
onSeekStop(this, ThumbType.LEFT.index, thumbs[ThumbType.LEFT.index].value)
@@ -101,11 +92,7 @@ open class RangeSeekBarView @JvmOverloads constructor(context: Context, attrs: A
10192

10293
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
10394
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
104-
val minW = paddingLeft + paddingRight + suggestedMinimumWidth
105-
viewWidth = View.resolveSizeAndState(minW, widthMeasureSpec, 1)
106-
val minH = paddingBottom + paddingTop + timeLineHeight
107-
val viewHeight = View.resolveSizeAndState(minH, heightMeasureSpec, 1)
108-
setMeasuredDimension(viewWidth, viewHeight)
95+
viewWidth=measuredWidth
10996
pixelRangeMin = 0f
11097
pixelRangeMax = (viewWidth - thumbWidth).toFloat()
11198
if (firstRun) {
@@ -128,32 +115,32 @@ open class RangeSeekBarView @JvmOverloads constructor(context: Context, attrs: A
128115
if (thumb.index == ThumbType.LEFT.index) {
129116
val x = thumb.pos + paddingLeft
130117
if (x > pixelRangeMin)
131-
canvas.drawRect(thumbWidth.toFloat(), 0f, (x + thumbWidth), timeLineHeight.toFloat(), shadowPaint)
118+
canvas.drawRect(thumbWidth.toFloat(), 0f, (x + thumbWidth), height.toFloat(), shadowPaint)
132119
} else {
133120
val x = thumb.pos - paddingRight
134121
if (x < pixelRangeMax)
135-
canvas.drawRect(x, 0f, (viewWidth - thumbWidth).toFloat(), timeLineHeight.toFloat(), shadowPaint)
122+
canvas.drawRect(x, 0f, (viewWidth - thumbWidth).toFloat(), height.toFloat(), shadowPaint)
136123
}
137124
}
138125
//draw stroke around selected range
139126
canvas.drawRect(
140127
(thumbs[ThumbType.LEFT.index].pos + paddingLeft + thumbWidth),
141128
0f,
142129
thumbs[ThumbType.RIGHT.index].pos - paddingRight,
143-
timeLineHeight.toFloat(),
130+
height.toFloat(),
144131
strokePaint
145132
)
146133
//draw edges
147134
val circleRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6f, context.resources.displayMetrics)
148135
canvas.drawCircle(
149136
(thumbs[ThumbType.LEFT.index].pos + paddingLeft + thumbWidth),
150-
timeLineHeight.toFloat() / 2f,
137+
height.toFloat() / 2f,
151138
circleRadius,
152139
edgePaint
153140
)
154141
canvas.drawCircle(
155142
thumbs[ThumbType.RIGHT.index].pos - paddingRight,
156-
timeLineHeight.toFloat() / 2f,
143+
height.toFloat() / 2f,
157144
circleRadius,
158145
edgePaint
159146
)

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

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
*/
2424
package com.lb.video_trimmer_library.view
2525

26+
import android.annotation.SuppressLint
2627
import android.content.Context
2728
import android.graphics.Bitmap
29+
import android.graphics.BitmapFactory
2830
import android.graphics.Canvas
2931
import android.media.MediaMetadataRetriever
3032
import android.media.ThumbnailUtils
@@ -33,54 +35,59 @@ import android.os.Build.VERSION
3335
import android.os.Build.VERSION_CODES
3436
import android.util.AttributeSet
3537
import android.util.LongSparseArray
36-
import android.util.TypedValue
3738
import android.view.View
3839
import com.lb.video_trimmer_library.utils.BackgroundExecutor
3940
import com.lb.video_trimmer_library.utils.UiThreadExecutor
4041

41-
open class TimeLineView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
42+
open class TimeLineView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) :
43+
View(context, attrs, defStyleAttr) {
4244
private var videoUri: Uri? = null
4345
@Suppress("LeakingThis")
44-
private var heightView: Int = initHeightView()
4546
private var bitmapList: LongSparseArray<Bitmap>? = null
4647

47-
open fun initHeightView(): Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40f, context.resources.displayMetrics).toInt().coerceAtLeast(1)
48-
49-
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
50-
val minW = paddingLeft + paddingRight + suggestedMinimumWidth
51-
val w = View.resolveSizeAndState(minW, widthMeasureSpec, 1)
52-
val minH = paddingBottom + paddingTop + heightView
53-
val h = View.resolveSizeAndState(minH, heightMeasureSpec, 1)
54-
setMeasuredDimension(w, h)
55-
}
56-
5748
override fun onSizeChanged(w: Int, h: Int, oldW: Int, oldH: Int) {
5849
super.onSizeChanged(w, h, oldW, oldH)
5950
if (w != oldW)
60-
getBitmap(w)
51+
getBitmap(w, h)
6152
}
6253

63-
private fun getBitmap(viewWidth: Int) {
54+
private fun getBitmap(viewWidth: Int, viewHeight: Int) {
55+
// if (isInEditMode)
56+
// return
57+
// Set thumbnail properties (Thumbs are squares)
58+
val thumbSize = viewHeight
59+
val numThumbs = Math.ceil((viewWidth.toFloat() / thumbSize).toDouble()).toInt()
60+
if (isInEditMode) {
61+
val bitmap = ThumbnailUtils.extractThumbnail(
62+
BitmapFactory.decodeResource(resources, android.R.drawable.sym_def_app_icon)!!, thumbSize, thumbSize
63+
)
64+
bitmapList = LongSparseArray()
65+
for (i in 0 until numThumbs)
66+
bitmapList!!.put(i.toLong(), bitmap)
67+
return
68+
}
69+
BackgroundExecutor.cancelAll("", true)
6470
BackgroundExecutor.execute(object : BackgroundExecutor.Task("", 0L, "") {
6571
override fun execute() {
6672
try {
6773
val thumbnailList = LongSparseArray<Bitmap>()
6874
val mediaMetadataRetriever = MediaMetadataRetriever()
6975
mediaMetadataRetriever.setDataSource(context, videoUri)
7076
// Retrieve media data
71-
val videoLengthInMs = (Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) * 1000).toLong()
72-
// Set thumbnail properties (Thumbs are squares)
73-
val thumbSize = heightView
74-
val numThumbs = Math.ceil((viewWidth.toFloat() / thumbSize).toDouble()).toInt()
77+
val videoLengthInMs =
78+
mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toLong() * 1000L
7579
val interval = videoLengthInMs / numThumbs
7680
for (i in 0 until numThumbs) {
77-
var bitmap: Bitmap?
78-
bitmap = if (VERSION.SDK_INT >= VERSION_CODES.O_MR1)
79-
mediaMetadataRetriever.getScaledFrameAtTime(i * interval, MediaMetadataRetriever.OPTION_CLOSEST_SYNC, thumbSize, thumbSize)
80-
else
81-
mediaMetadataRetriever.getFrameAtTime(i * interval, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)
81+
var bitmap: Bitmap? = if (VERSION.SDK_INT >= VERSION_CODES.O_MR1)
82+
mediaMetadataRetriever.getScaledFrameAtTime(
83+
i * interval, MediaMetadataRetriever.OPTION_CLOSEST_SYNC, thumbSize, thumbSize
84+
)
85+
else mediaMetadataRetriever.getFrameAtTime(
86+
i * interval,
87+
MediaMetadataRetriever.OPTION_CLOSEST_SYNC
88+
)
8289
if (bitmap != null)
83-
bitmap = ThumbnailUtils.extractThumbnail(bitmap, thumbSize, thumbSize) //Bitmap.createScaledBitmap(bitmap, thumbSize, thumbSize, false);
90+
bitmap = ThumbnailUtils.extractThumbnail(bitmap, thumbSize, thumbSize)
8491
thumbnailList.put(i.toLong(), bitmap)
8592
}
8693
mediaMetadataRetriever.release()
@@ -101,13 +108,14 @@ open class TimeLineView @JvmOverloads constructor(context: Context, attrs: Attri
101108
}, 0L)
102109
}
103110

111+
@SuppressLint("DrawAllocation")
104112
override fun onDraw(canvas: Canvas) {
105113
super.onDraw(canvas)
106114
if (bitmapList != null) {
107115
canvas.save()
108116
var x = 0
109-
for (i in 0 until bitmapList!!.size()) {
110-
val bitmap = bitmapList!!.get(i.toLong())
117+
for (i in 0L until bitmapList!!.size()) {
118+
val bitmap = bitmapList!!.get(i)
111119
if (bitmap != null) {
112120
canvas.drawBitmap(bitmap, x.toFloat(), 0f, null)
113121
x += bitmap.width

0 commit comments

Comments
 (0)