feat: 新增 Row 权重与网络图片验证页,图片改为客户端预取缓存
服务端:spike 页验证 Row/horizontalWeight 与 ContentScale;图片以 URL 下发,通过 X-Remote-Assets 提供清单,并从 RcScopeImpl 取 writer 用真实尺寸登记 addBitmapUrl,修复播放端 1x1 尺寸校验失败。Android:文档声明的图片先异步预取到内存 LRU 与本地文件缓存,自定义 BitmapLoader 同步读内存,消除 NetworkOnMainThreadException;缺图时用透明占位图避免整页黑屏。
This commit is contained in:
1 parent
8cc8107222
commit
244e539b47
6 files changed
+457
-15
No files matched your search
@@ -22,7 +22,7 @@ spec:
|
||||
spec:
|
||||
containers:
|
||||
- name: server
|
||||
image: composeremote/server:1.0.0-list
|
||||
image: composeremote/server:1.0.0-imgcache5
|
||||
imagePullPolicy: Never
|
||||
ports:
|
||||
- name: http
|
||||
|
||||
+42
-2
@@ -1,11 +1,20 @@
|
||||
package com.ttstd.composeremote.server.controller;
|
||||
|
||||
import com.ttstd.composeremote.server.RemoteDocumentBuilderKt;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 服务端驱动 UI 接口。
|
||||
@@ -17,6 +26,9 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
* </ul>
|
||||
* 返回 {@code application/octet-stream} 的 Remote Compose 二进制文档,
|
||||
* 由 Android 端 {@code RemoteComposePlayer} 原生渲染。
|
||||
*
|
||||
* <p>页面引用图片时,通过 {@code X-Remote-Assets} 响应头下发图片清单,
|
||||
* 播放端先异步预取并缓存,再交给播放器渲染。
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/remote-ui")
|
||||
@@ -26,8 +38,36 @@ public class RemoteUIController {
|
||||
value = {"/{page}", ""},
|
||||
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
|
||||
)
|
||||
public byte[] getPage(@PathVariable(name = "page", required = false) String page) {
|
||||
public ResponseEntity<byte[]> getPage(@PathVariable(name = "page", required = false) String page) {
|
||||
String target = (page == null || page.isBlank()) ? "home" : page;
|
||||
return RemoteDocumentBuilderKt.buildRemoteDocument(target);
|
||||
byte[] document = RemoteDocumentBuilderKt.buildRemoteDocument(target);
|
||||
List<String> assets = RemoteDocumentBuilderKt.buildRemoteDocumentAssets(target);
|
||||
|
||||
ResponseEntity.BodyBuilder builder = ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
if (!assets.isEmpty()) {
|
||||
builder.header("X-Remote-Assets", String.join(",", assets));
|
||||
}
|
||||
return builder.body(document);
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面引用的图片资源。
|
||||
*
|
||||
* <p>图片 URL 带内容标识时,可以在此长期缓存;当前先固定 30 天。
|
||||
*/
|
||||
@GetMapping(value = "/assets/{name:.+}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<byte[]> getAsset(@PathVariable("name") String name) throws IOException {
|
||||
if (!name.matches("[A-Za-z0-9._-]+")) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
ClassPathResource resource = new ClassPathResource("static/api/" + name);
|
||||
if (!resource.exists()) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_PNG)
|
||||
.cacheControl(CacheControl.maxAge(Duration.ofDays(30)).cachePublic())
|
||||
.body(resource.getContentAsByteArray());
|
||||
}
|
||||
}
|
||||
+246
@@ -4,6 +4,7 @@ import androidx.compose.remote.core.CoreDocument
|
||||
import androidx.compose.remote.creation.JvmRcPlatformServices
|
||||
import androidx.compose.remote.creation.RemoteComposeWriter
|
||||
import androidx.compose.remote.creation.dsl.Modifier
|
||||
import androidx.compose.remote.creation.dsl.RcContentScale
|
||||
import androidx.compose.remote.creation.dsl.RcDensityBehavior
|
||||
import androidx.compose.remote.creation.dsl.RcDp
|
||||
import androidx.compose.remote.creation.dsl.RcHeaderTag
|
||||
@@ -19,8 +20,10 @@ import androidx.compose.remote.creation.dsl.height
|
||||
import androidx.compose.remote.creation.dsl.onClick
|
||||
import androidx.compose.remote.creation.dsl.padding
|
||||
import androidx.compose.remote.creation.dsl.verticalScroll
|
||||
import androidx.compose.remote.creation.dsl.width
|
||||
import androidx.compose.remote.creation.profile.Profile
|
||||
import androidx.compose.remote.creation.profile.RemoteComposeWriterFactory
|
||||
import javax.imageio.ImageIO
|
||||
|
||||
/**
|
||||
* 文档逻辑画布尺寸,单位 dp。
|
||||
@@ -60,6 +63,9 @@ fun buildRemoteDocument(page: String): ByteArray {
|
||||
)
|
||||
|
||||
return createRcBuffer(rcProfile, *header) {
|
||||
// URL 图片需要底层 writer 登记真实尺寸,见 composeWriter() 的说明。
|
||||
val docWriter = composeWriter()
|
||||
|
||||
// 跳转目标以文本形式随文档下发,播放端只负责把 metadata 当作页面 id 使用。
|
||||
val homeTarget = remoteText("home")
|
||||
val detailTarget = remoteText("detail")
|
||||
@@ -71,12 +77,29 @@ fun buildRemoteDocument(page: String): ByteArray {
|
||||
when (page) {
|
||||
"detail" -> detailContent(homeTarget)
|
||||
"list" -> listContent(homeTarget)
|
||||
"spike" -> spikeContent(homeTarget)
|
||||
"spike-image" -> imageSpikeContent(homeTarget, docWriter)
|
||||
else -> homeContent(detailTarget, listTarget)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出 DSL 背后的 [RemoteComposeWriter]。
|
||||
*
|
||||
* `createRcBuffer` 只把 [RcScope] 交给内容 lambda,不会调用 Profile 里的 writer 工厂;
|
||||
* 而 URL 图片必须以真实尺寸登记到 writer(DSL 的 `remoteBitmapUrl` 固定按 1×1 写入,
|
||||
* 播放端解码时会因尺寸不匹配报 `dimensions don't match`)。
|
||||
* alpha24 没有公开入口,这里读 [androidx.compose.remote.creation.dsl.RcScopeImpl]
|
||||
* 的 writer 字段;取不到时返回 null,退回 1×1 的旧行为。
|
||||
*/
|
||||
private fun RcScope.composeWriter(): RemoteComposeWriter? = runCatching {
|
||||
val field = javaClass.getDeclaredField("writer")
|
||||
field.isAccessible = true
|
||||
field.get(this) as RemoteComposeWriter
|
||||
}.getOrNull()
|
||||
|
||||
private fun RcScope.homeContent(detailTarget: RcText, listTarget: RcText) {
|
||||
Text(
|
||||
text = "Remote Compose",
|
||||
@@ -118,6 +141,22 @@ private fun RcScope.homeContent(detailTarget: RcText, listTarget: RcText) {
|
||||
target = detailTarget,
|
||||
actionId = ACTION_OPEN_ACTIVITY
|
||||
)
|
||||
// 临时入口:Spike 验证完成后删除。
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
card(
|
||||
title = "Spike",
|
||||
body = "Row + weight 渲染验证页。",
|
||||
hint = "进入验证页 →",
|
||||
target = remoteText("spike")
|
||||
)
|
||||
// 临时入口:图片验证完成后删除。
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
card(
|
||||
title = "图片",
|
||||
body = "网络图片加载与 ImageScale 裁切验证。",
|
||||
hint = "进入验证页 →",
|
||||
target = remoteText("spike-image")
|
||||
)
|
||||
}
|
||||
|
||||
private fun RcScope.detailContent(homeTarget: RcText) {
|
||||
@@ -198,6 +237,184 @@ private fun RcScope.listItem(index: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spike 页面:验证 Row + weight 的渲染效果,以及「无固定高度」时的自适应能力。
|
||||
*
|
||||
* 用于评估「相对坐标 → 行分组 → Row/horizontalWeight」这套编译方案是否可行,
|
||||
* 验证完成后可以整体删除。
|
||||
*/
|
||||
private fun RcScope.spikeContent(homeTarget: RcText) {
|
||||
Text(text = "Spike · Row 权重", fontSize = RcSp(22f), color = TITLE_COLOR)
|
||||
Spacer(Modifier.height(RcDp(4f)))
|
||||
Text(
|
||||
text = "下面三行分别验证:1:2:3 权重、12 等分、带间距的列",
|
||||
fontSize = RcSp(13f),
|
||||
color = BODY_COLOR
|
||||
)
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
// 1) 权重比 1 : 2 : 3
|
||||
Row(Modifier.fillMaxWidth()) {
|
||||
Box(Modifier.weight(1f).height(RcDp(56f)).background(SPIKE_RED)) {
|
||||
Text(text = "1", fontSize = RcSp(16f), color = WHITE)
|
||||
}
|
||||
Box(Modifier.weight(2f).height(RcDp(56f)).background(SPIKE_GREEN)) {
|
||||
Text(text = "2", fontSize = RcSp(16f), color = WHITE)
|
||||
}
|
||||
Box(Modifier.weight(3f).height(RcDp(56f)).background(SPIKE_BLUE)) {
|
||||
Text(text = "3", fontSize = RcSp(16f), color = WHITE)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
// 2) 12 等分(模拟 12 列网格)
|
||||
Row(Modifier.fillMaxWidth()) {
|
||||
repeat(12) { index ->
|
||||
Box(
|
||||
Modifier
|
||||
.weight(1f)
|
||||
.height(RcDp(24f))
|
||||
.background(if (index % 2 == 0) SPIKE_BLUE else SPIKE_GREEN)
|
||||
) {}
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
// 3) 三列 + 8dp 间距
|
||||
Row(Modifier.fillMaxWidth()) {
|
||||
Box(Modifier.weight(1f).height(RcDp(48f)).background(SPIKE_RED)) {}
|
||||
Spacer(Modifier.width(RcDp(8f)))
|
||||
Box(Modifier.weight(1f).height(RcDp(48f)).background(SPIKE_GREEN)) {}
|
||||
Spacer(Modifier.width(RcDp(8f)))
|
||||
Box(Modifier.weight(1f).height(RcDp(48f)).background(SPIKE_BLUE)) {}
|
||||
}
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
// 4) 不写高度:验证文字卡片能否按内容自适应高度
|
||||
Row(Modifier.fillMaxWidth()) {
|
||||
Box(Modifier.weight(1f).background(CARD_BG).padding(RcDp(10f))) {
|
||||
Column(Modifier.fillMaxWidth()) {
|
||||
Text(text = "自适应 A", fontSize = RcSp(14f), color = TITLE_COLOR)
|
||||
Spacer(Modifier.height(RcDp(4f)))
|
||||
Text(
|
||||
text = "两行文字,用来观察高度是否随内容撑开。",
|
||||
fontSize = RcSp(12f),
|
||||
color = BODY_COLOR
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.width(RcDp(8f)))
|
||||
Box(Modifier.weight(1f).background(CARD_BG).padding(RcDp(10f))) {
|
||||
Column(Modifier.fillMaxWidth()) {
|
||||
Text(text = "自适应 B", fontSize = RcSp(14f), color = TITLE_COLOR)
|
||||
Spacer(Modifier.height(RcDp(4f)))
|
||||
Text(
|
||||
text = "这段文字更长一些,用来验证同一行内两个卡片的高度和对齐表现。",
|
||||
fontSize = RcSp(12f),
|
||||
color = BODY_COLOR
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
// 5) 嵌套:Row 里再放 Column,模拟网格单元内部结构
|
||||
Row(Modifier.fillMaxWidth()) {
|
||||
Box(Modifier.weight(2f).height(RcDp(72f)).background(SPIKE_PURPLE).padding(RcDp(8f))) {
|
||||
Column(Modifier.fillMaxWidth()) {
|
||||
Text(text = "嵌套 Row > Column", fontSize = RcSp(13f), color = WHITE)
|
||||
Spacer(Modifier.height(RcDp(4f)))
|
||||
Text(text = "2/3 宽度", fontSize = RcSp(11f), color = WHITE)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.width(RcDp(8f)))
|
||||
Box(Modifier.weight(1f).height(RcDp(72f)).background(SPIKE_ORANGE)) {
|
||||
Text(text = "1/3", fontSize = RcSp(13f), color = WHITE)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(RcDp(16f)))
|
||||
|
||||
card(
|
||||
title = "返回",
|
||||
body = "Spike 验证完成后回到首页。",
|
||||
hint = "点击返回首页 →",
|
||||
target = homeTarget
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Spike 页面:验证网络图片加载与 contentScale 的表现。
|
||||
*
|
||||
* 图片以 URL 形式随文档下发,由播放端预取并缓存后渲染;
|
||||
* 验证完成后可整体删除。
|
||||
*/
|
||||
private fun RcScope.imageSpikeContent(homeTarget: RcText, writer: RemoteComposeWriter?) {
|
||||
// DSL 的 remoteBitmapUrl(url) 会把声明尺寸写成 1×1,播放端解码后尺寸校验会失败;
|
||||
// 先用 writer 按真实尺寸注册同一个 URL,后续 remoteBitmapUrl 会复用已注册的 id。
|
||||
val (imageWidth, imageHeight) = spikeImageSize
|
||||
writer?.addBitmapUrl(SPIKE_IMAGE_URL, imageWidth, imageHeight)
|
||||
val bitmap = remoteBitmapUrl(SPIKE_IMAGE_URL)
|
||||
|
||||
Text(text = "Spike · 图片", fontSize = RcSp(22f), color = TITLE_COLOR)
|
||||
Spacer(Modifier.height(RcDp(4f)))
|
||||
Text(
|
||||
text = "原图 800×450,下面分别用 Fit / Crop / FillBounds 渲染",
|
||||
fontSize = RcSp(13f),
|
||||
color = BODY_COLOR
|
||||
)
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
Text(text = "1) Fit:完整显示,可能留边", fontSize = RcSp(13f), color = ACCENT_COLOR)
|
||||
Spacer(Modifier.height(RcDp(6f)))
|
||||
Image(
|
||||
bitmap,
|
||||
Modifier.fillMaxWidth().height(RcDp(160f)).background(CARD_BG),
|
||||
"fit",
|
||||
RcContentScale.Fit,
|
||||
1f
|
||||
)
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
Text(text = "2) Crop:填满容器,超出裁切", fontSize = RcSp(13f), color = ACCENT_COLOR)
|
||||
Spacer(Modifier.height(RcDp(6f)))
|
||||
Image(
|
||||
bitmap,
|
||||
Modifier.fillMaxWidth().height(RcDp(120f)),
|
||||
"crop",
|
||||
RcContentScale.Crop,
|
||||
1f
|
||||
)
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
Text(text = "3) 两列 + FillBounds(拉伸)", fontSize = RcSp(13f), color = ACCENT_COLOR)
|
||||
Spacer(Modifier.height(RcDp(6f)))
|
||||
Row(Modifier.fillMaxWidth()) {
|
||||
Image(
|
||||
bitmap,
|
||||
Modifier.weight(1f).height(RcDp(90f)),
|
||||
"left",
|
||||
RcContentScale.Crop,
|
||||
1f
|
||||
)
|
||||
Spacer(Modifier.width(RcDp(8f)))
|
||||
Image(
|
||||
bitmap,
|
||||
Modifier.weight(1f).height(RcDp(90f)),
|
||||
"right",
|
||||
RcContentScale.FillBounds,
|
||||
1f
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
|
||||
card(
|
||||
title = "返回",
|
||||
body = "图片验证完成后回到首页。",
|
||||
hint = "点击返回首页 →",
|
||||
target = homeTarget
|
||||
)
|
||||
}
|
||||
|
||||
private fun RcScope.card(
|
||||
title: String,
|
||||
body: String,
|
||||
@@ -227,3 +444,32 @@ private const val TITLE_COLOR = 0xFF0D47A1.toInt()
|
||||
private const val BODY_COLOR = 0xFF212121.toInt()
|
||||
private const val ACCENT_COLOR = 0xFF1565C0.toInt()
|
||||
private const val CARD_BG = 0xFFE3F2FD.toInt()
|
||||
|
||||
// Spike 页面用的对比色,验证通过后可一并删除。
|
||||
private const val WHITE = 0xFFFFFFFF.toInt()
|
||||
private const val SPIKE_RED = 0xFFE53935.toInt()
|
||||
private const val SPIKE_GREEN = 0xFF43A047.toInt()
|
||||
private const val SPIKE_BLUE = 0xFF1E88E5.toInt()
|
||||
private const val SPIKE_PURPLE = 0xFF8E24AA.toInt()
|
||||
private const val SPIKE_ORANGE = 0xFFFB8C00.toInt()
|
||||
|
||||
/** Spike 图片页使用的测试图(部署在服务端静态资源下)。 */
|
||||
private const val SPIKE_IMAGE_URL = "https://ttstd.com/api/remote-ui/assets/spike-image.png"
|
||||
|
||||
/** 测试图真实像素尺寸,用于在文档里声明图片的最大解码尺寸。 */
|
||||
private val spikeImageSize: Pair<Int, Int> by lazy {
|
||||
val stream = checkNotNull(
|
||||
Thread.currentThread().contextClassLoader?.getResourceAsStream("static/api/spike-image.png")
|
||||
) { "缺少 spike-image.png 测试图" }
|
||||
stream.use { ImageIO.read(it).let { image -> image.width to image.height } }
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面引用的图片资源清单。
|
||||
*
|
||||
* 播放端在 setDocument 前按该清单预取图片,避免播放器在主线程同步下载。
|
||||
*/
|
||||
fun buildRemoteDocumentAssets(page: String): List<String> = when (page) {
|
||||
"spike-image" -> listOf(SPIKE_IMAGE_URL)
|
||||
else -> emptyList()
|
||||
}
|
||||
@@ -4,3 +4,10 @@ server:
|
||||
spring:
|
||||
application:
|
||||
name: ComposeRemoteServer
|
||||
web:
|
||||
resources:
|
||||
cache:
|
||||
cachecontrol:
|
||||
# 静态图片允许客户端长期缓存,避免图片多时反复回源。
|
||||
max-age: 30d
|
||||
cache-public: true
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Reference in new issue
Block a user