How do you properly scale ImageView - android

ALL,
I am developing an application on Android where I need to load an image from the web and present it to the user in a ListView along with some other stuff. So I made 2 XML files (for row and whole display), wrote code to get the image and wrote the appropriate adapter.
Here is what I have:
product_line.xml:
<ImageView
android:id="#+id/product_picture"
android:contentDescription="#string/product_picture"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/product_name"
android:layout_toRightOf="#id/product_picture"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/product_price"
android:layout_below="#id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/add_to_cart"
android:layout_below="#id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_to_cart"
/>
Unfortunately, upon running the code I see that the view is much bigger than the downloaded image and so takes a lot of spaces despite the "wrap_content" settings.
Is it possible to somehow set the view dimensions to display everything properly?
Thank you.
[EDIT]
I found this: http://argillander.wordpress.com/2011/11/24/scale-image-into-imageview-then-resize-imageview-to-match-the-image/, but unfortunately it doesn't make any differences.
[/EDIT]

To scale down your image view use:
android:adjustViewBounds="true"
android:maxWidth="32dp" <!--set the width you wish to be -->

Related

Activity layout android ImageView, ImageButton scaling

I want to achieve something of the format as shown in the image. The blue part is background, the image of the guy and girl is a ImageButton. The original images for these are 88x128 (trainer000, trainer001). I want to rescale the ImageButton such that the image covers the entire height and the width is adjusted accordingly. Now I found 2-3 ways to do so but they make use of a separate class to calculate width. I want to know if there is a way to do it purely in the xml file itself.
What I used:
`
<ImageButton
android:id="#+id/maleimage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:layout_alignParentLeft="true"
android:src="#drawable/trainer000" />
<ImageButton
android:id="#+id/femaleimage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:layout_alignParentLeft="true"
android:src="#drawable/trainer001" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:text="Are you a boy or a girl?"
android:textAppearance="?android:attr/textAppearanceMedium" />
`
What I want is this:
http://imgur.com/9D6XZUT
EDIT: Found the solution. I just used Studio's Layout editor.

What is a correct way to place image over ImageView?

I'm novice in android development and still can't understand fully how sizing works with different layouts. I want to place a preview of the book into this template:
I've tried to implement it using FrameLayout. The idea is that the center of preview image will be exactly where the center of the png background is. Here is the code:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/book_frame" />
<ImageView
android:id="#+id/previewImage"
android:layout_width="83dp"
android:layout_height="83dp"
android:layout_gravity="center_vertical|center_horizontal"
android:src="#drawable/abs__ab_bottom_solid_dark_holo" />
</FrameLayout>
The result in layout builder look exactly like I want it to be:
On real phone it is different:
I think on other resolutions it will also differ from both variants. So my question is how to synchronize these images so after any resizing and distortions the preview will fit the cover correctly?
Possible solution would be to remove border from image and place it on previewImage instead. But there are several similar usecases in application where the border can't be removed, so I'd like to find out a universal solution for all of them.
You have your answer in your question.
What happening in your case image size matter for different screen resolution.
Hard-coded things always gives weird result in your case
android:layout_width="83dp"
android:layout_height="83dp" this piece of code.
Check this link this will guide you to manage drawables for different screens.
and here is another link
So the suitable solution for me was to separate border of inner image into its own ImageView, insert it into layout over the photo and add 1dp padding to the photo.
The layout become like this:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5" >
<ImageView
android:id="#+id/bookFrame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:src="#drawable/book_frame" />
<ImageView
android:id="#+id/previewImage"
android:layout_width="83dp"
android:layout_height="83dp"
android:layout_gravity="center_vertical|center_horizontal"
android:padding="1dp"
android:scaleType="centerCrop"
android:src="#drawable/abs__ab_bottom_solid_dark_holo" />
<ImageView
android:id="#+id/previewBorder"
android:layout_width="83dp"
android:layout_height="83dp"
android:layout_gravity="center_vertical|center_horizontal"
android:src="#drawable/preview_border" />
</FrameLayout>

How does object scaling through different screens work?

I am new to Android, but I still managed to design an app that runs just fine on my smartphone. I read the tutorial about Android support for different screen sizes and I thought: "Ok, that's great, Android itself will scale the app in order to fit other screen sizes. I just need to have different images for different resolutions so that they will look better."
Well, it seems I didn't understand how it works exactly. By using a Virtual Device within Android SDK, I run my app on a smaller screen size. Unfortunately, there was no automatic "scaling" and half of my app layout is not shown on the screen. An imageButton that on my smartphone was one sixth of the screen, it occupies maybe one third of this new smaller screen.
What did I forget? I read some explanations and other posts but the info I got seemed to indicate an automatic scaling that doesn't happen... Maybe I miss some basic rule?
Thank you
EDIT:
Here below I add a bit of my code as an example. How should I change it in order to make the ImageButtons fill the smartphone screen in the same way, even if the screen sizes change? (if it's possible).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/greenbackground"
tools:context=".GameFrame" >
<ImageButton
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="27dp"
android:background="#android:color/transparent"
android:clickable="false"
android:contentDescription="#string/app_name"
android:src="#drawable/empty" />
<ImageButton
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/image1"
android:layout_alignTop="#+id/image1"
android:layout_marginLeft="16dp"
android:background="#android:color/transparent"
android:clickable="false"
android:contentDescription="#string/app_name"
android:src="#drawable/empty" />
<ImageButton
android:id="#+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/image2"
android:layout_alignTop="#+id/image2"
android:background="#android:color/transparent"
android:clickable="false"
android:contentDescription="#string/app_name"
android:src="#drawable/empty" />
</RelativeLayout>
You need to use a ScollView in order to display UI elements which aren't visible on the small screens. Keep in mind that a ScrollView can only have one child:
<ScollView
[...] >
<MyParentLayout
[...] >
<MyUiElements
[...] >
</MyParentLayout>
</ScollView>

Overlapping image not properly displayed on all formats

I'm building a graph which consists of 2 images: 1 background image and one image containing the actual data points. The data points image show be aligned at the top of the background image.
The layout looks like this:
<RelativeLayout
android:id="#+id/graphImageWrap"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/graphImageBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:contentDescription="#string/stroom_grafiek"
/>
<ImageView
android:id="#+id/graphImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/graphImageBackground"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:contentDescription="#string/stroom_grafiek"
/>
</RelativeLayout>
This works well on a HDPI device:
But on a MDPI device it doesn't align properly:
I hope someone can explain what the problem is.
You may want to use a FrameLayout instead, and make sure the images are the same size.

ImageView scale type not working in list activity

I have used ImageView's before and understand the different scale types that can be set... However I am having an incredibly difficult time trying to get an ImageView to scale properly in the row of a ListActivity or an ExpandableListActivity.
I have tried setting the android:scaleType property to every single value but the image never scales down. I have set the min and max sizes as well and they don't seem to have any effect. I have done both of these things in both the XMl and in code to no avail...
Does anyone have any ideas or perhaps a workaround?
Thanks in advance!
EDIT: Here is the XML for the group rows in an ExpandableListView
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
>
<ImageView
android:id="#+id/item_selection_icon_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginLeft="30dp"
android:minWidth="10dp"
android:minHeight="10dp"
android:maxWidth="10dp"
android:maxHeight="10dp"
android:scaleType="centerInside"
/>
<!-- App Name -->
<TextView
android:id="#+id/item_app_name_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/item_selection_icon_id"
android:layout_alignBaseline="#id/item_selection_icon_id"
android:textStyle="normal|bold"
android:textSize="24sp"
/>
<!-- Package Information -->
<TextView
android:id="#+id/item_app_pkg_name_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/item_app_name_id"
android:layout_toRightOf="#id/item_selection_icon_id"
android:layout_weight="2"
android:textStyle="italic"
android:textSize="12sp"
/>
</RelativeLayout>
Thanks to Roman, this problem has been solved! I removed the min and max width and height lines and changed the android:layout_width/height attributes to the size I wanted and everything is now working out just fine.

Categories

Resources