How to properly scale imported image to fit ImageView? - android

In my application I am importing and displaying many images. To set img uri I am using Img.setImageUri(R.mipmap.myimage) But the result is making image croped in device determined way for example on my phone i have square app icons and layout looks like this
so it's square shaped and on emulator which has circular icons it looks like this
so it's in shape of circle
What i want is to resize images to fit image views
Images are stored as uri in database so i NEED to get result based on that.
my xml for it
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
android:layout_height="120dp"
android:id="#+id/foodlay"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="10dp"
app:cardCornerRadius="20dp"
android:layout_width="120dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:id="#+id/imago"
android:background="#null"
/>
</androidx.cardview.widget.CardView>
In code i just use Img.setImageUri(cursor.getString(6)) in adapter

i suggest to you use ShapeableImageView instead of this way, however if you want use your solution replace this
android:scaleType="centerCrop"
property instead of centerInside

Related

How to resize images to fit in card view properly

Hello I'm trying to resize images to the size of the card view but some images fit properly and some of them don't I want that all the images fit into the card view properly
I have used all scale type but the best I found is center crop but still after using it some images doesn't fit properly
here is the XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_marginStart="1dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="1dp"
app:cardCornerRadius="13dp"
app:cardElevation="1dp"
app:cardMaxElevation="4dp">
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/todo"
android:scaleType="centerCrop"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
Inside card view use:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/todo"
android:scaleType="centerCrop"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
I dont think there's a built-in way to accomplish this.
Also "fit" suggests your target ImageView and the image you want to display on it, have the same width to height ratio. Otherwise, while you can "fit" it, the image will be squished or stretched in one dimension.
Once you confirm that, you can use one of the many image processing libraries to do this.
Popular ones are
Glide
Picasso
Coil
Both Glide and Picasso have very similar APIs, so if you pick one and later want to change to the other, it is very easy.
Checkout the FitCenter transformation for Glide. I think that's what you're looking for, however remember that if your widt to height ratio doesn't match, the image will look weird.
If you want Picasso instead, check this answer.

set imageview in android-studio

Im trying to add 3 imageview with text, but i have problems with the image.
there coming some emty space under and over the image, so the image is been like a square.
And i have problem to change the other image and text.
It is like a layer around the image.
Like this:
This is my code.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/tuna" />
Set adjustViewBounds to true and mess around with the scale types.
This is happening because of your image file. It consists of transparent pixels.
you can either remove those using photoshop if you are adding images manually. or if you are getting that image from the server you try below answer to remove it via code. remove transparent pixels from bitmap
try to adjust the width and height of the image file using photo editor (ex. Adobe photoshop)

XML background image in layout

This is ridiculous, no error, I already did the same on some other projects and works fine but it still shows nothing...
Someone knows why my background image is fine in the editor (in the Android Studio editor it shows the background image), but when I run the app on my device, it shows the only white background with just my 2 buttons...
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ale.mediaspectrumplayer.MainActivity"
android:background="#drawable/background">
<Button
android:id="#+id/ID_MediaPlayer_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="24dp"
android:text="#string/main_MediaPlayer"
app:layout_constraintBottom_toTopOf="#+id/ID_SpectrumView_Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/ID_SpectrumView_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:text="#string/main_SpectrumView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Neither with code it works and I dunno why (layout.setBackgroundResource(R.drawable.background), and neither with image view and scale fitXY...that's absurd
Sometimes, if the image you are using to set as background is of high resolution or is heavy, system just avoids loading it to save the memory. In case you want that image in the background of the whole content, you have to set an image view whose height and width matches that of the parent layout. Then use an image loading library eg : Picasso, Glide etc to efficiently load the high resolution image in that ImageView without burdening the system
Have you tried using the android:scaleType attribute? This can change the position of the background image to cover the screen, sometimes if the image is too small it won't show up. Setting scaleType to center or centerCrop might help.
Here's the documentation: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
try Changing parent layout to RelativeLayout or any other layout.

ImageView with rounded corners via another Image as a background

I have two images - 1)a rectangular one, displaying the actual content and 2)a white image with rounded transparent corners
Is it possible to place image 1 inside image 2, keeping its size but making it to be the same shape as 2?
Basically I want Image 2 be the container of image 1.
I've tried layer and inset drawables but all the time image 1 overlapped image 2.
Thanks in advance!
UPDATE 1:
Here's my ImageView xml part:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/avatar"
android:src="#drawable/mainImg"
android:background="#drawable/backgroundImg"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center"
android:contentDescription="#string/desc" />
</LinearLayout>
UPDATE 2:
Below is the link to three images
1) background
2) main image
3) expected result (with rounded corners)
ImageShack upload
I have had to deal with this issue with an app I recently made. Notice how, in the first and second screenshot, the thumbnails are all framed.
To accomplish this, I'm stacking the image and the frame on top of each other in a FrameLayout. First I layout the actual image (#id/thumbnail), followed by the frame (#id/frame).
Important things to note are that the thumbnail is using a scaleType of "fitXY", and has a slight margin so the corners don't stick out behind the frame's rounded corners.
This really only works if your frame border is opaque, so you may have to make your frame edges the same color as your background.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="#dimen/thumbnail_size"
android:layout_height="#dimen/thumbnail_size"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="4dp"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pixel_frame"
android:scaleType="fitXY" />
</FrameLayout>
One easy solution would be to use just one ImageView with android:background for your image2, which you say is the container, andr android:src for image1, which is the actual image :
<ImageView
...
android:background="#drawable/image2"
android:src="#drawable/image1"
android:padding="2dp" />
Just add a padding, to specify how much empty space you want to leave between the "frame" and your actual "picture".
Use ImageButton.. Set Background as Image1 and Image src as Image2
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image2"
android:src="#drawable/image1" />

Android draws only part of ImageView

I want to draw ImageView with text over a relative layout with 9patch background.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash_last" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/appnameimg" >
</ImageView>
</RelativeLayout>
In editor i have this:
On the phone :
It seems that I have just 3 character from string.
Where is my mistake?
UPD
there background and text image. Can it be because of "bad" 9patch?
link to images
I am able to reproduce the issue. It looks like the way you are trying to define the 9 patch is the issue. You want one corner to be un-stretchable which is not possible. According to this doc, the area to be stretched is intersection of left and top one pixel black lines. Using that definition it is not possible to define corner as un-stretchable. If I remove the 9 patch the both images looks fine.
You need to define how your image will scale inside of the ImageView container.
Set android:scaleType="fitCenter" in your XML to center the image in the list view and fit it inside of the container.
Ex.
<ImageView
android:id="#+id/imageView2"
android:scaleType="fitCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/appnameimg" >
</ImageView>
or one of the other options that might suit you better from here
ImageView.ScaleType

Categories

Resources