getWidth and getHeight are returning a zero

后端 未结 6 1556
一整个雨季
一整个雨季 2020-12-03 07:12

I am trying to make a simple drawing program for the android.

I have a custom View class to handle the drawing. When I call its getWidth an

6条回答
  •  盖世英雄少女心
    2020-12-03 08:11

    I'm not certain, but it may have something to do with where the code is in the lifecycle of your activity. If you're calling getWidth() and getHeight() before the View is displayed on screen, you'll get a value of 0. I've had that happen to me, too.

    I'm not sure if there's a way around this. I had to rely on getting the hardware screen's width and height, instead of the view's width and height. You might end up having to approximate the width and height of your view and hard coding it.

提交回复
热议问题