2

I am writing a custom component in HarmonyOS using Java SDK.

I am trying to convert vp to Px.

In Android we can convert dp to px using below code.

TypedValue.applyDimension(
                        int unit,
                        float value,
                        android.util.DisplayMetrics metrics)

I tried the following code for HarmonyOS but not working.

TypedAttribute.computeTranslateRatio(DeviceCapability devCap)
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
C2C
  • 85
  • 6

1 Answers1

1

Try method AttrHelper.vp2px. doc: https://developer.harmonyos.com/cn/docs/documentation/doc-references/attrhelper-0000001054518726

android_su
  • 1,637
  • 4
  • 21
  • 30
  • I found one more solution, we can do also like this: `public static int dp2px(float dp) { return (int) (dp * TypedAttribute.computeTranslateRatio(new DeviceCapability())); }` – C2C Jul 28 '21 at 09:03