How to rotate ImageView and have its size adjust automatically? - android

Image 1 is without rotation,I want image 3 when I rotate ImageView, but something like image 2 is displaying. How can I have the ImageView change its dimensions automatically?
My guess is that after rotation, the width and height of the ImageView is preserved, and I do not want that
P.S. I don't want to change the orientation of the layout
here is the picture(Rectangle is the phone, person is the imageview)
Here is my xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/page">
<ImageView
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>
</FrameLayout>
<!-- I tried all scaleTypes, and set height/width to fill_parent, wrap_content,and match_parent nothing worked-->
any help is appreciated. thanks!

Remove adjustViewBounds and use scaleType fitCenter. It should work. Try this:
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>

Try this:
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:adjustViewBounds="true"
android:rotation="90"
/>
Edit:
android:adjustViewBounds="true"
will set scaleType="fitCenter", so I think you should try scaleType or adjustBounds ,no both.
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>

Related

How to adjust how much an ImageView is cropped by when using scaletype centerCrop on Android?

I have an ImageView (PhotoView) within a LinearLayout and I am applying scaletype center crop but is cropped a bit too much and appears too zoomed in. How can I resolve this?
<LinearLayout
android:id="#+id/photoViewContainer"
android:layout_width="wrap_content"
android:layout_height="#dimen/flexible_space_image_height"
android:background="#android:color/holo_purple"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/mainImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:background="#android:color/black" />
</LinearLayout>
I am not sure if you can control the cropping, but using scaletype="fitXY" and providing some padding worked for me.
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/mainImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:padding="16dp"
android:adjustViewBounds="true"
android:background="#android:color/black" />
In this, the scaletype="fitXY" would squeeze the image to the size of screen and padding it would fit it in center.
Also there is a hack that i have heard of, in which you replace the padding by negative padding, which would zoom out the image, but i have not tried it.

How to make the size of ImageView independent to parent when it is larger than the parent?

for example, if a 80dp square layout contains a smaller ImageView, the size of ImageView will not be scaled:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#FFFF00"
android:clipChildren="true">
<ImageView
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
</RelativeLayout>
but if the parent is smaller than the ImageView, eg:30dp:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#FFFF00"
android:clipChildren="true">
<ImageView
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
</RelativeLayout>
it would be scaled automatically:
but I want the ImageView cropped instead of scaled:
how can I get this crop effect in xml?
There is alot of techniques available.
Try to Add both attribute in your ImageView xml
android:scaleType="matrix"
OR
Use margin-right,left,up,down.
OR
Search Icon size in google and then edit this icon in photoshop give it a fix size of icon .
try adding following attribute in your ImageView xml
android:scaleType="matrix"
hope it helps :)

ImageView bounds extend beyond image

I have an ImageView defined as:
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-80dp"
android:layout_toRightOf="#+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="#drawable/tester"
android:adjustViewBounds="false" />
and I see this in Android Studio in the layout preview:
And notice the bounds of the ImageView. Is it possible to have the bounds be along the actual edges of the image I am displaying?
<ImageView
android:id="#+id/imageView"
android:src="#drawable/tester"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
Adding the scaleType to centerCrop should fix it for you. You can change the centerCrop to any other suggestions according to your need.
you have to set the scaletype to fitCenter
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-80dp"
android:layout_toRightOf="#+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="#drawable/tester"
android:adjustViewBounds="false"
android:scaleType="fitCenter"
/>
set android:adjustViewBounds="true"
It will set the bounds of the imageView to match the content
I think, the image is too large and you re wraping it so the imageview layout will be larger than you wanted to. Crop the image seems like the only solution you can do. You can try to scale

ImageView width and height does not work

I use an ImageView in my Relative Layout.
This is the Code:
<ImageView
android:id="#+id/jpicture_imageView"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/jName_TextView"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="fitXY" />
For testing purpose, I set width and height to 10dp, but the picture is always it's original size.
What am doing wrong?
When doing this kind of resizing stuff try going from crap and adding attributes little by little (when you are not sure what you are doing ;)
<ImageView
android:id="#+id/jpicture_imageView"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/jName_TextView"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="fitXY" />
This part can be the problem :
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/jName_TextView"
android:layout_centerVertical="true"
You align the view left and right, below another view and center vertically.
With the attibutes fitXY and adjustViewBounds the view will not be only 10dp. Try changing those attributes.
Delete them and add them again one by one to see the changes for yourself
the part with
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
was the problem. Removing one of them fixed it.
Thanks for yur answers.
Best regards,
Peter

How do I remove extra space above and below imageView?

I have a really simple image within a RelativeLayout and for some reason I am getting extra spacing on the top and bottom which I can't remove. How can I clear it out?
Here is what it looks like:
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/slice11pp" />
</RelativeLayout>
Try this
android:adjustViewBounds="true"
In your above imageView just add android:scaleType="fitXY"
Your problem is probably that you have no scale type set... Add it to the XML for the ImageView, "fitCenter" should be correct, but there are others, check: ScaleType.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/slice11pp"
android:scaleType="fitCenter" />
Must be enough adding the property:
android:adjustViewBounds="true"
for example:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/slice11pp"
android:adjustViewBounds="true" />
If your image view height is match_parent, even if you set android:adjustViewBounds="true" ImageView will add some extra blank space at top and bottom side. so, change ImageView height to wrap_content and set android:adjustViewBounds="true"
for example
<ImageView
android:id="#+id/img_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
Use drawable-nodpi folder if there is no specific requirement for images. Then android: adjustViewBounds = "true" acts as the default.
If you use drawable-nodpi you don't need to set android:adjustViewBounds = "true".
I think this is the most effortless method.
android:scaleType="centerCrop" by adding this
<ImageView
android:id="#+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#drawable/temp_image"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
works for me Before and After Image
this line of code will solve the problem
android:adjustViewBounds="true"
don't forget to rotate the image
just add ScaleType="fitxy" inside the Image view
Try this
<ImageView
(...)
android:adjustViewBounds="true" />
this is the perfect solution
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
it will not leave any extra single dp in any side. however the image will be distorted if it is not portrait image.

Categories

Resources