Skip to content

Commit

Permalink
bug: 解决centerCrop问题 close #276
Browse files Browse the repository at this point in the history
  • Loading branch information
hexleo committed Nov 21, 2022
1 parent cb7418e commit 0c2b20b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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')"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 0c2b20b

Please sign in to comment.