Android get View height and width

Android get View height and width

Wait for the View to load to complete

final ImageView iv = (ImageView) findViewById(R.id.iv_test);
		iv.setImageResource(R.drawable.abc);
		
		//Wait for ImageVivew to finish loading
		iv.post(new Runnable(){	

			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				//ImageView width and height
				Log.d("lxy", "iv_W = " + iv.getWidth() + ", iv_H = " + iv.getHeight());

				//Get the real width and height of Image in ImageView,
				int dw = iv.getDrawable().getBounds().width();
				int dh = iv.getDrawable().getBounds().height();
				Log.d("lxy", "drawable_X = " + dw + ", drawable_Y = " + dh);
				
				//Get the transformation matrix of Image in ImageView
				Matrix m = iv.getImageMatrix();
				float[] values = new float[10];
				m.getValues(values);
				
				//Image transformation matrix during the drawing process, from which the scaling coefficients in the x and y directions are obtained
				float sx = values[0];
				float sy = values ​​[4];
				Log.d("lxy", "scale_X = " + sx + ", scale_Y = " + sy);
				
				//Calculate the width and height of the Image actually drawn on the screen
				int cw = (int)(dw * sx);
				int ch = (int) (dh * sy);
				Log.d("lxy", "caculate_W = " + cw + ", caculate_H = " + ch);
			}});

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326672294&siteId=291194637