From 0c2b20b9a9ef0d68b0cd9421c6f0222e8031a52a Mon Sep 17 00:00:00 2001 From: hexleo Date: Mon, 21 Nov 2022 11:22:52 +0800 Subject: [PATCH] =?UTF-8?q?bug:=20=E8=A7=A3=E5=86=B3centerCrop=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20close=20#276?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tencent/qgame/animplayer/mix/MixRender.kt | 8 ++--- .../com/tencent/qgame/animplayer/mix/Src.kt | 31 ++++++++++++++++++- .../playerproj/player/AnimVapxDemoActivity.kt | 6 ++-- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/MixRender.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/MixRender.kt index 11583c80..b7e6e0f3 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/MixRender.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/MixRender.kt @@ -15,14 +15,11 @@ */ package com.tencent.qgame.animplayer.mix -import android.graphics.Bitmap import android.opengl.GLES11Ext import android.opengl.GLES20 -import android.opengl.GLUtils import com.tencent.qgame.animplayer.AnimConfig import com.tencent.qgame.animplayer.Constant import com.tencent.qgame.animplayer.PointRect -import com.tencent.qgame.animplayer.RenderConstant import com.tencent.qgame.animplayer.util.* /** @@ -63,7 +60,7 @@ class MixRender(private val mixAnimPlugin: MixAnimPlugin) { vertexArray.setVertexAttribPointer(shader.aPositionLocation) // src 纹理坐标 - srcArray.setArray(genSrcCoordsArray(srcArray.array, frame.frame.w, frame.frame.h, src.w, src.h, src.fitType)) + srcArray.setArray(genSrcCoordsArray(srcArray.array, frame.frame.w, frame.frame.h, src.drawWidth, src.drawHeight, src.fitType)) srcArray.setVertexAttribPointer(shader.aTextureSrcCoordinatesLocation) // 绑定 src纹理 GLES20.glActiveTexture(GLES20.GL_TEXTURE0) @@ -107,6 +104,9 @@ class MixRender(private val mixAnimPlugin: MixAnimPlugin) { } } + /** + * CENTER_FULL 并不是严格的centerCrop(centerCrop已经前置处理),此处主要是为防抖动做处理,复杂遮罩情况下需要固定src大小进行绘制防止抖动 + */ private fun genSrcCoordsArray(array: FloatArray, fw: Int, fh: Int, sw: Int, sh: Int, fitType: Src.FitType): FloatArray { return if (fitType == Src.FitType.CENTER_FULL) { if (fw <= sw && fh <= sh) { diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/Src.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/Src.kt index 779ec53a..e8d07580 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/Src.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/mix/Src.kt @@ -51,15 +51,21 @@ class Src { var srcId = "" var w = 0 var h = 0 + var drawWidth = 0 + var drawHeight = 0 var srcType = SrcType.UNKNOWN var loadType = LoadType.UNKNOWN var srcTag = "" - var bitmap: Bitmap? = null var txt = "" var style = Style.DEFAULT var color: Int = 0 var fitType = FitType.FIT_XY var srcTextureId = 0 + var bitmap: Bitmap? = null + set(value) { + field = value + genDrawSize(value) + } constructor(json: JSONObject) { srcId = json.getString("srcId") @@ -98,6 +104,29 @@ class Src { } + private fun genDrawSize(bitmap: Bitmap?) { + val bw = bitmap?.width?: w + val bh = bitmap?.height?: h + drawWidth = bw + drawHeight = bh + if (fitType == FitType.CENTER_FULL) { + if (w == 0 || h == 0) { + return + } + // 按src w h进行centerCrop处理 + val srcRate = w.toFloat() / h.toFloat() + val bitmapRate = bw.toFloat() / bh.toFloat() + + if (bitmapRate >= srcRate) { + drawHeight = h + drawWidth = (h * bitmapRate).toInt() + } else { + drawWidth = w + drawHeight = (w / bitmapRate).toInt() + } + } + } + override fun toString(): String { return "Src(srcId='$srcId', srcType=$srcType, loadType=$loadType, srcTag='$srcTag', bitmap=$bitmap, txt='$txt')" diff --git a/Android/PlayerProj/app/src/main/java/com/tencent/qgame/playerproj/player/AnimVapxDemoActivity.kt b/Android/PlayerProj/app/src/main/java/com/tencent/qgame/playerproj/player/AnimVapxDemoActivity.kt index ffa8f190..8cef90cb 100644 --- a/Android/PlayerProj/app/src/main/java/com/tencent/qgame/playerproj/player/AnimVapxDemoActivity.kt +++ b/Android/PlayerProj/app/src/main/java/com/tencent/qgame/playerproj/player/AnimVapxDemoActivity.kt @@ -103,8 +103,7 @@ class AnimVapxDemoActivity : Activity(), IAnimListener { * 比如:一个素材里需要显示多个头像,则需要定义多个不同的tag,表示不同位置,需要显示不同的头像,文字类似 */ val srcTag = resource.tag - - if (srcTag == "[sImg1]") { // 此tag是已经写入到动画配置中的tag + if (srcTag.isNotEmpty()) { val drawableId = if (head1Img) R.drawable.head1 else R.drawable.head2 head1Img = !head1Img val options = BitmapFactory.Options() @@ -121,8 +120,7 @@ class AnimVapxDemoActivity : Activity(), IAnimListener { override fun fetchText(resource: Resource, result: (String?) -> Unit) { val str = "恭喜 No.${1000 + Random().nextInt(8999)}用户 升神" val srcTag = resource.tag - - if (srcTag == "[sTxt1]") { // 此tag是已经写入到动画配置中的tag + if (srcTag.isNotEmpty()) { // 此tag是已经写入到动画配置中的tag result(str) } else { result(null)