Dynamic Height in MotionLayout Using JSON5

22 Views Asked by At

I'm using MotionLayout, and the heights of my components are defined in JSON5. Now, I have a requirement to change the height based on the screen size of different devices. So, I wrote a function to calculate the scaling ratio, but the problem is that my heights are defined in JSON5, and I can't use my function inside JSON5. Is there any way to make my heights change dynamically?

My calculate function:

    fun getToDeviceWidthDpRatio(screenWidth: Int): Float {
       
        if (screenWidth == Constants.DEVICE_BASE_WIDTH) {
            return 1f
        }
        var ratio = screenWidth / Constants.DEVICE_BASE_WIDTH.toFloat()
        val maxRatio = 1.4f
        if (ratio >= maxRatio) {
            ratio = maxRatio
        }
        return ratio
    }

Part of JSON5:

ConstraintSets: {
  start: {
    topBackground: {
      width: 'parent',
      height: 166, //this is which height I want to scale
      start: ['parent', 'start'],
      end: ['parent', 'end'],
      top: ['parent', 'top'],
      custom: {
        gradientStart: '#EA1717',
        gradientEnd: '#FD7777',
        cornerRadius: 16
      }
    }
  },
  end:{
    topBackground: {
      width: 'parent',
      height: 56, //this is which height I want to scale
      start: ['parent', 'start'],
      end: ['parent', 'end'],
      top: ['parent', 'top'],
      custom: {
        gradientStart: '#EA1717',
        gradientEnd: '#EA1717',
        cornerRadius: 0
      }
    }
  }
}
0

There are 0 best solutions below