yangchenggong1_wd

fix |> 20568 【依赖华为】我的评论列表中,上方展示的昵称,换行比安卓早,并且等级的位置展示不正确

  1 +import { GradeSpan } from './span/GradeSpan';
  2 +import { image } from '@kit.ImageKit';
  3 +import { LengthMetrics } from '@kit.ArkUI';
  4 +import { StringUtils } from 'wdKit';
  5 +
  6 +@Component
  7 +export struct UserGradeTextSpan{
  8 + @Watch('contentChange') @Prop nameContent:string = ""
  9 + @Watch('contentChange') @Prop gradeContent:string = ""
  10 + @Watch('contentChange') @Prop gradeWidth:number = 50
  11 +
  12 + taskId:number = -1
  13 +
  14 + customSpan3: GradeSpan | undefined = undefined;
  15 + style1: MutableStyledString | undefined = undefined;
  16 +
  17 + imagePixelMap: image.PixelMap | undefined = undefined;
  18 +
  19 + textController: TextController = new TextController();
  20 +
  21 + private async getPixmapFromMedia(resource: Resource) {
  22 + let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({
  23 + bundleName: resource.bundleName,
  24 + moduleName: resource.moduleName,
  25 + id: resource.id
  26 + });
  27 + let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength));
  28 + let createPixelMap: image.PixelMap = await imageSource.createPixelMap({
  29 + desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
  30 + desiredSize: { width: vp2px(this.gradeWidth), height: vp2px(18) }
  31 + });
  32 + await imageSource.release();
  33 + return createPixelMap;
  34 + }
  35 +
  36 + contentChange(){
  37 + if(this.taskId != -1){
  38 + clearTimeout(this.taskId)
  39 + }
  40 + this.taskId = setTimeout(() => {
  41 + this.createSpan()
  42 + }, 100);
  43 +
  44 + }
  45 +
  46 + async createSpan(){
  47 + if(StringUtils.isEmpty(this.nameContent)){
  48 + return
  49 + }
  50 + this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.my_grade_bg'))
  51 + this.customSpan3 = new GradeSpan(this.gradeContent, this.gradeWidth, 18,this.imagePixelMap);
  52 + this.style1 = new MutableStyledString(this.customSpan3);
  53 +
  54 + let name:MutableStyledString = new MutableStyledString(this.nameContent, [
  55 + {
  56 + start: 0,
  57 + length: 100,
  58 + styledKey: StyledStringKey.FONT,
  59 + styledValue: new TextStyle({ fontColor: Color.White,fontWeight: 500,fontSize: LengthMetrics.px(vp2px(20)) })
  60 + }])
  61 +
  62 + name.appendStyledString(new StyledString(this.customSpan3))
  63 + this.textController.setStyledString(name)
  64 + }
  65 +
  66 +
  67 + aboutToAppear() {
  68 + this.createSpan()
  69 + }
  70 +
  71 + build() {
  72 + Text(undefined, { controller: this.textController })
  73 + .copyOption(CopyOptions.InApp)
  74 + .maxLines(2)
  75 + }
  76 +}
  1 +import { drawing } from '@kit.ArkGraphics2D';
  2 +import { image } from '@kit.ImageKit';
  3 +
  4 +export class GradeSpan extends CustomSpan {
  5 + width: number = 160;
  6 + word: string = "";
  7 + height: number = 10;
  8 + imagePixelMap: image.PixelMap | undefined = undefined;
  9 +
  10 +
  11 + constructor(word: string, width: number, height: number,imagePixelMap: image.PixelMap) {
  12 + super();
  13 + this.word = word;
  14 + this.width = width;
  15 + this.height = height;
  16 + this.imagePixelMap = imagePixelMap;
  17 + }
  18 +
  19 + onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics {
  20 + return { width: this.width, height: this.height };
  21 + }
  22 +
  23 + onDraw(context: DrawContext, options: CustomSpanDrawInfo) {
  24 + let canvas = context.canvas;
  25 +
  26 + const brush = new drawing.Brush();
  27 +
  28 + canvas.attachBrush(brush);
  29 + let options2 = new drawing.SamplingOptions(drawing.FilterMode.FILTER_MODE_NEAREST);
  30 + if (this.imagePixelMap) {
  31 + canvas.drawImage(this.imagePixelMap, options.x , options.lineBottom - vp2px(this.height) - 10, options2);
  32 + }
  33 +
  34 + brush.setColor({ alpha: 255, red: 237, green: 40, blue: 0 });
  35 + canvas.attachBrush(brush);
  36 +
  37 + const font = new drawing.Font();
  38 + font.setSize(vp2px(12));
  39 + const textBlob = drawing.TextBlob.makeFromString(this.word, font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
  40 + // canvas.drawTextBlob(textBlob, options.x + 20, options.lineBottom - 15);
  41 + canvas.drawTextBlob(textBlob, options.x + vp2px(9), options.lineBottom - vp2px(7.5));
  42 +
  43 + canvas.detachBrush();
  44 + }
  45 +
  46 + setWord(word: string) {
  47 + this.word = word;
  48 + }
  49 +
  50 +}
@@ -8,6 +8,7 @@ import MinePageDatasModel from '../model/MinePageDatasModel'; @@ -8,6 +8,7 @@ import MinePageDatasModel from '../model/MinePageDatasModel';
8 import { EmptyComponent } from '../components/view/EmptyComponent'; 8 import { EmptyComponent } from '../components/view/EmptyComponent';
9 import { CustomTitleUI } from '../components/reusable/CustomTitleUI'; 9 import { CustomTitleUI } from '../components/reusable/CustomTitleUI';
10 import { TrackingPageBrowse, TrackConstants, TrackingButton, ParamType, Tracking } from 'wdTracking/Index'; 10 import { TrackingPageBrowse, TrackConstants, TrackingButton, ParamType, Tracking } from 'wdTracking/Index';
  11 +import { UserGradeTextSpan } from '../components/view/UserGradeTextSpan';
11 12
12 const TAG = "MineHomePage" 13 const TAG = "MineHomePage"
13 14
@@ -121,37 +122,17 @@ struct MineHomePage { @@ -121,37 +122,17 @@ struct MineHomePage {
121 122
122 Column() { 123 Column() {
123 Row() { 124 Row() {
124 - Text(`${this.userName}`)  
125 - .fontColor($r('app.color.white'))  
126 - .maxLines(2)  
127 - .textOverflow({ overflow: TextOverflow.Ellipsis })  
128 - .fontSize(20)  
129 - .lineHeight(26)  
130 - .fontWeight(500)  
131 - .constraintSize({maxWidth:180})  
132 - .onClick(()=>{  
133 - let params: editModelParams = {  
134 - editContent: this.userName  
135 - }  
136 - WDRouterRule.jumpWithPage(WDRouterPage.editUserNikeNamePage,params)  
137 - })  
138 -  
139 - if(this.levelId>0){  
140 - Text(`等级${this.levelId}`)  
141 - .fontColor($r('app.color.color_ED2800'))  
142 - .fontSize(12)  
143 - .lineHeight(18)  
144 - .fontWeight(500)  
145 - .margin({ left: 6 })  
146 - .backgroundImage($r("app.media.my_grade_bg"))  
147 - .backgroundImageSize(ImageSize.Cover)  
148 - .padding({left: 6,right: 6})  
149 - .height(18)  
150 - .borderRadius({topRight:2,bottomRight:2})  
151 - }  
152 125
153 - Blank()  
154 - } 126 + UserGradeTextSpan({nameContent:`${this.userName}`,gradeContent:`等级${this.levelId}`,gradeWidth:this.levelId >9 ? 50 : 45})
  127 +
  128 + }.constraintSize({maxWidth:`calc(100% - 16vp - 90vp)`})
  129 + .padding({top:8})
  130 + .onClick(()=>{
  131 + let params: editModelParams = {
  132 + editContent: this.userName
  133 + }
  134 + WDRouterRule.jumpWithPage(WDRouterPage.editUserNikeNamePage,params)
  135 + })
155 136
156 Row() { 137 Row() {
157 Row() { 138 Row() {