Jak uzyskać szerokość i wysokość Androida.widget.ImageView?

╔══════════════════════════════════════════════╗   ^
║ ImageView    ╔══════════════╗                ║   |
║              ║              ║                ║   |
║              ║ Actual image ║                ║   |
║              ║              ║                ║   |60px height of ImageView
║              ║              ║                ║   |
║              ║              ║                ║   |
║              ╚══════════════╝                ║   |
╚══════════════════════════════════════════════╝   
<------------------------------------------------>
                   90px width of ImageView

Mam widok obrazu z domyślną wysokością i szerokością, obrazy są przechowywane w db i chcę skalować obraz zgodnie z szerokością wysokości Imageview. Ponieważ nie chcę, aby podawał wartości domyślne, ponieważ kiedy kiedykolwiek zmieniam jego wysokość i szerokość, również muszę to zmienić w kodzie.

Próbuję uzyskać wysokość i szerokość ImageView, ale 0 jest zwracane do mnie w obu przypadkach.

int height = ((ImageView) v.findViewById(R.id.img_ItemView)).getHeight();

To zwraca mi 0, nawet jeśli ma domyślną wysokość i szerokość

Author: einverne, 2011-01-13

10 answers

Moja odpowiedź na to pytanie może Ci pomóc:

int finalHeight, finalWidth;
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
final TextView tv = (TextView)findViewById(R.id.size_label);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = iv.getMeasuredHeight();
        finalWidth = iv.getMeasuredWidth();
        tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
        return true;
    }
});

Możesz następnie dodać swoją pracę ze skalowaniem obrazu za pomocą metody onPreDraw ().

 207
Author: kcoppock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:03:05

Właśnie ustawiłem tę właściwość i teraz Android OS zajmuje się wszystkim.

Android: adjustViewBounds= "true"

Użyj tego w swoim układzie.xml gdzie umieściłeś swój ImageView: d

 25
Author: AZ_,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-09 02:37:27

Mogę uzyskać szerokość i wysokość obrazu przez jego rysowanie;

int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();
 17
Author: Rashid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-08 07:25:03

Powodem, dla którego wymiary ImageView są równe 0, jest to, że podczas ich odpytywania widok nadal nie wykonał kroków układu i miary. Powiedziałeś tylko, jak będzie się "zachowywał" w układzie, ale nadal nie obliczono, gdzie umieścić każdy widok.

Jak wybrać rozmiar, który ma być wyświetlany w widoku obrazu? Nie możesz po prostu użyć jednej z opcji skalowania zaimplementowanych natywnie?

 13
Author: Pedro Loureiro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-18 00:19:17

Post do wątku UI działa dla mnie.

final ImageView iv = (ImageView)findViewById(R.id.scaled_image);

iv.post(new Runnable() {
            @Override
            public void run() {
                int width = iv.getMeasuredWidth();
                int height = iv.getMeasuredHeight();

            }
});
 9
Author: element6,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-22 05:06:46

Myślę, że możesz pozwolić Android OS zająć się tym za Ciebie. Ustaw typ skali w widoku ImageView na fitXY, A wyświetlany obraz będzie miał rozmiar odpowiadający bieżącemu rozmiarowi widoku.

<ImageView 
    android:layout_width="90px" 
    android:layout_height="60px"
    android:scaleType="fitXY" />
 4
Author: Ian Leslie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-13 13:43:17

Twój plik xml:

 <ImageView android:id="@+id/imageView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@drawable/image"
               android:scaleType="fitXY"
               android:adjustViewBounds="true"/>

Twój plik java:

ImageView imageView = (ImageView)findViewById(R.id.imageView);
     int width = imageView.getDrawable().getIntrinsicWidth();
     int   height = imageView.getDrawable().getIntrinsicHeight();
 4
Author: omor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-13 09:20:00

Najprostszym sposobem jest uzyskanie szerokości i wysokości ImageView w metodzie onwindowfocuschanged aktywności

 @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    height = mImageView.getHeight();
    width = mImageView.getWidth();

}
 2
Author: Aqib,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-26 11:11:57

Jeśli utworzyłeś wiele obrazów dynamicznie , Spróbuj tego:

// initialize your images array

private ImageView myImages[] = new ImageView[your_array_length];

// Utwórz programowo i dodaj do widoku nadrzędnego

 for (int i = 0; i < your_array_length; i++) {
                myImages[i] = new ImageView(this);
                myImages[i].setId(i + 1);
                myImages[i].setBackgroundResource(your_array[i]);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        frontWidth[i], frontHeight[i]);
                ((MarginLayoutParams) params).setMargins(frontX_axis[i],
                        frontY_axis[i], 0, 0);
                myImages[i].setAdjustViewBounds(true);
                myImages[i].setLayoutParams(params);

                if (getIntent() != null && i != your_array,length) {
                    final int j = i;
                    myImages[j].getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        public boolean onPreDraw() {
                            myImages[j].getViewTreeObserver().removeOnPreDrawListener(this);
                    finalHeight = myImages[j].getMeasuredHeight();
                        finalWidth = myImages[j].getMeasuredWidth();
                    your_textview.setText("Height: " + finalHeight + " Width: " + finalWidth);
                            return true;
                        }
                    });
                }
                your_parent_layout.addView(myImages[i], params);
            }

/ / to jest to. Szczęśliwego Kodowania.

 1
Author: Hiren Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-06 06:14:07

Mój przyjaciel przez to u nie dostają wysokości obrazu przechowywanego w db.ale dostajesz wysokość widoku.aby uzyskać wysokość obrazu, należy utworzyć bitmapę z db, S obrazu.i wtedy u może pobrać wysokość i szerokość imageview

 -2
Author: chikka.anddev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-13 13:24:55