Skip to content

Commit 8a79f85

Browse files
authored
Merge pull request #62 from Tencent/feature/android_animview_interface
Feature/android animview interface
2 parents 6d5f474 + a2f31e0 commit 8a79f85

8 files changed

Lines changed: 213 additions & 71 deletions

File tree

Android/PlayerProj/animplayer/publish.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ext {
2323
// library artifact(单个module一般就填写library name)
2424
artifact = 'animplayer'
2525
libraryName = 'animplayer'
26-
libraryVersion = '2.0.13'
26+
libraryVersion = '2.0.14'
2727
libraryDescription = ''
2828
// bintrayName 是你在网页Repository页面能看到的名称
2929
bintrayName = 'vap'

Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import com.tencent.qgame.animplayer.mask.MaskConfig
2020
import com.tencent.qgame.animplayer.plugin.AnimPluginManager
2121
import com.tencent.qgame.animplayer.util.ALog
2222

23-
class AnimPlayer(val animView: AnimView) {
23+
class AnimPlayer(val animView: IAnimView) {
2424

2525
companion object {
2626
private const val TAG = "${Constant.TAG}.AnimPlayer"

Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,27 @@ import com.tencent.qgame.animplayer.inter.IAnimListener
3030
import com.tencent.qgame.animplayer.inter.IFetchResource
3131
import com.tencent.qgame.animplayer.inter.OnResourceClickListener
3232
import com.tencent.qgame.animplayer.mask.MaskConfig
33+
import com.tencent.qgame.animplayer.textureview.InnerTextureView
3334
import com.tencent.qgame.animplayer.util.ALog
3435
import com.tencent.qgame.animplayer.util.IScaleType
3536
import com.tencent.qgame.animplayer.util.ScaleType
3637
import com.tencent.qgame.animplayer.util.ScaleTypeUtil
3738
import java.io.File
3839

3940
open class AnimView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0):
41+
IAnimView,
4042
FrameLayout(context, attrs, defStyleAttr),
4143
TextureView.SurfaceTextureListener {
4244

4345
companion object {
4446
private const val TAG = "${Constant.TAG}.AnimView"
4547
}
48+
private val player: AnimPlayer
49+
4650
private val uiHandler by lazy { Handler(Looper.getMainLooper()) }
4751
private var surface: SurfaceTexture? = null
48-
private var player: AnimPlayer? = null
4952
private var animListener: IAnimListener? = null
50-
private var innerTextureView: TextureView? = null
53+
private var innerTextureView: InnerTextureView? = null
5154
private var lastFile: FileContainer? = null
5255
private val scaleTypeUtil = ScaleTypeUtil()
5356

@@ -90,14 +93,15 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
9093
init {
9194
hide()
9295
player = AnimPlayer(this)
93-
player?.animListener = animProxyListener
96+
player.animListener = animProxyListener
9497
}
9598

9699

97-
fun prepareTextureView() {
100+
override fun prepareTextureView() {
98101
uiHandler.post {
99102
removeAllViews()
100-
innerTextureView = TextureView(context).apply {
103+
innerTextureView = InnerTextureView(context).apply {
104+
player = this@AnimView.player
101105
isOpaque = false
102106
surfaceTextureListener = this@AnimView
103107
layoutParams = scaleTypeUtil.getLayoutParam(this)
@@ -106,21 +110,21 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
106110
}
107111
}
108112

109-
fun getSurfaceTexture(): SurfaceTexture? {
113+
override fun getSurfaceTexture(): SurfaceTexture? {
110114
return innerTextureView?.surfaceTexture ?: surface
111115
}
112116

113117
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
114118
ALog.i(TAG, "onSurfaceTextureSizeChanged $width x $height")
115-
player?.onSurfaceTextureSizeChanged(width, height)
119+
player.onSurfaceTextureSizeChanged(width, height)
116120
}
117121

118122
override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
119123
}
120124

121125
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
122126
ALog.i(TAG, "onSurfaceTextureDestroyed")
123-
player?.onSurfaceTextureDestroyed()
127+
player.onSurfaceTextureDestroyed()
124128
uiHandler.post {
125129
innerTextureView?.surfaceTextureListener = null
126130
innerTextureView = null
@@ -132,7 +136,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
132136
override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
133137
ALog.i(TAG, "onSurfaceTextureAvailable")
134138
this.surface = surface
135-
player?.onSurfaceTextureAvailable(width, height)
139+
player.onSurfaceTextureAvailable(width, height)
136140
}
137141

138142
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
@@ -144,9 +148,9 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
144148
override fun onAttachedToWindow() {
145149
ALog.i(TAG, "onAttachedToWindow")
146150
super.onAttachedToWindow()
147-
player?.isDetachedFromWindow = false
151+
player.isDetachedFromWindow = false
148152
// 自动恢复播放
149-
if ((player?.playLoop ?: 0) > 0) {
153+
if ((player.playLoop ?: 0) > 0) {
150154
lastFile?.apply {
151155
startPlay(this)
152156
}
@@ -159,72 +163,68 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
159163
if (belowKitKat()) {
160164
release()
161165
}
162-
player?.isDetachedFromWindow = true
163-
player?.onSurfaceTextureDestroyed()
166+
player.isDetachedFromWindow = true
167+
player.onSurfaceTextureDestroyed()
164168
}
165169

166-
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
167-
val res = isRunning() && ev != null && player?.pluginManager?.onDispatchTouchEvent(ev) == true
168-
return if (!res) super.dispatchTouchEvent(ev) else true
169-
}
170170

171-
open fun setAnimListener(animListener: IAnimListener?) {
171+
override fun setAnimListener(animListener: IAnimListener?) {
172172
this.animListener = animListener
173173
}
174174

175-
open fun setFetchResource(fetchResource: IFetchResource?) {
176-
player?.pluginManager?.getMixAnimPlugin()?.resourceRequest = fetchResource
175+
override fun setFetchResource(fetchResource: IFetchResource?) {
176+
player.pluginManager.getMixAnimPlugin()?.resourceRequest = fetchResource
177177
}
178178

179-
open fun setOnResourceClickListener(resourceClickListener: OnResourceClickListener?) {
180-
player?.pluginManager?.getMixAnimPlugin()?.resourceClickListener = resourceClickListener
179+
override fun setOnResourceClickListener(resourceClickListener: OnResourceClickListener?) {
180+
player.pluginManager.getMixAnimPlugin()?.resourceClickListener = resourceClickListener
181181
}
182182

183183
/**
184184
* 兼容方案,优先保证表情显示
185185
*/
186186
open fun enableAutoTxtColorFill(enable: Boolean) {
187-
player?.pluginManager?.getMixAnimPlugin()?.autoTxtColorFill = enable
187+
player.pluginManager.getMixAnimPlugin()?.autoTxtColorFill = enable
188188
}
189189

190-
fun setLoop(playLoop: Int) {
191-
player?.playLoop = playLoop
190+
override fun setLoop(playLoop: Int) {
191+
player.playLoop = playLoop
192192
}
193193

194-
fun supportMask(isSupport : Boolean, isEdgeBlur : Boolean) {
195-
player?.supportMaskBoolean = isSupport
196-
player?.maskEdgeBlurBoolean = isEdgeBlur
194+
override fun supportMask(isSupport : Boolean, isEdgeBlur : Boolean) {
195+
player.supportMaskBoolean = isSupport
196+
player.maskEdgeBlurBoolean = isEdgeBlur
197197
}
198198

199-
fun updateMaskConfig(maskConfig: MaskConfig?) {
200-
player?.updateMaskConfig(maskConfig)
199+
override fun updateMaskConfig(maskConfig: MaskConfig?) {
200+
player.updateMaskConfig(maskConfig)
201201
}
202202

203203

204204
@Deprecated("Compatible older version mp4, default false")
205205
fun enableVersion1(enable: Boolean) {
206-
player?.enableVersion1 = enable
206+
player.enableVersion1 = enable
207207
}
208208

209209
// 兼容老版本视频模式
210210
@Deprecated("Compatible older version mp4")
211211
fun setVideoMode(mode: Int) {
212-
player?.videoMode = mode
212+
player.videoMode = mode
213213
}
214214

215-
fun setFps(fps: Int) {
216-
player?.fps = fps
215+
override fun setFps(fps: Int) {
216+
player.fps = fps
217217
}
218218

219-
fun setScaleType(type : ScaleType) {
219+
override fun setScaleType(type : ScaleType) {
220220
scaleTypeUtil.currentScaleType = type
221221
}
222222

223-
fun setScaleType(scaleType: IScaleType) {
223+
override fun setScaleType(scaleType: IScaleType) {
224224
scaleTypeUtil.scaleTypeImpl = scaleType
225225
}
226226

227-
fun startPlay(file: File) {
227+
override fun startPlay(file: File) {
228228
try {
229229
val fileContainer = FileContainer(file)
230230
startPlay(fileContainer)
@@ -233,7 +233,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
233233
}
234234
}
235235

236-
fun startPlay(assetManager: AssetManager, assetsPath: String) {
236+
override fun startPlay(assetManager: AssetManager, assetsPath: String) {
237237
try {
238238
val fileContainer = FileContainer(assetManager, assetsPath)
239239
startPlay(fileContainer)
@@ -243,28 +243,32 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
243243
}
244244

245245

246-
fun startPlay(fileContainer: FileContainer) {
246+
override fun startPlay(fileContainer: FileContainer) {
247247
ui {
248248
if (visibility != View.VISIBLE) {
249249
ALog.e(TAG, "AnimView is GONE, can't play")
250250
return@ui
251251
}
252-
if (player?.isRunning() == false) {
252+
if (!player.isRunning()) {
253253
lastFile = fileContainer
254-
player?.startPlay(fileContainer)
254+
player.startPlay(fileContainer)
255255
} else {
256256
ALog.i(TAG, "is running can not start")
257257
}
258258
}
259259
}
260260

261261

262-
fun stopPlay() {
263-
player?.stopPlay()
262+
override fun stopPlay() {
263+
player.stopPlay()
264+
}
265+
266+
override fun isRunning(): Boolean {
267+
return player.isRunning()
264268
}
265269

266-
fun isRunning(): Boolean {
267-
return player?.isRunning() ?: false
270+
override fun getRealSize(): Pair<Int, Int> {
271+
return scaleTypeUtil.getRealSize()
268272
}
269273

270274
private fun hide() {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making vap available.
3+
*
4+
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the MIT License (the "License"); you may not use this file except in
7+
* compliance with the License. You may obtain a copy of the License at
8+
*
9+
* http://opensource.org/licenses/MIT
10+
*
11+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
12+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.tencent.qgame.animplayer
17+
18+
import android.content.res.AssetManager
19+
import android.graphics.SurfaceTexture
20+
import com.tencent.qgame.animplayer.inter.IAnimListener
21+
import com.tencent.qgame.animplayer.inter.IFetchResource
22+
import com.tencent.qgame.animplayer.inter.OnResourceClickListener
23+
import com.tencent.qgame.animplayer.mask.MaskConfig
24+
import com.tencent.qgame.animplayer.util.IScaleType
25+
import com.tencent.qgame.animplayer.util.ScaleType
26+
import java.io.File
27+
28+
interface IAnimView {
29+
30+
fun prepareTextureView()
31+
32+
fun getSurfaceTexture(): SurfaceTexture?
33+
34+
fun setAnimListener(animListener: IAnimListener?)
35+
36+
fun setFetchResource(fetchResource: IFetchResource?)
37+
38+
fun setOnResourceClickListener(resourceClickListener: OnResourceClickListener?)
39+
40+
fun setLoop(playLoop: Int)
41+
42+
fun supportMask(isSupport: Boolean, isEdgeBlur: Boolean)
43+
44+
fun updateMaskConfig(maskConfig: MaskConfig?)
45+
46+
fun setFps(fps: Int)
47+
48+
fun setScaleType(type: ScaleType)
49+
50+
fun setScaleType(scaleType: IScaleType)
51+
52+
fun startPlay(file: File)
53+
54+
fun startPlay(assetManager: AssetManager, assetsPath: String)
55+
56+
fun startPlay(fileContainer: FileContainer)
57+
58+
fun stopPlay()
59+
60+
fun isRunning(): Boolean
61+
62+
fun getRealSize(): Pair<Int, Int>
63+
}

Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/MixTouch.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ import com.tencent.qgame.animplayer.PointRect
2424
class MixTouch(private val mixAnimPlugin: MixAnimPlugin) {
2525

2626
fun onTouchEvent(ev: MotionEvent): Resource? {
27-
val viewWith = mixAnimPlugin.player.animView.width
28-
val viewHeight = mixAnimPlugin.player.animView.height
27+
val (viewWith, viewHeight) = mixAnimPlugin.player.animView.getRealSize()
2928
val videoWith = mixAnimPlugin.player.configManager.config?.width ?: return null
3029
val videoHeight = mixAnimPlugin.player.configManager.config?.height ?: return null
3130

3231
if (viewWith == 0 || viewHeight == 0) return null
3332

3433
when(ev.action) {
3534
MotionEvent.ACTION_UP -> {
36-
val x = ev.rawX * videoWith / viewWith
37-
val y = ev.rawY * videoHeight / viewHeight
35+
val x = ev.x * videoWith / viewWith.toFloat()
36+
val y = ev.y * videoHeight / viewHeight.toFloat()
3837
val list = mixAnimPlugin.frameAll?.map?.get(mixAnimPlugin.curFrameIndex)?.list
3938
list?.forEach {frame ->
4039
val src = mixAnimPlugin.srcMap?.map?.get(frame.srcId) ?: return@forEach
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.tencent.qgame.animplayer.textureview
2+
3+
import android.content.Context
4+
import android.util.AttributeSet
5+
import android.view.MotionEvent
6+
import android.view.TextureView
7+
import com.tencent.qgame.animplayer.AnimPlayer
8+
9+
class InnerTextureView @JvmOverloads constructor(
10+
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
11+
) : TextureView(context, attrs, defStyleAttr) {
12+
13+
var player: AnimPlayer? = null
14+
15+
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
16+
val res = player?.isRunning() == true
17+
&& ev != null
18+
&& player?.pluginManager?.onDispatchTouchEvent(ev) == true
19+
return if (!res) super.dispatchTouchEvent(ev) else true
20+
}
21+
}

0 commit comments

Comments
 (0)