合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.2 KiB

3 months ago
  1. <template>
  2. <view></view>
  3. </template>
  4. <script lang="uts">
  5. import SVG from 'com.caverock.androidsvg.SVG';
  6. import ImageView from 'android.widget.ImageView';
  7. import PictureDrawable from 'android.graphics.drawable.PictureDrawable';
  8. import { ButtonClickListener, createColoredBitmap, setSvgImage } from './utils'
  9. //原生提供以下属性或方法的实现
  10. export default {
  11. /**
  12. * 组件名称也就是开发者使用的标签
  13. */
  14. name: "l-svg-x",
  15. /**
  16. * 组件涉及的事件声明只有声明过的事件才能被正常发送
  17. */
  18. emits: ['load', 'error', 'click'],
  19. /**
  20. * 属性声明组件的使用者会传递这些属性值到组件
  21. */
  22. props: {
  23. "src": {
  24. type: String,
  25. default: ""
  26. },
  27. "color": {
  28. type: String,
  29. default: ""
  30. },
  31. },
  32. /**
  33. * 组件内部变量声明
  34. */
  35. data() {
  36. return {
  37. svg: null as SVG | null,
  38. timer: -1
  39. }
  40. },
  41. /**
  42. * 属性变化监听器实现
  43. */
  44. watch: {
  45. "src": {
  46. handler(_ : string) {
  47. if (this.src == '') return
  48. setTimeout(() => {
  49. this.update(true)
  50. }, 0)
  51. },
  52. immediate: true
  53. },
  54. "color": {
  55. handler(_ : string) {
  56. if (this.color == '') return
  57. setTimeout(() => {
  58. this.update(false)
  59. }, 0)
  60. },
  61. immediate: true
  62. },
  63. },
  64. /**
  65. * 规则如果没有配置expose则methods中的方法均对外暴露如果配置了expose则以expose的配置为准向外暴露
  66. * ['publicMethod'] 含义为只有 `publicMethod` 在实例上可用
  67. */
  68. expose: ['setCSS'],
  69. methods: {
  70. setSvg() {
  71. clearTimeout(this.timer)
  72. this.timer = setTimeout(()=> {
  73. const drawable = new PictureDrawable(this.svg!.renderToPicture())
  74. if ([null, ''].includes(this.color)) {
  75. this.$el?.setImageDrawable(drawable)
  76. } else {
  77. const newBM = createColoredBitmap(drawable, this.color)
  78. if(newBM == null) return
  79. this.$el?.setImageBitmap(newBM)
  80. }
  81. },20)
  82. },
  83. update(init : boolean) {
  84. if(this.svg == null) {
  85. setSvgImage(this.src, (svg) => {
  86. if(svg != null) {
  87. this.svg = svg
  88. this.setSvg()
  89. if(init) {
  90. //svg.getDocumentWidth()
  91. const detail = new ImageLoadEventDetail( svg.getDocumentViewBox().width(), svg.getDocumentViewBox().height())
  92. const loadEvent = new UniImageLoadEvent('load', detail)
  93. this.$emit('load', loadEvent)
  94. }
  95. return
  96. }
  97. this.$emit('error')
  98. })
  99. } else {
  100. // this.setSvg()
  101. }
  102. },
  103. setCSS(css : string) {
  104. // this.$el?.setCSS(css)
  105. }
  106. },
  107. /**
  108. * [可选实现] 组件被创建组件第一个生命周期
  109. * 在内存中被占用的时候被调用开发者可以在这里执行一些需要提前执行的初始化逻辑
  110. */
  111. created() {},
  112. /**
  113. * [可选实现] 对应平台的view载体即将被创建对应前端beforeMount
  114. */
  115. NVBeforeLoad() {},
  116. /**
  117. * [必须实现] 创建原生View必须定义返回值类型
  118. * 开发者需要重点实现这个函数声明原生组件被创建出来的过程以及最终生成的原生组件类型
  119. * Android需要明确知道View类型需特殊校验
  120. */
  121. NVLoad() : ImageView {
  122. let ImageView = new ImageView(this.$androidContext)
  123. ImageView.setOnClickListener(new ButtonClickListener(this));
  124. return ImageView
  125. },
  126. /**
  127. * [可选实现] 原生View已创建
  128. */
  129. NVLoaded() {},
  130. /**
  131. * [可选实现] 原生View布局完成
  132. */
  133. NVLayouted() {},
  134. /**
  135. * [可选实现] 原生View将释放
  136. */
  137. NVBeforeUnload() {},
  138. /**
  139. * [可选实现] 原生View已释放这里可以做释放View之后的操作
  140. */
  141. NVUnloaded() {},
  142. /**
  143. * [可选实现] 组件销毁
  144. */
  145. unmounted() {},
  146. /**
  147. * [可选实现] 自定组件布局尺寸用于告诉排版系统组件自身需要的宽高
  148. * 一般情况下组件的宽高应该是由终端系统的排版引擎决定组件开发者不需要实现此函数
  149. * 但是部分场景下组件开发者需要自己维护宽高则需要开发者重写此函数
  150. */
  151. NVMeasure(size : UTSSize) : UTSSize {
  152. // size.width = 512.0.toFloat();
  153. // size.height = 512.0.toFloat();
  154. return size;
  155. }
  156. }
  157. </script>
  158. <style>
  159. </style>