Why android resizes image? - android

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.

Related

Add white spaces to an image to fit to a specific resolution

In apps like NoCrop an image is made to fit a resolution of 1:1 (made square) by adding some extra spaces at the top and bottom of the image (shown below).
Similarly, I wish to add some spaces to an image through my Android app, to fit a resolution of ratio, say 16:9. My app creates a video by stitching these images together, and for the video, all the images first need to be converted into a fixed resolution.
I have not used Bitmaps much, nor I am an expert in Android. Any kind of help would be appreciated! :)
Try adding these to your ImageView in xml:
android:scaleType="centerCrop"
android:adjustViewBounds="true"
This will crop your image so that it fits the ImageView

I want to enter images in my android activity. Where should I put the images?

I'm using Android Studio 1.2.2 I made a form activity and I want to insert an image. Where should I put the image? What changes I have to do in my mainactivity.java
file?
You can use Android's ImageView tag. You need to write it inside .xml file of your activity. You can take reference of it from android's devloper's website.
In Andorid studio you can not directly drag & drop images.
It will be good for you to take reference of any example.Here is the ImageView xml code.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
You can now make this ImageView reference in your MainActivity (Java) like below:
ImageView imageView;
imageView = (ImageView) findViewById(R.id.imageView1);
Take a look at the docs: http://developer.android.com/guide/topics/resources/drawable-resource.html
Commonly, you will want to use some of the qualifiers by screen density and provide each image at different resolutions, docs here, this way lets Android pick up the best image size and scale it to match the phone screen.
Personally i use -nodpi and perform scalation by code, this way lets only have one copy of each image, and, gives you max control to position and size your images.
Hope this helps.

TextView Background Resource

I implemented the following TextView in a RelativeView:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/bg"/>
The problem is that the size will be set dynamically to 15% of the screen while the application is running. But when I do this the background ressource doesn't get the size of the TextView. It has the original size of 150x150 px. What can I do to avoid this?
(I don't want to take a smaller ressource because it doesn't look well then)
Try using another scaleType, for example android:scaleType="fitXY".
Also you can try making a nine-patch image. To do this, go to your Android SDK folder - tools - draw9patch application. Then you will have options to open your image and then save it as nine-patch image. Reference it as usual in your layout.

Avoid image scaling in ImageView

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.

Creating photo miniature in a List View row

I want to create a list with some text and a photo miniature in each row.
So I created a row layout with a TextView and a ImageView, my question is how do I make the ImageView just a little square and then make an image adapt to the size of the ImageView?
<ImageView .... android:scaleType="centerInside" />
Set full screen image as an image resource of this image view. It will be downscaled automatically.
Additionally you can limit width and height setting android:maxWidth and android:maxHeight attributes to prevent minature to be too large.
my recommendation is you do not scale the image to fit the view as this is an expensive operation and will slow down your app it is preferable to prescale the item to the size you want.
Take a look at this Strange out of memory issue while loading an image to a Bitmap object. It's a correct way to get miniature with minimum memory consumption.

Categories

Resources