I have an image loaded asynchronously by its url. Then i set the layout width and height according to the size of the of the image loaded. Is there a way i can get this aside from getting the size from my ImageLoader class using BitmapFactoryOptions (i forgot the, if it is BitmapOptions or BitmapFactoryOptions).
The problem is that at the start of the activity, the image is not rendered.
I am not quitely sure your case, but it is still able to get image's size by setting BitmapFactory.Options.inJustDecodeBounds = true even when the image is not rendered.
Related
I need to load some images from URI to Bitmap variables and perform some operation with them togheter. I need the bitmaps to be squared images with fixes size, scaled down and cropped. By now I use this code:
return Picasso.with(c).load(imageUri).resize(size, size).get();
but, obviously, the image will be resized without keep its aspect ratio.
I want to resize the image with these requirements:
the smaller dimension (width or height) should be equals to size
the greater dimension should be cropped to size, keep image centered
The key is using centerInside after resize. See link
Picasso.with(c).load(imageUri).resize(size, size).centerInside().get()
set your imageview height and width fix inside xml and then set image to imageview like
Picasso.with(YourActivityName.this)
.load(imageUri)
.into(imageview_id);
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.
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.
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.
I have an activity which only has an ImageView on it. I'm setting an image bitmap for this view that is much taller than wider. So, for example, my activity has 720x950 and my image is 918x2077.
When I add the image in the imageView, both side parts of imageView stays white (read: no image in there).
If I call mImage.getWidth() it returns me the width of the entire imageView of 720 (which is the activity wide).
I would like to get the width of the image in specific, not of the view.. is that possible?
It sounds like the aspect ratio of your image is being kept. You want to know how much width the image is actually occupying.
You can calculate it!
if your bitmap is 918x2077 and your window size is 720x950, then to calculate your width, simply do:
(950/2077) * 918 = 419.8844...
Hope this helps :)
So I'm trying to make my android app build a bitmap to the size of an imageview. The imageview's size changes based on the size of the screen, so how could I pass the size to a method that makes a bitmap that is always the size of the imageview?
Thanks
Pass your imageview object to your method which creates bitmap. Use that object to get height and width.
i am working on displaying an image and placing an icon on top of it... clicking the icon will show an enlarged version of the image...
though putting the imageview holding the image in a LinearLayout scales the image to the width of the dialog, the problem is that i need to display the image in a dialog but the image is very high resolution and hence is far bigger than the width of the dialog...
I need to show the actual image with scrolling for both ways to see the whole image... But whenever i try putting the imageView in a scrollview the top of my imageview is blank... and again though image scrolls downwards the width is scaled to the width of the dialog...
Helppppppppp guys....
Thanx for your help androidbase... I found the solution... Actually we can say its a workaround but better than any other solution i can think of...
Actually what i do is that i implement webview and directly load the image url in it using webview.loadurl(myurl);
and further i enabled the zoom featured too hence i get an allround complete zooming capabilities...
Thanx all 4 ur help
developer.android.com/reference/android/graphics/Bitmap.html
developer.android.com/reference/android/graphics/BitmapFactory.html
refer this link.
Bitmap mIcon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.twitter_icon);
imgview.setImageBitmap(mIcon1);
convert your image to bitmap and use. hope it works.....
EDIT:
URL img_value = new URL(string_url_input);
if (profile != null) {
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
image_view.setImageBitmap(mIcon1);
}