BannerAd.ets 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { advertising } from "@kit.AdsKit";
  2. interface BannerParam {
  3. adWidth: number,
  4. adHeight: number
  5. }
  6. export class BannerAd {
  7. adParam: advertising.AdRequestParams = {
  8. // 广告类型:横幅广告
  9. adType: 8,
  10. // 'testw6vs28auh3'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID
  11. adId: 'testw6vs28auh3',
  12. // 广告位宽
  13. adWidth: 360,
  14. // 广告位高
  15. adHeight: 57,
  16. };
  17. adOptions: advertising.AdOptions = {
  18. // 设置广告内容分级上限
  19. adContentClassification: 'A'
  20. };
  21. adDisplayOptions: advertising.AdDisplayOptions = {
  22. // 是否静音,默认不静音
  23. mute: false,
  24. // 广告轮播的时间间隔,单位ms,取值范围[30000, 120000]
  25. refreshTime: 30000
  26. }
  27. ratio: number = 1;
  28. adWidth: number = -1;
  29. adHeight: number = -1;
  30. constructor(param?: BannerParam) {
  31. if (param && param.adWidth > 0 && param.adHeight > 0) {
  32. this.adParam.adWidth = param.adWidth
  33. this.adParam.adHeight = param.adHeight
  34. }
  35. if (this.adParam?.adWidth && typeof (this.adParam?.adWidth) === 'number' && this.adParam?.adWidth > 0) {
  36. this.adWidth = this.adParam?.adWidth;
  37. }
  38. if (this.adParam?.adHeight && typeof (this.adParam?.adHeight) === 'number' && this.adParam?.adHeight > 0) {
  39. this.adHeight = this.adParam?.adHeight;
  40. }
  41. if (this.adWidth > 0 && this.adHeight > 0) {
  42. this.ratio = this.adWidth / this.adHeight;
  43. }
  44. }
  45. }