feat: new frame style

This commit is contained in:
zhoubinjia committed 2026-01-08 13:52:57 +08:00
1 parent fddde1d201
commit e38ec1adee
9 files changed
+306 -148

No files matched your search

@@ -0,0 +1,66 @@
{
"id": "black_border",
"name": "品质黑框",
"version": 1,
"layout": {
"position": "BORDER",
"height": 200,
"backgroundColor": "#1E1E1E",
"padding": 60,
"borderWidth": 60
},
"elements": [
{
"type": "logo",
"logoType": "BRAND",
"alignment": "CENTER",
"size": 72,
"line": 0
},
{
"type": "text",
"textType": "DEVICE_MODEL",
"alignment": "CENTER",
"fontSize": 24,
"color": "#C6AE7A",
"fontWeight": "BOLD",
"line": 1
},
{
"type": "text",
"textType": "FOCAL_LENGTH_35MM",
"alignment": "CENTER",
"fontSize": 16,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
},
{
"type": "text",
"textType": "APERTURE",
"alignment": "CENTER",
"fontSize": 16,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
},
{
"type": "text",
"textType": "SHUTTER_SPEED",
"alignment": "CENTER",
"fontSize": 16,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
},
{
"type": "text",
"textType": "ISO",
"alignment": "CENTER",
"fontSize": 16,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
}
]
}
+2 -1
View File
@@ -3,8 +3,9 @@
"name": "复古胶片",
"version": 1,
"layout": {
"position": "BOTTOM",
"position": "BORDER",
"height": 64,
"borderWidth": 16,
"backgroundColor": "#2C2416",
"padding": 16
},
@@ -1,29 +0,0 @@
{
"id": "minimal_black",
"name": "极简黑框",
"version": 1,
"layout": {
"position": "BOTTOM",
"height": 60,
"backgroundColor": "#1A1A1A",
"padding": 20
},
"elements": [
{
"type": "text",
"textType": "DEVICE_MODEL",
"alignment": "START",
"fontSize": 14,
"color": "#FFFFFF",
"fontWeight": "MEDIUM"
},
{
"type": "text",
"textType": "DATE",
"alignment": "END",
"fontSize": 12,
"color": "#AAAAAA",
"format": "yyyy/MM/dd"
}
]
}
+65
View File
@@ -0,0 +1,65 @@
{
"id": "no_frame",
"name": "无框水印",
"version": 1,
"layout": {
"position": "OVERLAY",
"height": 150,
"backgroundColor": "#EE000000",
"padding": 20
},
"elements": [
{
"type": "logo",
"logoType": "BRAND",
"alignment": "CENTER",
"size": 44,
"line": 0
},
{
"type": "text",
"textType": "DEVICE_MODEL",
"alignment": "CENTER",
"fontSize": 16,
"color": "#FFFFFF",
"fontWeight": "MEDIUM",
"line": 1
},
{
"type": "text",
"textType": "FOCAL_LENGTH_35MM",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"fontWeight": "NORMAL",
"line": 2
},
{
"type": "text",
"textType": "APERTURE",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"fontWeight": "NORMAL",
"line": 2
},
{
"type": "text",
"textType": "SHUTTER_SPEED",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"fontWeight": "NORMAL",
"line": 2
},
{
"type": "text",
"textType": "ISO",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"fontWeight": "NORMAL",
"line": 2
}
]
}
+2 -1
View File
@@ -3,8 +3,9 @@
"name": "拍立得",
"version": 1,
"layout": {
"position": "BOTTOM",
"position": "BORDER",
"height": 120,
"borderWidth": 24,
"backgroundColor": "#FFFEF8",
"padding": 24
},
@@ -1,73 +0,0 @@
{
"id": "pro_camera",
"name": "专业相机",
"version": 1,
"layout": {
"position": "BOTTOM",
"height": 72,
"backgroundColor": "#0D0D0D",
"padding": 16
},
"elements": [
{
"type": "logo",
"logoType": "BRAND",
"alignment": "START",
"size": 48,
"tint": "#FF6600"
},
{
"type": "text",
"textType": "DEVICE_MODEL",
"alignment": "START",
"fontSize": 14,
"color": "#FF6600",
"fontWeight": "BOLD"
},
{
"type": "divider",
"orientation": "VERTICAL",
"alignment": "START",
"length": 20,
"thickness": 2,
"color": "#FF6600",
"margin": 10
},
{
"type": "text",
"textType": "FOCAL_LENGTH_35MM",
"alignment": "START",
"fontSize": 11,
"color": "#CCCCCC"
},
{
"type": "text",
"textType": "APERTURE",
"alignment": "START",
"fontSize": 11,
"color": "#CCCCCC"
},
{
"type": "text",
"textType": "SHUTTER_SPEED",
"alignment": "START",
"fontSize": 11,
"color": "#CCCCCC"
},
{
"type": "text",
"textType": "ISO",
"alignment": "START",
"fontSize": 11,
"color": "#CCCCCC"
},
{
"type": "text",
"textType": "DATETIME",
"alignment": "END",
"fontSize": 10,
"color": "#888888",
"format": "yyyy.MM.dd HH:mm"
}
]
}
@@ -47,7 +47,8 @@ data class FrameLayout(
val position: FramePosition = FramePosition.BOTTOM,
val heightDp: Int = 80,
val backgroundColor: Int = Color.WHITE,
val paddingDp: Int = 16
val paddingDp: Int = 16,
val borderWidthDp: Int = 0 // 四周边框宽度(仅 BORDER 模式使用)
)
/**
@@ -56,7 +57,9 @@ data class FrameLayout(
enum class FramePosition {
TOP,
BOTTOM,
BOTH
BOTH,
OVERLAY, // 叠加在图片上,不增加额外边框区域
BORDER // 四周边框 + 底部信息区
}
/**
@@ -86,7 +89,7 @@ sealed class FrameElement {
val alignment: ElementAlignment = ElementAlignment.CENTER,
val sizeDp: Int = 24,
val tint: Int? = null,
val marginDp: Int = 8,
val marginDp: Int = 0,
val line: Int = 0
) : FrameElement()
@@ -10,8 +10,6 @@ import com.hinnka.mycamera.gallery.PhotoMetadata
import java.text.SimpleDateFormat
import java.util.*
import androidx.core.graphics.createBitmap
import kotlin.math.max
import kotlin.math.min
/**
@@ -58,30 +56,73 @@ class FrameRenderer(private val context: Context) {
val frameHeight = (dpToPx(layout.heightDp) * scale).toInt()
val padding = (dpToPx(layout.paddingDp) * scale).toInt()
val borderWidth = (dpToPx(layout.borderWidthDp) * scale).toInt()
// 计算输出尺寸
val outputWidth = originalBitmap.width
val outputHeight = when (layout.position) {
FramePosition.BOTTOM -> originalBitmap.height + frameHeight
FramePosition.TOP -> originalBitmap.height + frameHeight
FramePosition.BOTH -> originalBitmap.height + frameHeight * 2
val outputWidth: Int
val outputHeight: Int
when (layout.position) {
FramePosition.BOTTOM -> {
outputWidth = originalBitmap.width
outputHeight = originalBitmap.height + frameHeight
}
FramePosition.TOP -> {
outputWidth = originalBitmap.width
outputHeight = originalBitmap.height + frameHeight
}
FramePosition.BOTH -> {
outputWidth = originalBitmap.width
outputHeight = originalBitmap.height + frameHeight * 2
}
FramePosition.OVERLAY -> {
outputWidth = originalBitmap.width
outputHeight = originalBitmap.height
}
FramePosition.BORDER -> {
// 四周边框 + 底部信息区
outputWidth = originalBitmap.width + borderWidth * 2
outputHeight = originalBitmap.height + borderWidth * 2 + frameHeight
}
}
// 创建输出 Bitmap
val output = createBitmap(outputWidth, outputHeight)
val canvas = Canvas(output)
// 绘制背景
backgroundPaint.color = layout.backgroundColor
canvas.drawRect(0f, 0f, outputWidth.toFloat(), outputHeight.toFloat(), backgroundPaint)
// OVERLAY 模式不需要绘制整体背景
if (layout.position != FramePosition.OVERLAY) {
backgroundPaint.color = layout.backgroundColor
canvas.drawRect(0f, 0f, outputWidth.toFloat(), outputHeight.toFloat(), backgroundPaint)
}
// 绘制原图
val photoTop = when (layout.position) {
FramePosition.BOTTOM -> 0f
FramePosition.TOP -> frameHeight.toFloat()
FramePosition.BOTH -> frameHeight.toFloat()
val photoLeft: Float
val photoTop: Float
when (layout.position) {
FramePosition.BOTTOM -> {
photoLeft = 0f
photoTop = 0f
}
FramePosition.TOP -> {
photoLeft = 0f
photoTop = frameHeight.toFloat()
}
FramePosition.BOTH -> {
photoLeft = 0f
photoTop = frameHeight.toFloat()
}
FramePosition.OVERLAY -> {
photoLeft = 0f
photoTop = 0f
}
FramePosition.BORDER -> {
photoLeft = borderWidth.toFloat()
photoTop = borderWidth.toFloat()
}
}
canvas.drawBitmap(originalBitmap, 0f, photoTop, null)
canvas.drawBitmap(originalBitmap, photoLeft, photoTop, null)
// 绘制边框内容
when (layout.position) {
@@ -92,7 +133,6 @@ class FrameRenderer(private val context: Context) {
top = originalBitmap.height.toFloat(),
right = (outputWidth - padding).toFloat(),
bottom = outputHeight.toFloat(),
frameHeight = frameHeight,
scale = scale
)
}
@@ -103,7 +143,6 @@ class FrameRenderer(private val context: Context) {
top = 0f,
right = (outputWidth - padding).toFloat(),
bottom = frameHeight.toFloat(),
frameHeight = frameHeight,
scale = scale
)
}
@@ -115,7 +154,6 @@ class FrameRenderer(private val context: Context) {
top = 0f,
right = (outputWidth - padding).toFloat(),
bottom = frameHeight.toFloat(),
frameHeight = frameHeight,
scale = scale
)
// 底部
@@ -125,7 +163,44 @@ class FrameRenderer(private val context: Context) {
top = (originalBitmap.height + frameHeight).toFloat(),
right = (outputWidth - padding).toFloat(),
bottom = outputHeight.toFloat(),
frameHeight = frameHeight,
scale = scale
)
}
FramePosition.OVERLAY -> {
// 叠加模式:绘制从全透明到半透明的渐变背景
val overlayTop = (originalBitmap.height - frameHeight).toFloat()
// 创建线性渐变:从顶部全透明到底部半透明
val gradientShader = LinearGradient(
0f, overlayTop,
0f, outputHeight.toFloat(),
Color.TRANSPARENT,
layout.backgroundColor,
Shader.TileMode.CLAMP
)
backgroundPaint.shader = gradientShader
canvas.drawRect(0f, overlayTop, outputWidth.toFloat(), outputHeight.toFloat(), backgroundPaint)
backgroundPaint.shader = null // 重置 shader
// 绘制水印内容
drawFrameContent(
canvas, template.elements, metadata, showAppBranding,
left = padding.toFloat(),
top = overlayTop + padding.toFloat(),
right = (outputWidth - padding).toFloat(),
bottom = outputHeight.toFloat() - padding.toFloat(),
scale = scale
)
}
FramePosition.BORDER -> {
// 四周边框模式:底部信息区
val infoTop = (originalBitmap.height + borderWidth * 2).toFloat()
drawFrameContent(
canvas, template.elements, metadata, showAppBranding,
left = padding.toFloat(),
top = infoTop,
right = (outputWidth - padding).toFloat(),
bottom = outputHeight.toFloat() - padding.toFloat(),
scale = scale
)
}
@@ -143,7 +218,6 @@ class FrameRenderer(private val context: Context) {
top: Float,
right: Float,
bottom: Float,
frameHeight: Int,
scale: Float = 1f
) {
// 将元素按对齐方式分组
@@ -151,25 +225,43 @@ class FrameRenderer(private val context: Context) {
val centerElements = elements.filter { getAlignment(it) == ElementAlignment.CENTER }
val endElements = elements.filter { getAlignment(it) == ElementAlignment.END }
// 获取所有行号以计算行数
val allLines = elements.map { getLine(it) }.filter { it >= 0 }.distinct().sorted()
val lineCount = allLines.size
val height = bottom - top
/**
* 绘制元素组
* 计算指定行号的垂直中心位置
*/
fun drawGroup(groupElements: List<FrameElement>, initialX: Float, leftToRight: Boolean) {
val lines = groupElements.map { getLine(it) }.filter { it >= 0 }.distinct()
val hasMultipleLines = lines.size > 1
fun getLineCenterY(line: Int): Float {
return when {
line == -1 -> top + height / 2f
lineCount <= 1 -> top + height / 2f
lineCount == 2 -> when (line) {
0 -> top + height * 0.35f
1 -> top + height * 0.65f
else -> top + height / 2f
}
lineCount >= 3 -> when (line) {
0 -> top + height * 0.22f
1 -> top + height * 0.50f
2 -> top + height * 0.78f
else -> top + height / 2f
}
else -> top + height / 2f
}
}
/**
* 绘制左对齐或右对齐的元素组
*/
fun drawAlignedGroup(groupElements: List<FrameElement>, initialX: Float, leftToRight: Boolean) {
val currentXPerLine = mutableMapOf<Int, Float>()
for (element in if (leftToRight) groupElements else groupElements.reversed()) {
val line = getLine(element)
val centerY = when {
line == -1 -> top + frameHeight / 2f
!hasMultipleLines -> top + frameHeight / 2f
line == 0 -> top + frameHeight * 0.35f
line == 1 -> top + frameHeight * 0.65f
else -> top + frameHeight / 2f
}
val centerY = getLineCenterY(line)
val x = currentXPerLine.getOrDefault(line, initialX)
val width = drawElement(canvas, element, metadata, showAppBranding, x, centerY, leftToRight, scale)
@@ -177,25 +269,55 @@ class FrameRenderer(private val context: Context) {
val nextX = if (leftToRight) x + width else x - width
if (line == -1) {
// 全局居中元素,推进所有行的 X 坐标
// 全局元素,推进所有行的 X 坐标
currentXPerLine[-1] = nextX
currentXPerLine[0] = nextX
currentXPerLine[1] = nextX
currentXPerLine[2] = nextX
} else {
currentXPerLine[line] = nextX
}
}
}
/**
* 绘制居中对齐的元素组(每行独立居中)
*/
fun drawCenteredGroup(groupElements: List<FrameElement>) {
// 按行分组
val elementsByLine = groupElements.groupBy { getLine(it) }
val availableWidth = right - left
val elementSpacing = dpToPx(8) * scale // 元素间距
for ((line, lineElements) in elementsByLine) {
// 计算该行的总宽度(扣除最后一个元素的间距)
val lineWidth = lineElements.sumOf {
measureElementWidth(it, metadata, showAppBranding, scale).toDouble()
}.toFloat() - elementSpacing // 减去最后一个元素多余的间距
// 计算该行的起始 X 位置(居中)
val startX = left + (availableWidth - lineWidth) / 2f
val centerY = getLineCenterY(line)
// 绘制该行的所有元素
var currentX = startX
for ((index, element) in lineElements.withIndex()) {
val isLast = index == lineElements.size - 1
val width = drawElement(canvas, element, metadata, showAppBranding, currentX, centerY, true, scale)
// 最后一个元素不加间距
currentX += if (isLast) (width - elementSpacing) else width
}
}
}
// 绘制左侧元素
drawGroup(startElements, left, true)
drawAlignedGroup(startElements, left, true)
// 绘制右侧元素
drawGroup(endElements, right, false)
drawAlignedGroup(endElements, right, false)
// 绘制中间元素
val centerWidth = measureElementsWidth(centerElements, metadata, showAppBranding, scale)
drawGroup(centerElements, left + (right - left - centerWidth) / 2f, true)
// 绘制中间元素(每行独立居中)
drawCenteredGroup(centerElements)
}
/**
@@ -241,6 +363,7 @@ class FrameRenderer(private val context: Context) {
xPerLine[-1] = max
xPerLine[0] = max
xPerLine[1] = max
xPerLine[2] = max
} else {
val current = xPerLine.getOrDefault(line, 0f)
xPerLine[line] = current + width
@@ -266,7 +389,7 @@ class FrameRenderer(private val context: Context) {
textPaint.measureText(text) + dpToPx(8) * scale
}
is FrameElement.Logo -> {
(dpToPx(element.sizeDp) + dpToPx(8)) * scale
(dpToPx(element.sizeDp) + dpToPx(8)) * scale + dpToPx(element.marginDp) * scale * 2
}
is FrameElement.Divider -> {
if (element.orientation == DividerOrientation.VERTICAL) {
@@ -107,7 +107,8 @@ object FrameTemplateParser {
position = FramePosition.valueOf(obj.optString("position", "BOTTOM")),
heightDp = obj.optInt("height", 80),
backgroundColor = parseColor(obj.optString("backgroundColor", "#FFFFFF")),
paddingDp = obj.optInt("padding", 16)
paddingDp = obj.optInt("padding", 16),
borderWidthDp = obj.optInt("borderWidth", 0)
)
}