-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
cutils/src/main/java/com/carlos/cutils/media/CVideoView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.carlos.cutils.media | ||
|
||
import android.content.Context | ||
import android.media.MediaPlayer | ||
import android.util.Log | ||
import android.widget.MediaController | ||
import android.widget.VideoView | ||
|
||
/** | ||
* Created by Carlos on 2021/1/29. | ||
*/ | ||
class CVideoView(context: Context, videoView: VideoView) { | ||
|
||
var mVideoView: VideoView = videoView | ||
var mContext: Context = context | ||
private val TAG = "CVideoView" | ||
|
||
fun start(videoPath: String, isLoop: Boolean = false) { | ||
mVideoView.setVideoPath(videoPath) | ||
mVideoView.setOnPreparedListener { | ||
it?.isLooping = isLoop | ||
mVideoView.start() | ||
Log.d(TAG, "start-> is loop:$isLoop") | ||
} | ||
} | ||
|
||
fun start() { | ||
if (mVideoView.isPlaying) | ||
return | ||
mVideoView.start() | ||
} | ||
|
||
fun pause() { | ||
mVideoView.pause() | ||
} | ||
|
||
fun release() { | ||
mVideoView.suspend() | ||
} | ||
|
||
fun getDuration() = mVideoView.duration | ||
|
||
fun getCurrentPosition() = mVideoView.currentPosition | ||
|
||
fun seekTo(position: Int) = mVideoView.seekTo(position) | ||
|
||
|
||
fun setOnErrorListener(onErrorListener: OnErrorListener, isHandled: Boolean = false) { | ||
mVideoView.setOnErrorListener { mp, what, extra -> | ||
Log.e(TAG, "onError->what:${what} - extra:$extra") | ||
onErrorListener.onError(mp, what, extra) | ||
isHandled | ||
} | ||
} | ||
|
||
fun setOnCompletionListener(onCompletionListener: OnCompletionListener) { | ||
mVideoView.setOnCompletionListener { | ||
Log.e(TAG, "onCompletion") | ||
onCompletionListener.onCompletion(it) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* show default media controller | ||
*/ | ||
fun showMediaController() { | ||
val mediaController = MediaController(mContext) | ||
mVideoView.setMediaController(mediaController) | ||
} | ||
|
||
interface OnErrorListener { | ||
fun onError(mp: MediaPlayer?, what: Int, extra: Int) | ||
} | ||
|
||
interface OnCompletionListener { | ||
fun onCompletion(mp: MediaPlayer?) | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
cutils/src/main/java/com/carlos/cutils/util/MyFrameCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.carlos.cutils.util | ||
|
||
import android.util.Log | ||
import android.view.Choreographer | ||
import android.view.Choreographer.FrameCallback | ||
|
||
/** | ||
* Created by Carlos on 2020/12/7. | ||
*/ | ||
class MyFrameCallback : FrameCallback { | ||
private val TAG = "性能检测" | ||
private var lastTime: Long = 0 | ||
override fun doFrame(frameTimeNanos: Long) { | ||
lastTime = if (lastTime == 0L) { | ||
//代码第一次初始化。不做检测统计。 | ||
frameTimeNanos | ||
} else { | ||
val times = (frameTimeNanos - lastTime) / 1000000 | ||
val frames = (times / 16).toInt() | ||
if (times > 16) { | ||
Log.w(TAG, "UI线程超时(超过16ms):" + times + "ms" + " , 丢帧:" + frames) | ||
} | ||
frameTimeNanos | ||
} | ||
Choreographer.getInstance().postFrameCallback(this) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
cutils/src/main/java/com/carlos/cutils/view/TextViewAlwaysMarquee.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.carlos.cutils.view | ||
|
||
import android.content.Context | ||
import android.text.TextUtils | ||
import android.util.AttributeSet | ||
import androidx.appcompat.widget.AppCompatTextView | ||
|
||
/** | ||
* Created by Carlos on 2020/11/28. | ||
* TV文字落焦跑马灯 | ||
*/ | ||
class TextViewAlwaysMarquee( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyleAttr: Int = 0 | ||
) : AppCompatTextView(context, attrs, defStyleAttr) { | ||
|
||
init { | ||
ellipsize = TextUtils.TruncateAt.MARQUEE | ||
marqueeRepeatLimit = -1 | ||
setSingleLine() | ||
} | ||
|
||
/** | ||
* 用于外部获取焦点 | ||
*/ | ||
fun showMarquee(isShow: Boolean) { | ||
ellipsize = if (isShow) | ||
TextUtils.TruncateAt.MARQUEE | ||
else | ||
null | ||
} | ||
|
||
override fun isFocused(): Boolean { | ||
return true | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.carlos.cutils | ||
|
||
import org.junit.Test | ||
import java.lang.StringBuilder | ||
|
||
/** | ||
* Created by Carlos on 2020/11/24. | ||
* dimen适配转换测试 | ||
*/ | ||
class DimenTest { | ||
|
||
@Test | ||
fun convert1080to720() { | ||
convertDimen(data, 1080, 720) | ||
} | ||
|
||
@Test | ||
fun printlnPx() { | ||
for (i in 1..1180) { | ||
println(" <dimen name=\"px$i\">${i}px</dimen>") | ||
} | ||
} | ||
|
||
private fun convertDimen(data: String, oldDimen: Int, newDimen: Int) { | ||
val result = StringBuilder() | ||
for (line in data.lines()) { | ||
val dataRegex = Regex("(?<=>).*(?=[dp][px])") | ||
val string = | ||
dataRegex.replace(line) { (it.value.toInt() * newDimen / oldDimen).toString() } | ||
result.appendLine(string) | ||
} | ||
println(result) | ||
} | ||
|
||
private val data: String = """ | ||
<dimen name="dimen_3600px">360px</dimen> | ||
<dimen name="dimen_720px">720px</dimen> | ||
<dimen name="dimen_1080px">1080px</dimen> | ||
""".trimIndent() | ||
} |