feat: implement visual color picker with opacity support

This commit is contained in:
zhoubinjia committed 2026-05-13 18:56:05 +08:00
1 parent 1cf7261cf2
commit adb35924b8
7 files changed
+152

No files matched your search

@@ -70,6 +70,11 @@ data class FrameLayout(
val lineSpacingDp: Int = 8, // 行间距
val paddingDp: Int = 16,
val borderWidthDp: Int = 0, // 四周边框宽度(仅 BORDER 模式使用)
val photoShadowEnabled: Boolean = false,
val photoShadowRadiusDp: Int = 0,
val photoShadowOffsetXDp: Int = 0,
val photoShadowOffsetYDp: Int = 2,
val photoShadowColor: Int = 0xCC000000.toInt(),
val imageResName: String? = null, // 边框图片资源名称(仅 IMAGE 模式使用,内置资源)
val imagePath: String? = null // 边框图片文件路径(仅 IMAGE 模式使用,外部导入)
)
@@ -1,6 +1,7 @@
package com.hinnka.mycamera.frame
import android.graphics.Color
import androidx.compose.runtime.snapshots.toInt
import java.util.UUID
/**
@@ -160,6 +161,11 @@ data class FrameLayoutDraft(
val lineSpacingDp: Int = 8,
val paddingDp: Int = 16,
val borderWidthDp: Int = 0,
val photoShadowEnabled: Boolean = false,
val photoShadowRadiusDp: Int = 0,
val photoShadowOffsetXDp: Int = 0,
val photoShadowOffsetYDp: Int = 2,
val photoShadowColor: Int = 0xCC000000.toInt(),
val imageResName: String? = null,
val imagePath: String? = null
) {
@@ -171,6 +177,11 @@ data class FrameLayoutDraft(
lineSpacingDp = lineSpacingDp,
paddingDp = paddingDp.coerceAtLeast(0),
borderWidthDp = borderWidthDp.coerceAtLeast(0),
photoShadowEnabled = photoShadowEnabled,
photoShadowRadiusDp = photoShadowRadiusDp.coerceAtLeast(0),
photoShadowOffsetXDp = photoShadowOffsetXDp,
photoShadowOffsetYDp = photoShadowOffsetYDp,
photoShadowColor = photoShadowColor,
imageResName = imageResName,
imagePath = imagePath
)
@@ -184,6 +195,11 @@ data class FrameLayoutDraft(
lineSpacingDp = layout.lineSpacingDp,
paddingDp = layout.paddingDp,
borderWidthDp = layout.borderWidthDp,
photoShadowEnabled = layout.photoShadowEnabled,
photoShadowRadiusDp = layout.photoShadowRadiusDp,
photoShadowOffsetXDp = layout.photoShadowOffsetXDp,
photoShadowOffsetYDp = layout.photoShadowOffsetYDp,
photoShadowColor = layout.photoShadowColor,
imageResName = layout.imageResName,
imagePath = layout.imagePath
)
@@ -36,6 +36,7 @@ class FrameRenderer(
private val backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val photoShadowPaint = Paint(Paint.ANTI_ALIAS_FLAG)
/**
* 渲染带边框的照片
@@ -174,6 +175,16 @@ class FrameRenderer(
photoTop = borderWidth.toFloat()
}
}
drawPhotoShadowIfNeeded(
canvas = canvas,
layout = layout,
photoLeft = photoLeft,
photoTop = photoTop,
photoWidth = originalBitmap.width.toFloat(),
photoHeight = originalBitmap.height.toFloat(),
borderWidth = borderWidth,
scale = scale
)
canvas.drawBitmap(originalBitmap, photoLeft, photoTop, null)
// 绘制边框内容
@@ -266,6 +277,42 @@ class FrameRenderer(
return output
}
private fun drawPhotoShadowIfNeeded(
canvas: Canvas,
layout: FrameLayout,
photoLeft: Float,
photoTop: Float,
photoWidth: Float,
photoHeight: Float,
borderWidth: Int,
scale: Float
) {
val supportsBorderShadow = borderWidth > 0 &&
(layout.position == FramePosition.BORDER || layout.position == FramePosition.BOTH)
if (!supportsBorderShadow || !layout.photoShadowEnabled) return
val shadowAlpha = (layout.photoShadowColor ushr 24) and 0xFF
if (shadowAlpha == 0) return
val radius = dpToPx(layout.photoShadowRadiusDp.coerceAtLeast(0)).toFloat() * scale
val offsetX = dpToPx(layout.photoShadowOffsetXDp) * scale
val offsetY = dpToPx(layout.photoShadowOffsetYDp) * scale
photoShadowPaint.reset()
photoShadowPaint.isAntiAlias = true
photoShadowPaint.color = Color.WHITE
photoShadowPaint.setShadowLayer(radius, offsetX, offsetY, layout.photoShadowColor)
canvas.drawRect(
photoLeft,
photoTop,
photoLeft + photoWidth,
photoTop + photoHeight,
photoShadowPaint
)
photoShadowPaint.clearShadowLayer()
}
private fun drawFrameContent(
canvas: Canvas,
elements: List<FrameElement>,
@@ -2,6 +2,7 @@ package com.hinnka.mycamera.frame
import android.content.Context
import android.graphics.Color
import androidx.compose.runtime.snapshots.toInt
import com.hinnka.mycamera.utils.PLog
import org.json.JSONArray
import org.json.JSONObject
@@ -162,6 +163,21 @@ object FrameTemplateParser {
if (template.layout.borderWidthDp > 0) {
put("borderWidth", template.layout.borderWidthDp)
}
if (template.layout.photoShadowEnabled) {
put("photoShadowEnabled", true)
}
if (template.layout.photoShadowRadiusDp > 0) {
put("photoShadowRadius", template.layout.photoShadowRadiusDp)
}
if (template.layout.photoShadowOffsetXDp != 0) {
put("photoShadowOffsetX", template.layout.photoShadowOffsetXDp)
}
if (template.layout.photoShadowOffsetYDp != 2) {
put("photoShadowOffsetY", template.layout.photoShadowOffsetYDp)
}
if (template.layout.photoShadowColor != 0xCC000000.toInt()) {
put("photoShadowColor", colorToHex(template.layout.photoShadowColor))
}
template.layout.imageResName?.let { put("imageResName", it) }
template.layout.imagePath?.let { put("imagePath", it) }
})
@@ -191,6 +207,7 @@ object FrameTemplateParser {
if (template.layout.heightDp < 0) errors += "layout.height"
if (template.layout.paddingDp < 0) errors += "layout.padding"
if (template.layout.borderWidthDp < 0) errors += "layout.borderWidth"
if (template.layout.photoShadowRadiusDp < 0) errors += "layout.photoShadowRadius"
if (template.layout.position == FramePosition.IMAGE &&
template.layout.imageResName.isNullOrBlank() &&
template.layout.imagePath.isNullOrBlank()
@@ -246,6 +263,11 @@ object FrameTemplateParser {
lineSpacingDp = obj.optInt("lineSpacing", 8),
paddingDp = obj.optInt("padding", 16),
borderWidthDp = obj.optInt("borderWidth", 0),
photoShadowEnabled = obj.optBoolean("photoShadowEnabled", false),
photoShadowRadiusDp = obj.optInt("photoShadowRadius", 0),
photoShadowOffsetXDp = obj.optInt("photoShadowOffsetX", 0),
photoShadowOffsetYDp = obj.optInt("photoShadowOffsetY", 2),
photoShadowColor = parseColor(obj.optString("photoShadowColor", "#CC000000")),
imageResName = obj.optString("imageResName").takeIf { it.isNotEmpty() },
imagePath = obj.optString("imagePath").takeIf { it.isNotEmpty() }
)
@@ -593,6 +593,58 @@ private fun FrameBasicTab(
onDraftChange(draft.copy(layout = draft.layout.copy(borderWidthDp = it.coerceAtLeast(0))))
}
)
if (draft.layout.borderWidthDp > 0) {
SwitchRow(
label = stringResource(R.string.frame_editor_photo_shadow),
checked = draft.layout.photoShadowEnabled,
onCheckedChange = {
onDraftChange(
draft.copy(
layout = draft.layout.copy(
photoShadowEnabled = it,
photoShadowRadiusDp = if (it && draft.layout.photoShadowRadiusDp == 0) {
36
} else {
draft.layout.photoShadowRadiusDp
}
)
)
)
}
)
if (draft.layout.photoShadowEnabled) {
IntField(
label = stringResource(R.string.frame_editor_photo_shadow_radius),
value = draft.layout.photoShadowRadiusDp,
onValueChange = {
onDraftChange(draft.copy(layout = draft.layout.copy(photoShadowRadiusDp = it.coerceAtLeast(0))))
}
)
IntField(
label = stringResource(R.string.frame_editor_photo_shadow_offset_x),
value = draft.layout.photoShadowOffsetXDp,
allowNegative = true,
onValueChange = {
onDraftChange(draft.copy(layout = draft.layout.copy(photoShadowOffsetXDp = it)))
}
)
IntField(
label = stringResource(R.string.frame_editor_photo_shadow_offset_y),
value = draft.layout.photoShadowOffsetYDp,
allowNegative = true,
onValueChange = {
onDraftChange(draft.copy(layout = draft.layout.copy(photoShadowOffsetYDp = it)))
}
)
ColorField(
label = stringResource(R.string.frame_editor_photo_shadow_color),
value = draft.layout.photoShadowColor,
onValueChange = {
onDraftChange(draft.copy(layout = draft.layout.copy(photoShadowColor = it)))
}
)
}
}
}
} else {
Text(
@@ -505,6 +505,11 @@
<string name="frame_editor_color_opacity">透明度</string>
<string name="frame_editor_layout_line_spacing">行间距 (dp)</string>
<string name="frame_editor_layout_border_width">边框宽度 (dp)</string>
<string name="frame_editor_photo_shadow">照片阴影</string>
<string name="frame_editor_photo_shadow_radius">阴影模糊 (dp)</string>
<string name="frame_editor_photo_shadow_offset_x">阴影 X 偏移 (dp)</string>
<string name="frame_editor_photo_shadow_offset_y">阴影 Y 偏移 (dp)</string>
<string name="frame_editor_photo_shadow_color">阴影颜色</string>
<string name="frame_editor_image_hint">图片边框使用带透明区域的 PNG 或 WebP。选中的素材会被复制到应用私有目录。</string>
<string name="frame_editor_pick_image">选择边框图片</string>
<string name="frame_editor_image_source_path">当前图片:%s</string>
+5
View File
@@ -515,6 +515,11 @@
<string name="frame_editor_color_opacity">Opacity</string>
<string name="frame_editor_layout_line_spacing">Line Spacing (dp)</string>
<string name="frame_editor_layout_border_width">Border Width (dp)</string>
<string name="frame_editor_photo_shadow">Photo Shadow</string>
<string name="frame_editor_photo_shadow_radius">Shadow Blur (dp)</string>
<string name="frame_editor_photo_shadow_offset_x">Shadow X Offset (dp)</string>
<string name="frame_editor_photo_shadow_offset_y">Shadow Y Offset (dp)</string>
<string name="frame_editor_photo_shadow_color">Shadow Color</string>
<string name="frame_editor_image_hint">Image frames use a transparent PNG or WebP. The selected asset will be copied into app storage.</string>
<string name="frame_editor_pick_image">Choose Frame Image</string>
<string name="frame_editor_image_source_path">Image source: %s</string>