Avoid image scaling in ImageView - android

I am using an Image view with these XML attributes
<ImageView
android:id="#+id/widget_list_bitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:scaleType="center"
android:src="#drawable/place_holder" >
</ImageView>
and at runtime I create a let's say 400x400 bitmap, save it to a file and set the ImageView src using a file URI
remoteViews.setUri(R.id.widget_list_bitmap, "setImageURI", myFileUri);
(this is to avoid RemoteViews issues with large bitmaps).
Everything works fine except that the displayed image is scaled down and is smaller than 400x400, more like 250 x 250 (possibly 400/density, density = 1.5).
Is there a way to force an ImageView to really not scaling images it fetches from URI?

You could manually download it and when you load it with BitmapFactory set inScaled to false. Then you would use setImageViewBitmap instead of setUri or setImageViewUri.
Update:
Unfortunately I don't really see a way around this using setUri. An alternative to using setUri to get around the 1MB limit is to use setImageViewBitmap but slice up the bitmap and display it on multiple views, maybe a 2x2 or 3x3 grid of ImageViews or something like that. You'd have to split it into multiple updates using partiallyUpdateAppWidget, but I think it should work. The hitch is that this requires API 11.

Related

Android Glide without resizing imageview

I've started an app where I'm getting images from a RESTful server in response to the user clicking on another component on the screen. I've set it up so that it gets the data back, creates a byte array and displays that on the screen - all good.
However I've learnt that this may have memory leaks and the best way to load images is with glide. I've got the simple glide working but it has problems. Specifically when I reload an image onto an image view the component shrinks to a height of zero and then resizes again. How can I say to Glide - keep the component the same size during the transition? Crossfade doesn't work and I can't see anything that can do this.
EDIT
The code with the glide is as follows:
Glide.with(this)
.load(Base64.decode(image, Base64.DEFAULT))
.into((ImageView) findViewById(R.id.cutoutImageView));
The image variable is a string containing the images retrieved from a RESTful service.
The XML defining the imageview is as follows:
<ImageView
android:id="#+id/cutoutImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/annotatedImageView"
android:layout_marginLeft="10px"
android:layout_marginTop="10px"
android:background="#color/colorPrimary"
android:padding="1dp"/>
Note that I can't hardcode the size of the image view because I don't know what size image I'll be getting from the RESTful service until I get it. I do know that once I get one image then the subsequent images will all be the same size though.
Another issue is that glide wants to scale the iamge - I don't I want to show the image at the size I get it. How do I do that?

Android drawable memory management

I implemented a RecyclerView in one of my Activities.
I use a customized RecyclerView adapter to load data from a SQLite Database (using Room and LiveData-structure) and display it in the view.
All of the rows have a large ImageView where I want to show a Drawable (just a .png). To do that I first tried to use the approach written on the official Android Developers site but it did not work well for me.
Therefore I am trying to use Glide as a library recommended by Google.
I have 16 drawables in my project. They all have a scaling between 800x800 and 1920x1080 pixels.
In my adapter I load the data from my database and based on the drawable id, the image as well (so there is no image data stored in the database; I did this previously).
Unfortunately my App cannot handle that amount of image cache which leads to an OutOfMemoryError exception. That's why I used
android:largeHeap = "true"
in my Android Manifest.
I know this is not a good solution and I also know that there has to be a way to use less memory for such a small amount of pictures.
In Glide I use
GlideApp
.load(R.drawable.my_drawable)
.fitcenter()
.into(myImageView);
But unfortunately the image does not get shrinked or smaller. I thought that Glide can scale the image based on the size of the ImageView, screen size and so on.
What am I doing wrong? Is there a better way to use less memory without fixed scaling so that my pictures not become ugly?
EDIT:
I first stored my images in the basic "drawable" folder in my project structure (copy and paste).
My second approach was the gimp-android-xdpi addon which exports images or icons for any android density (mdpi, hdpi, xhdpi etc.). But this did not help either.
call override(horizontalSize, verticalSize).
This will resize the image before displaying it in the ImageView.

showing image of sixe 64 X 64 on any size imageview without loosing its quality

I am retrieving image from server and setting it to image view.The image size is 64 X 64 .
But I want it to suppport all size image views. If I use imageview of size 150dp X 150dp
it gets blurred out,it stretches.Is there any method so that my image maintains its quality even on bigger size imageview?I had heard to use ninepatch images in drawable but here i'm downloading it from server and image size is fixed 64 X 64 .
Plz Help!!!!
NinePatch is for stretching images, usually only for background images with a single color or a gradient in one direction. So that is not your answer.
As to showing your 64x64 image as 150x150.. no. It's just as if you'd do image enlargement in an image editor - the information is not there. Although it shouldn't be that bad.
Check this to see if any alternative fits your needs:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Typically used:
<ImageView
android:id="#+id/image1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="0dp"
/>
I am afraid it can't be done...Not with all image types at least. There are images called VECTOR images which retains quality at significant level of image resizes. So you should try using some vector kinda images which could be of .PNG images, etc.
Because shrinking a larger image works most of the time, but enlarging smaller images will always under distortion.

Scaling image without losing quality

Is it possible to scale image without loosing its quality?I have seen posts that say to use Ninepatch images but how can we download an image and convert it to ninepatch image so that i can show it in ImageView.
Is there any other way so that my images with smaller size can be scaled like in whats app
Everytime you scale an image it's losing quality, the only thing you can do to keep the quality is keeping a copy of the original image (which doesn't make much sense since you can just load it again from resources).
If you mean the problem was that android was scaling your images (and changing the quality) you can put images on a folder named "raw" inside res.
Ninepatch images can be made manually by you, you just paint an image that has two extra pixels on width and two on height, and when you save it , name it something.9.png
if you want to know how to draw them correctly follow this: http://developer.android.com/tools/help/draw9patch.html

Why android resizes image?

I'm trying to change my application data source to the sdcard so that it doesn't take too much internal memory.
However, I had an Image that was dynamically loaded from the resource and displayed in an ImageView, now I load the image from the sdcard whenever I need it.
I had been using imageBox.setImageResource(imageID) and I changed to imageBox.setImageDrawable(imageDrawable).
The problem is that now Android resizes my image and it doesn't fit where it should.
Why is it resizing the image this way? And how can I stop it?
Thanks in advance :)
Add the tag android:scaleType="fitXY" to your ImageView's layout XML.
Ex.
<ImageView
... several settings ...
android:scaleType="fitXY"/>
Edit:
If your image is simply too big to fit inside the ImageView - it will have to do some kind of scaling. There are some other options as described in the API:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Try the following for ImageView:
android:layout_height="fill_parent"
android:layout_width="fill_parent"
No Scale Type.
The ImageView Should be within Fixed Size (in dp) Parent View.

Categories

Resources