An "engineer" bug with 3 years of Android development experience

Source: Internet
Author: User

A question about the ImageView setting ScaleType.

Just 9 o'clock in the evening, one of my outsourcing partners to send a project code I, told me to see a "bug", said the toss for a long time, the picture selector after the selection of pictures, it is necessary to display pictures to the interface, we can imagine the publication of pictures, because our camera image is certainly the length and width are inconsistent, In order to unify the format, it is generally to set the ImageView to be displayed as ScaleType = Centercrop or center.

The problem is: He has set the above attributes, hearted invalid! ImageView set to ScaleType = Centercrop or center, no effect on the picture.

First, the case diagram:

Ideal effects and problem effects (left-to-right):

    

Common xml:

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= ". Mainactivity ">Ten  One<LinearLayout AAndroid:id= "@+id/images_container" -android:paddingleft= "10DP" -android:orientation= "Horizontal" theAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" > -          -  +  -</LinearLayout> +  A  at</RelativeLayout>
View Code

Let's look at the code for the problem :

XML for ImageView:

1 <?xml version= "1.0" encoding= "Utf-8"?>2 <ImageView3     xmlns: Android= "Http://schemas.android.com/apk/res/android"4     android:id= "@+id/image_one"5     android:scaletype= "Centercrop"6     android:layout_width= "30DP"7     android: layout_height= "30DP"/>
View Code

  He set the dynamic AddView () method to add user-selected pictures, Java code, in order to avoid lengthy, I have done simplification, the case effect is the same:

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.test);5         FinalImageView image = (ImageView) layoutinflater.from ( This). Inflate (R.layout.send_post_image,NULL,false);6LinearLayout Images_container =(LinearLayout) Findviewbyid (r.id.images_container);7 Image.setimageresource (r.drawable.beni);8 Images_container.addview (image);9}
View Code

such code, seemingly no problem, through Layoutinflater.from (this). Inflate (...) to instantiate a view, and most crucially, this view is the XML above the ImageView, which explicitly sets the width and height to be 30DP, the display mode is Centercrop, and finally added to a linearlayout by AddView. However, this code shows the effect is right, non-ideal effect!! Haha, it feels like a sudden epiphany.

  you may have such an impression that most of our projects or exercises in the ImageVIew use are direct Findviewbyid, the same imageVIew settings, but no problem, yes, really no problem, You can replace the above code with this test:

Test.xml Change to:

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= ". Mainactivity ">Ten  One<LinearLayout AAndroid:id= "@+id/images_container" -android:paddingleft= "10DP" -android:orientation= "Horizontal" theAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" > -<ImageView -Android:id= "@+id/image" +Android:scaletype= "Centercrop" -Android:layout_width= "30DP" +android:layout_height= "30DP"/> A  at  -</LinearLayout> -  -  -</RelativeLayout>
View Code

  Java in exchange for

1 @Override 2     protected void onCreate (Bundle savedinstancestate) {3         Super . OnCreate (savedinstancestate); 4         Setcontentview (r.layout.test); 5         Final ImageView image = (ImageView) Findviewbyid (r.id.image); 6         Image.setimageresource (r.drawable.beni); 7     }
View Code

  The effect that this shows is what we expect.

Why did the AddView () approach fail ?

The cause of the problem is this : Any method that uses AddView (...), regardless of the width and height of the XML you instantiate for the View itself, is not effective, see clearly, is the width height failure, above the SC Aletype is valid, the problem in Java code call AddView time and did not pass in the Layoutparam layout parameters, OK, let's stick to the source, witness the truth only one.

1 /**2 * <p>adds a child view. If No layout parameters is already set on the3 * Default Parameters for this viewgroup is set on the child.</p>4      * 5 * <p><strong>Note:</strong> do not invoke the method from6      * {@link#draw (Android.graphics.Canvas)}, {@link#onDraw (Android.graphics.Canvas)},7      * {@link#dispatchDraw (Android.graphics.Canvas)} or any related method.</p>8      *9      * @paramChild the Child view to addTen      * One      * @see#generateDefaultLayoutParams () A      */ -      Public voidAddView (View child) { -AddView (Child,-1); the     } -  -  Public voidAddView (View Child,intindex) { -         if(Child = =NULL) { +             Throw NewIllegalArgumentException ("Cannot add a null child view to a ViewGroup"); -         } +Layoutparams params =child.getlayoutparams (); A         if(Params = =NULL) { atparams =generatedefaultlayoutparams (); -             if(Params = =NULL) { -                 Throw NewIllegalArgumentException ("Generatedefaultlayoutparams () cannot return null"); -             } -         } - AddView (Child, index, params); in     } -  to protectedlayoutparams Generatedefaultlayoutparams () { +         return Newlayoutparams (layoutparams.wrap_content, layoutparams.wrap_content); -}
View Code

  because you do not have to pass in Layoutparam and get is again null, why get is NULL, please add this sentence in the case code above , LOG.D ("Zzzzz", "" "+image.getlayoutparams ()), you will see the output null, and eventually call the source:

Protected Layoutparams Generatedefaultlayoutparams () {
return new Layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content);
}
Direct use of the wrap_parent, so ah, caused the failure. So what am I going to do with AddView ? You can add a wide height limit.
1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.test);5         FinalImageView image = (ImageView) layoutinflater.from ( This). Inflate (R.layout.send_post_image,NULL,false);6Linearlayout.layoutparams LP =NewLinearlayout.layoutparams (50,50);/**here*/7LinearLayout Images_container =(LinearLayout) Findviewbyid (r.id.images_container);8IMAGE.SETLAYOUTPARAMS (LP);/**Add*/9 Image.setimageresource (r.drawable.beni);TenLOG.D ("Zzzzz", "" "+image.getlayoutparams ()); One Images_container.addview (image); A}
View Code

All right, it's almost 1 o'clock.

Summarized below. Cause such things happen, I think there is still a lack of self-practical coding problems, now the framework is more flooding, and think of the old saying: We do not produce code, we are GitHub's porter ...

An "engineer" bug with 3 years of Android development experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.