Why do I need to wrap an ImageView into a FrameLayout?

前端 未结 6 2127
醉话见心
醉话见心 2020-12-14 01:00

Here is a simple layout:

      

         


        
6条回答
  •  抹茶落季
    2020-12-14 01:51

    This applies to almost all the view but answer is written considering specifically ImageView

    Guideline to set Height and Width

    Choose one of this three options only.

    1. Set both height and width to "WRAP_CONTENT"
    2. Set both height and width to "FILL_PARENT"
    3. Set both height and width to "FIXED SIZE"

      *Avoid using mixture of them say Width=WRAP_CONTENT and Height=FILL_PARENT

    Guideline to set SCALETYPE for ImageView

    What does ScaleType do?

    It scale the image you set to the ImageView, It doesn't scale ImageView so when you set it to fitStart it will scale your image and show on the right but your imageView will remain same in terms of height and width as what you have specified.

    As if you are using WRAP_CONTENT for height and width you don't need to specify ScaleType cause your ImageView is automatically going to be as big as your image.

    As if you are using FILL_PARENT for height and width you will have option to show the image in Center,Start,End or in Full view.. so based on that you can choose your ScaleType.

    As if you are using FIXED_SIZE fpr height and width you should opt for scaletype=FITXY.


    Guideline to set Image into ImageView

    As if you are setting image statically in XML then you have one image on your hand so you can create image of the size you wish as per your layout design and just move on with WRAP_CONTENT.

    As if you are setting image at runtime after downloading it from somewhere, that time download the image and create bitmap of the size you prefer and then set it to ImageView


    Specific to your case

    You can see that your image is quite stretched and the text is not readable that's because you have downloaded and set image as it is but in smaller height and width.. rather you should have done like,

    Set ImageView height and width to WRAP_CONTENT in XML file

                        
    

    Download image in your required size say 50X100, for example see this code

    class LoadPics extends AsyncTask {
        String urlStr;
        ImageView iv;
        int width, height;
    
        public LoadPics(ImageView iv, String url, int width, int height) {
            this.iv = iv;
            this.urlStr = url;
            this.width = width;
            this.height = height;
        }
    
        @Override
        protected Bitmap doInBackground(Void... params) {
            try {
                    InputStream in = null;
                    try {
                        URL url = new URL(urlStr);
                        URLConnection urlConn = url.openConnection();
                        HttpURLConnection httpConn = (HttpURLConnection) urlConn;
                        httpConn.connect();
                        in = httpConn.getInputStream();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inJustDecodeBounds = true;
    
                    int scale = 2;
                    if (o.outHeight > width || o.outWidth > height) {
                        scale = 2 ^ (int) Math.ceil(Math.log(width
                                / (double) Math.max(o.outHeight, o.outWidth))
                                / Math.log(0.5));
                    }
    
                    if (scale <= 1 && o.outHeight > 150) {
                        scale = 5;
                    }
    
                    BitmapFactory.Options o2 = new BitmapFactory.Options();
                    o2.inSampleSize = scale;
                    Bitmap b1 = BitmapFactory.decodeStream(in, null, o2);
                    b1 = Bitmap.createScaledBitmap(b1, width, height, true);
                    loader.addToCache(urlStr, b1);
                    publishProgress(b1);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onProgressUpdate(Bitmap... values) {
            super.onProgressUpdate(values);
            iv.setImageBitmap(values[0]);
        }
    }
    

    And call this method with your arguments,

    new LoadPics(ivUser,imgURL,100,50).execute();
    

提交回复
热议问题