Problem Description
I have enough food to say! ⊙﹏⊙ b Khan .
Today, a picture of the demo, want to dynamically change the size of a picture and margin values. However, when you get the width and height of the ImageView in activity, it is always 0. Hey, I don't understand the TM, how can it be 0?
<span style= "White-space:pre" ></span>imageview1 = (ImageView) Findviewbyid (R.ID.ID_IMG1); int width = Imageview1.getwidth (); int height = imageview1.getheight ();
Analysis Reason
At first, I suspect that the code has a problem, may be wrong, but repeated examination and experiment several times, but also TM is 0. The Egg hurts! Later on the Internet a search, discovered the new continent.
In the OnCreate method, the control is not actually drawn, which means that the control is only started when the OnCreate method is finished, so the value returned by the width and height in the OnCreate method is 0. But I also want to Onresume method is not possible? Later found or not! I've customized the ImageView, and then I print the log logs, and I see that this is the case, after the OnCreate and Onresume methods have been executed, the control's measurement (onmeasure) and the Draw (OnDraw) methods are executed.
Myimageview.java---Custom ImageView class:
public class Myimageview extends ImageView {public Myimageview (context context) {super (context);} Public Myimageview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); LOG.I ("ImageView", "onmeasure ..."); @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); LOG.I ("ImageView", "OnDraw ...");}}
Activity class:
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.LAYOUT.ACTIVITY_DEMO3); LOG.I ("ImageView", "onCreate ..."); @Overrideprotected void Onresume () {super.onresume (); LOG.I ("ImageView", "Onresume ...");
The log logs are printed as follows:
Solutions
Since the OnCreate method does not work, there should be other methods! So from the Internet to find the following several methods!
The first method: Use Bitmapfactory.
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.songjiang); LOG.I ("ImageView", "width:" + bitmap.getwidth ()); LOG.I ("ImageView", "Height:" + bitmap.getheight ());
The second method: Use the Measurespec.makemeasurespec method.
int w = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified); int h = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); Img.measure (W, h); LOG.I ("Imageview2", "width:" + img.getmeasuredwidth ()); LOG.I ("Imageview2", "Height:" + img.getmeasuredheight ());
The third method: Use the Viewtreeobserver.addonpredrawlistener method.
Viewtreeobserver vto = Img.getviewtreeobserver (); Vto.addonpredrawlistener (new Onpredrawlistener () {@Overridepublic Boolean Onpredraw () {int height = img.getmeasuredheight (); int width = Img.getmeasuredwidth (); LOG.I ("Imageview3", "width:" + width); LOG.I ("imageview3", "Height:" + height); return true;});
Fourth method: Use the Viewtreeobserver.addongloballayoutlistener method.
Viewtreeobserver vto = Img.getviewtreeobserver (); Vto.addongloballayoutlistener (new Ongloballayoutlistener () {@ overridepublic void Ongloballayout () {img.getviewtreeobserver (). Removeglobalonlayoutlistener (this); LOG.I ("Imageview4", "width:" + img.getwidth ()); LOG.I ("Imageview4", "Height:" + img.getheight ());});
Note: Method Three is called 3 times.
It's easy to dynamically change the ImageView in your code!
Displaymetrics metrics = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics); int desitydpi = metrics.densitydpi; Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.hi); int width = bitmap.getwidth () * 160/desityd Pi;int height = bitmap.getheight () * 160/DESITYDPI; Layoutparams params = Imageview1.getlayoutparams ();p arams.width = Width;params.height = height; Imageview1.setlayoutparams (params);
Download
Demo code here download here! (then upload!) )
Android from zero single row to get ImageView of the width of 0?