Scaled images with volley

大兔子大兔子 提交于 2019-12-14 04:27:13

问题


I am trying to stretch images that I load with volley. XML isn't much help, while it can shrink them it doesn't help enlarging them. My exploration of the topic led me to the conclusion that this can be achieved only programmatically.

What is my idea ?

To extend the volley library and overriding one of the methods, resizing the bitmap right after download and before displaying it.

I used this code to resize images that were already on the phone, but this doesn't help me much with random images from the internet.

        Point outSize=new Point();
        float  destWidth=1;
        float  destHeight=1;

        @SuppressWarnings("deprecation")
        @TargetApi(13)
        private Bitmap scaleBitmap(int mFile){
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            if (android.os.Build.VERSION.SDK_INT >= 13){
                display.getSize(outSize);
                destWidth = outSize.x;
                destHeight = outSize.y;
            }else{
                destWidth=display.getWidth();
                destWidth=display.getHeight();
            }

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            Bitmap orig = BitmapFactory.decodeResource(getResources(), mFile);
            float srcWidth = orig.getWidth();
            float srcHeight = orig.getHeight();
            float inSampleSize = 1;
            if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                inSampleSize=(destHeight/2)/srcHeight;
            }else{
            inSampleSize=(destHeight/4)/srcHeight;}
            options = new BitmapFactory.Options();
            Bitmap resized=Bitmap.createScaledBitmap(orig, (int)Math.round(orig.getWidth()*inSampleSize), (int)Math.round(orig.getHeight()*inSampleSize), true);
            destWidth=1;
            destHeight=1;
            return resized;
        }

Basically I want to assign to orig the image that is downloaded, then resize it and then display it. My question is: What class do I extend ? I took a look there but since I am inexperienced I couldn't figure out what exactly to look for. Is it ImageLoader ? And more specifically: should i Override the getBitmap() method and add an edited version of the code for scaling ?

Disclaimer: I am very inexperienced and I would accept other ideas too.


回答1:


I solved it a few days ago. Here is how the regular ImageView handles resizing of images: If you are using Match_parent as width and wrap_content as height(I tested with both as match_parent and the results were the same) it will NOT scale the image IF the Height of your picture is lower than the width (which is quite typical) . Meaning that image view scales based on the smaller side. This meant that there are 2 solutions:

  • Do what I wanted to do originally which involves writing a new class that extends imageview/networkimageview and then programmatically adding the code that i listed to enlarge the picture.
  • Or simply specify the desired Height of the view programmatically and imageview will then simply scale the image to fit the height of the new View. This options invalidates the wrap_content for Width and simply puts in as much as you need.

Here is the code I used:

 Display display = getActivity().getWindowManager().getDefaultDisplay();
 Point outSize=new Point();
 int destWidth;
 if (android.os.Build.VERSION.SDK_INT >= 13){
    display.getSize(outSize);
    destWidth = outSize.x;
 }else{
    destWidth=display.getWidth();
 }
 img.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT,destWidth*5625/10000));

In my case I needed to fit a 640x360 image so I used a ratio of 0,5625. This method will work if you do not know the picture's parameters BUT you have to get the parameters beforehand and put them in place, so I guess it might be more useful to use the first strategy, but since I solved my problem I don't have an example for the first method.




回答2:


I think the issue that you're seeing is caused by the default behavior of how the underlying code for NetworkImageView works.

Have a look at the code for ImageRequest which is used by NetworkImageView . There is a function there called getResizedDimension which says something like "Scales one side of a rectangle to fit aspect ratio" . This may have caused your issue since the image(s) that you're downloading may not have the perfect aspect ratio computed every time for a specific device.

Are you displaying a single image, or images in a grid? If it is just a single image, there might be a work around. I haven't seen your full code, on where you set the image or image url, so I am going to assume the flow here:

1.) Use the regular ImageView.

2.) Use the regular Volley request pointing to the image's URL

3.) When you receive the response (parseNetworkResponse), decode (the array of bytes) it yourself, using the method you mentioned above that works for you. Some tips can also be found here on how to do that: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

4.) After decoding, programmatically set the ImageView's bitmap with your decoded result

If this is a grid type thing, then maybe you need to make your own version of NetworkImageView and its underlying support classes (sub-class).

Suggestion:

Have a look at Picasso as well, IF you have the freedom to choose the library. Looks very simple to use for your stated needs.



来源:https://stackoverflow.com/questions/26874926/scaled-images-with-volley

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!