适配效果优化
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { _decorator, Component, math, Node, Sprite } from "cc";
|
||||
import { _decorator, Component, math, Node, Sprite, UITransform } from "cc";
|
||||
import { Config18x } from "db://assets/Scripts/Main/Config/Config18x";
|
||||
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
|
||||
import { ViewManager } from "db://assets/Scripts/Main/Manager/ViewManager";
|
||||
@@ -20,8 +20,11 @@ export class DetailImageItem extends Component {
|
||||
base: GirlDetailPanel;
|
||||
url: string;
|
||||
|
||||
uitransform: UITransform;
|
||||
|
||||
onLoad() {
|
||||
GButton.BandClick(this.node, this.onClickThis, this);
|
||||
this.uitransform = this.node.getComponent(UITransform);
|
||||
}
|
||||
|
||||
onClickThis() {
|
||||
@@ -36,6 +39,54 @@ export class DetailImageItem extends Component {
|
||||
this.base.setVideEnable(false);
|
||||
}
|
||||
}
|
||||
|
||||
scaleToFillItem() {
|
||||
if (!this.img || !this.img.spriteFrame) return;
|
||||
|
||||
// 延迟一帧确保UI布局完成
|
||||
this.scheduleOnce(() => {
|
||||
this.doScaleToFill();
|
||||
}, 0);
|
||||
}
|
||||
|
||||
doScaleToFill() {
|
||||
if (!this.img || !this.img.spriteFrame) return;
|
||||
|
||||
const itemSize = this.uitransform.contentSize;
|
||||
const imageSize = this.img.spriteFrame.originalSize;
|
||||
|
||||
console.log("DetailImageItem doScaleToFill:", {
|
||||
itemSize,
|
||||
imageSize,
|
||||
url: this.url
|
||||
});
|
||||
|
||||
if (
|
||||
itemSize.width === 0 ||
|
||||
itemSize.height === 0 ||
|
||||
imageSize.width === 0 ||
|
||||
imageSize.height === 0
|
||||
) {
|
||||
console.log("DetailImageItem: Invalid size, skipping scale");
|
||||
return;
|
||||
}
|
||||
|
||||
const scaleX = itemSize.width / imageSize.width;
|
||||
const scaleY = itemSize.height / imageSize.height;
|
||||
const scale = Math.max(scaleX, scaleY);
|
||||
|
||||
console.log("DetailImageItem scale values:", { scaleX, scaleY, finalScale: scale });
|
||||
|
||||
// 设置Sprite的sizeMode为CUSTOM,允许自定义大小
|
||||
this.img.sizeMode = Sprite.SizeMode.CUSTOM;
|
||||
|
||||
// 获取图片节点的UITransform并设置大小
|
||||
const imgTransform = this.img.node.getComponent(UITransform);
|
||||
if (imgTransform) {
|
||||
imgTransform.setContentSize(imageSize.width * scale, imageSize.height * scale);
|
||||
console.log("DetailImageItem: Set image size to", imgTransform.contentSize);
|
||||
}
|
||||
}
|
||||
refresh(path: string, base: GirlDetailPanel) {
|
||||
this.base = base;
|
||||
this.lock.active = false;
|
||||
@@ -54,7 +105,14 @@ export class DetailImageItem extends Component {
|
||||
// this.img.color = new math.Color(255, 255, 255, 255);
|
||||
// }
|
||||
|
||||
ResManager.I.changeBundleSpriteFrame(this.img, this.url, "Chat18x");
|
||||
ResManager.I.changeBundleSpriteFrame(
|
||||
this.img,
|
||||
this.url,
|
||||
"Chat18x",
|
||||
() => {
|
||||
this.scaleToFillItem();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user