I have a requirement to place an image at the end of layout, and it will be 20% out of the screen boundary, I am unable to find out any way to achieve this, the only possible way is to set image view width to 1000dp, the some part of image is out of the screen, but this does not seem to be a valid approach
I want to achieve something like following.
Try using Horizontal/vertical bias in ConstraintLayout. Something like:
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="104dp"
android:src="#drawable/ic_person_black"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.2"
app:layout_constraintTop_toTopOf="parent" />
In above example imageview is cropped 20% from right
Related
There's an ImageView with certain size (100dp, 100dp). And there's an image which size is 1000-1000px.
That's how it looks right now:
1
What i want is:
2
So image wont come out of ImageView's borders and will be sort of zoomed to center in some value, that i can change. Can it be done in some way? Maybe i need something different from ImageView? At the moment, i use adapter for recyclerview, which loads list of items that contain an image that needs to be kinda zoomed.
<ImageView
android:id="#+id/cart_item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/min" />
just add android:scaleType="centerCrop"
here is the code look like:
<ImageView
android:id="#+id/cart_item_image"
android:layout_margin="5dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop" />
I would like to have an image always perfectly centered on top of the bottom of another image.
My first thought was setting the background image's bottom constraint to, for example 100dp from another view, and then the foreground image's bottom constraint to 50dp from the same view, and set it's height to 100dp.
This way it will always be on the center of the background image's bottom center, but I am making the foreground view not responsive by giving it a fixed size, which is something I would like to avoid.
Another thought I had was to constraint both the top and bottom of the foreground image to the bottom of the background image, but then the view just collapses.
I can't find any settings that would allow that to work.
*I need to keep a ratio for the foreground image.
I found an answer here:
https://gist.github.com/writtmeyer/f5971266394b2316eb01b566cdeff2a0
It suggests a clever way if using a dummy view.
You can do it by constraining your view to the start and end of the target, so that it will keep centered to it. Here's a sample:
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/targetView"
app:layout_constraintStart_toStartOf="#+id/targetView"
app:layout_constraintEnd_toEndOf="#+id/targetView" />
I think this is what you are looking for.
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="#mipmap/ic_launcher" />
<ImageView
android:id="#+id/foregroundImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#id/backgroundImage"
app:layout_constraintEnd_toEndOf="#id/backgroundImage"
app:layout_constraintStart_toStartOf="#id/backgroundImage"
app:layout_constraintTop_toTopOf="#id/backgroundImage"
app:srcCompat="#mipmap/ic_launcher"/>
I wan to place Small ImageView on the anchor of CircularView just like below image
My Code is
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.hey.heypickup.UI.UICircularImage xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/contct_imgpic"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_margin="5dp"
android:layout_weight="0.04"
android:padding="10dp"
app:line_color="#color/colorPrimary"
app:line_color_press="#color/green"
android:src="#drawable/ph_1"
app:line_width="1dp"
app:padding="0dp"/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="bottom|end"
android:src="#drawable/ic_phone_black"
/>
</FrameLayout>
Result of above code:
but the Second imageview not appearing at the achor of circularImageView?
Can we achieve this without using floating action button ?
if yes then how ?
Since your second image is overlapping the first image,
I think it's because your ImageView has a wrap_content height and width. You should make it a fix size, and try it out because if your ic_dialog_email image is large enough then it will take entire space and might overlap the first image.
Would you make height and width fix and try again? For example, make it 15dp each and take a look. Also remove that margin from ImageView along with that change.
You'll just need to trial and check the exact size your need for the second image along with required margin to position itself correctly as per your UI requirements
Ok here is the problem...
I have a image background that need some text and additional graphics on it. The background image needs to be in the center of the screen and may not stretch. Here is what i need to do:
The problem is that i need to align the text to the background image.
I've tried to wrap it all into a relative layout - something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bg_image"
android:layout_centerInParent="true"
android:src="#drawable/member_card"/>
<TextView
android:layout_height="wrap_content"
android:layout_alignLeft="#id/bg_image"
android:text="#string/membercard_info"
android:layout_marginTop="40dp"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignLeft="#id/bg_image"
android:layout_marginTop="200dp"
/>
</RelativeLayout>
This will not work since android adds additional padding to the image to avoid it from stretching.
Here is what happens in two different cases:
So how do I align the text to the background image?
I've solved this problem in the past in code by baking it all into one image,- but would like to do this in xml.
If you want to remove padding, you can use manually set it. However, if you want overlapping elements, you generally want to use FrameLayout. Look here: http://mobile.tutsplus.com/tutorials/android/android-sdk_frame-layout/
Set a gravity inside the frame layout to align it.
if you want an ImageView with additional layers drawn on top of that, see this thread: How to maintain multi layers of ImageViews and keep their aspect ratio based on the largest one?
There a padding around the image because you set the imageView size to fill its parent ( using match_parent )
You should try to set it to wrap its content :
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>
EDIT : If your picture is bigger that the screen size, you need to have it scaled keeping the aspect ratio.
To do this, use match_parent in vertical with a scaleType to FIT_CENTER
and keep the wrap_content setting for the width ( since we want the image view left/right bounds stuck to the image content )
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
.../>
Is this better ?
I have an ImageView, and I want it to be clickable. The image itself is only 32x32 pixels, but I want the clickable region to be larger so it's easier to hit. I was hoping I could set the layout margin values to increase the size of the imageview's clickable area:
<ImageView
layout_width="32dip"
layout_height="32dip"
layout_margin="20dip" />
That doesn't work, what I could do is just recreate the image resource to have a larger transparent region around it. That's a bit annoying because it's hard to tweak if I ever need to change the clickable region. It's not just a single png either, it's a statelistdrawable so I have to resize 3 different pngs if I ever need to tweak the clickable area.
Anything else I can do?
Thanks
Use padding. layout margins are used if for inserting space outside the boundary of the view.
for equal padding on all sides
<ImageView
android:padding="20dip" />
or to set the padding on each side
<ImageView
android:paddingLeft="10dip"
android:paddingRight="15dip"
android:paddingTop="23dip"
android:paddingBottom="12dip" />
Hope that helps !
Instead of resizing the image (Peter Knego's answer) or increasing padding (Saimon's answer) I suggest to set the minWidth and minHeight attributes in combination with scaleType="center":
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="40dp"
android:minHeight="40dp"
android:scaleType="center"
android:src="#drawable/your_image" />
That makes sure that small images have at least a size of 40x40dp while the image is not up-scaled if it's smaller. If one day the image is going to be replaced with another image larger than minWidth and minHeight it will not grow larger than 40x40dp as it gets downscaled. Thus a minimum clickable dimension of 40x40dp is always guaranted and the image is always displayed nicely.
Suggestions (never tried it myself):
Create ImageView as large as you want than put image into it without scaling
ImageView.setScaleType(ImageView.ScaleType.CENTER).
Brute force approach: create new larger png that has original image centered and the rest of it is transparent.
Use like below; where width/height is your touch radius
<ImageView
android:id="#+id/editProfile"
android:layout_width="32dp"
android:layout_height="32dp"
android:padding="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_edit"
android:scaleType="fitCenter"
/>
In the above code, I wanted my src size to be viewed as 24dp width/height. And touch radius of 32dp. so I had used padding of 8dp.