I want to diplay an image so that it takes up a minmum width of the screen and should scale in its aspect ratio (the image width might be smaller or larger than the minimun width) I also want to have a fixed size border around this image. This is my xml section for it:
<ImageView
android:id="#+id/detailed_view_boxArt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/detailed_view_heading"
android:padding="10dp"
android:layout_marginLeft="5dp"
android:scaleType="centerInside"
android:background="#000"/>
This however does not produce what I want...the problem is that it does not scale the image horizontally far enough (ie. the 10px padding that I have seems a lot more on the right and left)
How would I produce the result that I want?
to set minimum width and heidht use the attributes android:minHeight="100dip" and android:minWidth="200dip".
to set the Border check my post.
Try this (I emphasized the important part):
<ImageView
android:id="#+id/detailed_view_boxArt"
*android:layout_width="match_parent"*
*android:layout_height="wrap_content"*
*android:adjustViewBounds="true"*
*android:scaleType="centerCrop"*
android:layout_alignParentLeft="true"
android:layout_below="#id/detailed_view_heading"
android:padding="10dp"
android:layout_marginLeft="5dp"
android:background="#000"/>
Related
How can I get an ImageButton that has a fixed height and is only as wide as it needs according to the ratio of its source image?
I tried the following:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="8pt"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/list_download"
android:background="#android:color/black"
android:padding="0pt"/>
I set background to black just to see the boundaries of it exactly, and here is the outcome.
I have seen some solutions that suggest to use negative paddings but it is not an elegant solution. There should be a better one.
I tried similar configurations on an ImageView too, but it also had extra padding. So it's not an ImageButton specific problem (i.e. it is not related to this nine-patch issue).
EDIT: If I change scaleType from fitCenter to fitStart, then the outcome is like this. So there is somehow a minimum width.
It's because you've set a limited height. That limits the image height, but when Android calculates the width of the content (the original unrezised image), it's wider.
You'll get the image without the black "padding" on the side if you use wrap_content for the height as well.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/list_download"
android:background="#android:color/black"
android:padding="0pt"/>
So, if you want the picture to be a specific size, you could resize it in your drawable folder to the size you want.
you just need use android:scaleType="fitXY" and set padding 0dp.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:padding="0dp"/>
you can put it in a Frame like so :
<FrameLayout
android:layout_width="wrap_content"
android:background="#android:color/black"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/list_download"
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="8dp"/>
</FrameLayout>
it will be much easier to play with it like so
Here is a small gif of my current situation. I guess many images are better than many words.
As you notice, when I switch for the smaller image, the seekbar size changes. I know this is because of the seekbar's width being set to match_parentand the image's width to wrap_content.
What I don't know is how to overcome this problem, as I can't really hardcode width value otherwise it'll probably get messed up on various screensizes.
So my question is :
Is there a way to prevent this behaviour that is clean? I could simply get the width at runtime and set it as minimal width and it would probably work (heck, I could try it now to make sure), but that is just a horrible thing to do code-wise.
Ideally, I'm guessing there is a way to prevent this in the .xml file, but I couldn't figure it out, playing with the different paddings, margins, scaling and layout sizes.
Here is what you're looking at :
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageButton
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:id="#+id/playButton"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp"
android:layout_centerVertical="true"
android:src="#drawable/play"
android:onClick="PlayButtonClicked" />
<SeekBar
android:max="100"
android:id="#+id/volumeSeekBar"
android:layout_centerVertical="true"
android:layout_margin="15dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_toRightOf="#+id/playButton"
android:layout_toLeftOf="#+id/muteButton" />
<ImageButton
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:id="#+id/muteButton"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:layout_centerVertical="true"
android:onClick="MuteButtonClicked"
android:src="#drawable/sound" />
</RelativeLayout>
Ideally it would be nice for the volume icons to be of the same size.
If that is not an option, try replacing android:layout_toLeftOf="#+id/muteButton" with android:layout_marginRight="15dp+30dp+largest_icon_width_in_dp"
where 15dp is your seekbar right margin, 30dp is the mute button right margin.
The third option would be to set a fixed width for the mute button equal to the width of the largest image.
I have an ImageView which needs to have a maximum height of 150dp and a width that matches the parent. However, what I need is for the actual image to be cropped, so the picture is as wide as the ImageView, but centered and cropped so the top and bottom of the picture aren't visible (sort of a preview). What I have currently:
<ImageView
android:adjustViewBounds="true"
android:background="#fafafa"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxHeight="150dp"
android:id="#+id/articlepreviewimage"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/naslovclanka"
android:layout_centerHorizontal="true"
/>
Also, it's crucial that the image stays in its' original proportions, so no putting it as background, no scaling etc. I just need it to fill up the width of the ImageView and show the center of the picture in that ImageView.
The way its' currently done, it scales the image so it fits into the height or width, but doesn't cut away anything. How to do this?
take a look at android:scaleType="centerCrop"
There are also a lot of other scale types that might fit what you what.
Try doing the following:
<ImageView
android:id="#+id/yourimageId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:layout_gravity="center_vertical"
android:clickable="true"
android:scaleType="fitXY"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:maxHeight="150dp"/>
I do not want the (black) space between the imageview and the following textview. what am I doing wrong? Thanks for your help.
<RelativeLayout
android:id="#+id/layout_contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/club_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/condition_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/club_banner"
android:background="#drawable/overview_cell_header_bg"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:text="#string/Condition_1"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="normal" />
The problem is that firStart uses the START constant from the Matrix.ScaleToFit that will only chooses to scale the image either x or y axis
documentation:
Compute a scale that will maintain the original src aspect ratio,
but will also ensure that src fits entirely inside dst. At least one
axis (X or Y) will fit exactly.
solution:
You need to use the fitXY instead to scale the image from x and y axis
Problem:
android:scaleType="fitStart"
This makes the picture fit at the start (top) of the ImageView.
Change the scaleType to something else that matches your needs.
I tried your code and I get the expected result.
The problem must be in your image I guess. Is that part below the pic actually transparent?
for whom is interested in this i solved the issue by simply adding
android:adjustViewBounds="true"
i have this little layout i defined to inserted in every page of my app:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#null"
android:src="#drawable/sa_info"
android:layout_alignParentRight="true"/>
i've sa_info.png that is 118x118 but i want it to look much smaller on the screen. if i try to set width and height of the imagebutton to the desired size (lets say 48dp) the resulting image when i run the app is cropped to the center:
so the only way i have to get the desired result is scale the image itself to the desired resolution.
what i'm doing wrong, is there a better way to do this?
Try adding
android:scaleType="fitCenter" -> http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
android:adjustViewBounds="true" -> Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.