D
D
Dmitry Serkov2016-10-02 23:24:40
Android
Dmitry Serkov, 2016-10-02 23:24:40

How to get ImageView size?

How can I get the size of an ImageView on application startup?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Gapchenko, 2016-10-03
@Hutaab

The ViewTreeObserver gives you the ability to hook into certain stages in the "life cycle" of a View, including the stage when the View's dimensions have been determined:

imageview.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //
        // Тут вы пишите ваш код, который завязан на знание размера View
        //

        // А тут отсоединяете OnGlobalLayoutListener
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            imageview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            imageview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    } 
});

K
Konstantin Dovnar, 2016-10-03
@SolidlSnake

The View class has getWidth and getHeight methods. Returns the width and height of your view in pixels.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question