A 3-year android Development Experience & quot; engineer & quot; bug & quot;

Source: Internet
Author: User

A "bug" with three years of android development experience as "engineer"

A question about how to set scaleType in imageView.

Just at a.m., one of my outsourcing partners sent me a project code and asked me to look at such a "bug, after selecting an image, the image selector will display the image on the interface. You can imagine posting an image, because the image length and width of our camera must be different. to unify the format, generally, the imageView to be displayed is set to scaleType = centerCrop or center.

The problem is: after setting the above attribute, it is not valid! Set imageView to scaleType = centerCrop or center, which has no effect on images.

First, the example is shown below:

Ideal results and question results (left-> right ):

    

Common xml:

 1 <relativelayout 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity">10 11     <linearlayout 12="" 13="" 14="" 15="" 16="" android:id="@+id/images_container" android:paddingleft="10dp" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">17         18 19 20     </linearlayout>21 22 23 </relativelayout>

We can see the code of the problem:

ImageView xml:

1 <!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->2 <imageview 3="" 4="" 5="" 6="" 7="" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/image_one" android:scaletype="centerCrop" android:layout_width="30dp" android:layout_height="30dp"></imageview>

He sets the dynamic addView () method to add the image selected by the user and java code. To avoid a long article, I have simplified it and the results are the same:

1 @Override2     protected void onCreate(Bundle savedInstanceState) {3         super.onCreate(savedInstanceState);4         setContentView(R.layout.test);5         final ImageView image = (ImageView) LayoutInflater.from(this).inflate(R.layout.send_post_image, null, false);6         LinearLayout images_container = (LinearLayout) findViewById(R.id.images_container);7         image.setImageResource(R.drawable.beni);8         images_container.addView(image);9     }

This Code seems to be okay, through LayoutInflater. from (this ). inflate (...) this View is the xml of the imageView above. The width and height are set to 30dp, and the display mode is centerCrop, add it to a linearLayout using addView. However, the result of this Code is the right image, which is not an ideal result !! Haha.

You may have the impression that we use findViewById directly for most projects or imageVIew in the exercises. The same imageView settings are the same, but there is no problem, you can replace the above Code with this:

Replace test. xml:

 1 <relativelayout 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity">10 11     <linearlayout 12="" 13="" 14="" 15="" 16="" android:id="@+id/images_container" android:paddingleft="10dp" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">17         <imageview 18="" 19="" 20="" 21="" android:id="@+id/image" android:scaletype="centerCrop" android:layout_width="30dp" android:layout_height="30dp">22 23 24     </imageview></linearlayout>25 26 27 </relativelayout>
View Code

For java

1 @Override2     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     }

This shows what we expect.

Why did the addView () method fail?

The cause of the problem is: Any use of addView (...) no matter what width and height of the xml of the View you instantiate are set, it is ineffective. Please be clear that the width height is invalid, the above scaleType is valid. The LayoutParam layout parameter is not passed in when addView is called in the java code. Well, we will stick to the source code and witness only one truth.

 1 /** 2      * 

Adds a child view. If no layout parameters are already set on the child, the 3 * default parameters for this ViewGroup are set on the child.

4*5 *

Note:Do not invoke this method from 6 * {@ link # draw (android. graphics. canvas) },{ @ link # onDraw (android. graphics. canvas)}, 7 * {@ link # dispatchDraw (android. graphics. canvas)} or any related method.

8*9 * @ param child the child view to add 10*11 * @ see # generateDefaultLayoutParams () 12 */13 public void addView (View child) {14 addView (child, -1); 15} 16 17 public void addView (View child, int index) {18 if (child = null) {19 throw new IllegalArgumentException ("Cannot add a null child view to a ViewGroup"); 20} 21 LayoutParams params = child. getLayoutParams (); 22 if (params = null) {23 params = generatedefadefalayoutparams (); 24 if (params = null) {25 throw new IllegalArgumentException ("generatedefalalayoutparams () cannot return null "); 26} 27} 28 addView (child, index, params); 29} 30 31 protected LayoutParams generateDefaultLayoutParams () {32 return new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); 33} View Code

Because LayoutParam is not passed in by myself, and get is null, why is get null? Please add this log, Log in the example code above. d ("zzzzz", "" + image. getLayoutParams ();, the output is null, and the following code is called:

Protected LayoutParams generatedefadefalayoutparams () {return new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT);} directly uses Wrap_parent, which leads to failure. What should I do if I want to use addView? Add a limit on width and height.
1 @ Override 2 protected void onCreate (Bundle savedInstanceState) {3 super. onCreate (savedInstanceState); 4 setContentView (R. layout. test); 5 final ImageView image = (ImageView) LayoutInflater. from (this ). inflate (R. layout. send_post_image, null, false); 6 LinearLayout. layoutParams lp = new LinearLayout. layoutParams (50, 50);/** here */7 LinearLayout images_container = (LinearLayout) findViewById (R. id. images_container); 8 image. setLayoutParams (lp);/** add */9 image. setImageResource (R. drawable. beni); 10 Log. d ("zzzzz", "" + image. getLayoutParams (); 11 images_container.addView (image); 12}

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

Summary. As a result, I think it is still a lack of self-practice coding. Now the framework is rampant and I think of the old saying: We don't produce code, we are all github porters...

Related Article

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.