Skip to content

Commit

Permalink
Add some classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
xbdcc committed Jan 31, 2021
1 parent c680722 commit e609ab1
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* `21/01/31` Add some classes.
* `20/07/26` Optimize the code.(v0.0.29)
* `20/07/05` Add CBaseViewPagerAdapter class.
* `20/03/14` Add some classes.(v0.0.28)
Expand Down
80 changes: 80 additions & 0 deletions cutils/src/main/java/com/carlos/cutils/media/CVideoView.kt
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 cutils/src/main/java/com/carlos/cutils/util/MyFrameCallback.kt
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)
}
}
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
}

}
40 changes: 40 additions & 0 deletions cutils/src/test/java/com/carlos/cutils/DimenTest.kt
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()
}

0 comments on commit e609ab1

Please sign in to comment.