Scaling pictures - android

Welcome.
I have a problem with the clarity of the image.
Images displayed by:
<ImageView
android:id="#+id/imageView"
android:layout_width="260px"
android:layout_height="190px"
android:layout_gravity="center"
android:src="#drawable/icon" />
The problem is that computer shows the picture is clear but it's not on the phone.
How do I handle this?
Image has a resolution of 260x190 pixels.
Regards.

<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon" />
That wraps the image in a way that does not strech it in any way and because the image looks bigger on bigger devices and smaller on smaller devices(aka scale).
By setting a fixed size, it shrinks on bigger devices and it blurs on smaller devices because the image gets bigger and the scale is all wrong

Related

Android: OS not re-sizing images with the same scale

I am troubled by how the Android OS is handling resizing on an app of mine. Basically I have a background image that is 480x800 (so 480px wide) and an image that goes at the bottom, also 480px wide.
On the Galaxy S (480x800 screen) everything looks fine, as shown below:
On the Galaxy S3 (720x1280), however, the background is getting stretched to 720px wide but the image at the bottom only to 640px wide, as shown below:
I tried to create a xhdpi folder with 640x960 images, but same thing happens (background stretched to 720px, image only to 640px). I also tried to play with "ScaleType", but no luck so far.
Does anyone know why the OS would re-size the two images with a different scale, and how to fix it?
Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bg" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/significados"
android:layout_gravity="center"
android:layout_marginTop="216dp"
android:onClick="goToSignificados"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ranking"
android:layout_gravity="center"
android:onClick="goToRanking"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/engracados"
android:layout_gravity="center"
android:onClick="goToEngracados"
/>
<ImageView
android:id="#+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:clickable="true"
android:contentDescription="#string/image"
android:onClick="goToMarket"
android:src="#drawable/googleplay" />
</LinearLayout>
You'll have to post your XMLs to get a more specific answer but basically all "Views" may handle image scaling differently.
In your example,
You can make your button stretch all the way by using width = match-parent.
"Backgrounds" will stretch by default, but can also tile.
"Buttons" will have a min size of the background size but will stretch when needed (button has too much text)
As a side note, you shouldn't depend on the exact pixels of the images. You should look into nine-patch or making tiled backgrounds and make sure you take advantage of things like "match-parent, gravity, etc."

Resizing Image to fit Imageview

I've look at MANY questions on Stackoverflow and tried many workaround but it still didn't work.
What I wanna achieve :
Yet I am achieving this :
This is my current xml code:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/text"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/ic_action_search" />
The best way to achieve this is to scale the image (bitmap) using a Matrix.
An alternative is to use a library I previously created which does this for you:
https://github.com/laurencedawson/ZoomableImageView
This will automatically scale the image to fit the screen and also add pinch to zoom.
add
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/text"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/ic_action_search" />
CenterInside, Resize Image According To view dimensions and its Resolutions. So if an Image Has size of 300*450 and we have set this image to an imageview of scale type centerinside, with dimensions 200*400, then it will make size of image to 200*300, According to the target dimensions, so scaling it down make 200 is minimum in width and height. I hope I am clear enough. In cases where you need to set dimensions of exact size use FItXY, as suggested by Adeel.
You want to do it inside the XML file right?
<ImageView
android:layout_width="size of image (in density pixels)"
android:layout_height="size of image (in density pixels)" />
so for example:
<ImageView
android:layout_width="60dp"
android:layout_height="60dp" />

Android: layout with imageView re-sizing buttons wrongly

I have a layout problem, I want an image to be in the middle of two buttons. So I created the two buttons and defined a weightSum of 1. I then set the weight of the two buttons to .40 and the image to .20 so that the image would be a small logo in the middle of the two buttons. The problem is the image is stretched out and the buttons are pushed aside.
<Button
android:id="#+id/buttonClear"
style="#style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".40"
android:background="#drawable/custombuttonred"
android:text="#string/clear"
android:textSize="50sp" />
<ImageView
android:id="#+id/myimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:src="#drawable/imageid" />
<Button
android:id="#+id/buttonBackspace"
style="#style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".40"
android:background="#drawable/custombuttonred"
android:text="#string/backspace"
android:textSize="20sp" />
I had also faced the same issue. I used the button insted of ImageView and set background image to it.
This code: http://pastebin.com/3P9BWEcQ
Works well for me
I think you could use dp for resizing the image for both height & width.Say if you ar image is in original size of 400px width & 200 px height,just resize according to the screen size i.e if you are device in which you are testing currently could only hold half of the image size then android:layout_width="200dp" android:layout_height="100dp".If you follow resizing using dp image will get resized to both the smaller & larger device you are using.Make sure that when you have any larger devices screen than the one currently you are testing,just try to have the image as large as possible to get the best results.

Image Size in larger devices

I have this image in my app, when I tested it in my Droid X and Droid bionic and other smaller devices, it looks fine. But when I tested it on my Android Tablet, the image was stretched. I just want to know if there is a proper way to solve this issue.
Note: I have tried adding the actual size of the image like 200dp by 300 dp. But it still don't work
Here is my code
<RelativeLayout
android:id="#+id/image"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/kittens"/>
</RelativeLayout>
don't use wrap_content for ImageView, instead use dp like:
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/kittens"/>

How can I scale an image using only one dimension in Android?

I am trying to scale an image specifying only the width I want. The height would be scaled in a way that the aspect ratio is preserved. The image I am using is big enough to cover the whole screen. The code I am trying looks like this:
<ImageView android:src="#drawable/logo"
android:layout_width="100sp" android:id="#+id/imageView1"
android:layout_height="wrap_content" android:scaleType="centerInside"/>
If I use "fitXY" the image doesnt preserve the aspect ratio and when I use "centerInside" the image is not scaled.. Any idea?
Thank you in advance.
Jesbomar's solution with pretty formatting:
<ImageView
android:src="#drawable/logo"
android:adjustViewBounds="true"
android:layout_width="100sp"
android:id="#+id/imageView1"
android:layout_height="wrap_content"
android:scaleType="centerInside"
/>
I prefer scaleType="centerInside" to "scaleXY" to make the image as big as possible without changing the aspect ratio or cropping any part of it.
Thanks for coming back with your solution jesbomar, it was very helpful.

Categories

Resources