Scale an image in Kotlin Android Studio - android

I'm new to Kotlin and Java and don't know how to scale an image in an image view.
I want something like this: MyImageView.setHeight(300)
I don't want to distort the image.
Thanks in advance!

This solution might be overkill for your current question, but if you have to show images in your app, I recommend using an image handling library such as Picasso or Glide.
If you are to use Picasso, add it to your dependencies by adding the following line to your gradle files :
dependencies {
implementation 'com.squareup.picasso:picasso:2.71828'
}
Then in your Kotlin file, when you are about to load the picture onto an ImageView,
do it using Picasso using the following lines :
Picasso
.get()
.load(imageUrl)
.fit()
.centerInside()
.into(imageView)

Yes, I agree with rgv. Use an image handling library, preferably Picasso. It can reduce a lot of hardwork and also, the result is better than what you would have got by scaling image manually.

Related

Picasso with recyclerview auto rotates some images

I'm having issues with Picasso displaying some pictures in the wrong orientation and others in the right one. I have this one rotated: https://imgur.com/BBSKFJm . I've seen threads like this one: Picasso displays in wrong orientation and this one: Why image auto rotate when set to Imageview with Picasso where it's recommended to either use Compressor from https://github.com/zetbaitsu/Compressor or to manually rotate it.
I was hoping for one that just undoes the weird rotation for a standard recycler view carousel experience pulling from uris. I'd also be open to techniques to keep it uniform such as to crop the images in a recyclerview to deal with the potential issue of the proportions causing this: Android ImageView Displaying Rotated Images Although Source Is Not Rotated.
I faced a problem like this, and I solved it with the Glide library.
dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
and just
GlideApp.with(context)
.load("http://via.url.com/300.png")
.placeholder(R.drawable.placeholder)
.error(R.drawable.imagenotfound)
.into(ivImg);

Xamarin android Glide webp animation

I'm using this library (Xamarin binding library of Glide), to display Gif in my app. But i would like to display Webp animations to save some space in my app.
Glide.With(context)
.Load(url)
.Into(imageView);
But the animation is locked on the first frame and didn't played (webp animation example).
Did i miss something?
Thank you.
Regards, Samih
It's a known issue.
Right now, Glide has no built-in webp support and defaults to the system.It seems like Glide will work when you introduce some libraries that support WebP.

Picasso doesn't load view in MainActivity

I'm doing a basic Android Studio Project for loading a URL into an ImageView using Picasso with Kotlin. I have followed every step from the official webpage of Picasso, but when I run my app the emulator shows an empty view.
In my Gradle I added the implementation of Picasso:
implementation 'com.squareup.picasso:picasso:2.71828'
And Internet permission within manifest tag too:
<uses-permission android:name="android.permission.INTERNET"/>
And in MainActivity the basic use of Picasso:
Picasso.get().load("http://paproject.online/hp.jpg").into(imageTest)
imageTest is the id of a Imageview with layout_height = 200dp and layout_weight = 200dp.
Maybe Your Photo size is too big so in ImageView tag in XML Edit layout_width and layout_height to Const Size Like 100dp.
and If U can Change Picasso with Glide. maybe Can help you
Your image is larger (1024x768) than your ImageView, but of course that should not be a reason for you to get blank. Try as below which also includes local image resource nopic for the error cases. Seems you have the correct include
// Lazy load the image with Picasso
get()
.load(yourURL)
.placeholder(R.drawable.nopic)
.error(R.drawable.nopic)
.into(img);
You can resize your image before including it into imageView
Picasso
.with(context)
.load("http://paproject.online/hp.jpg")
.resize(200, 200) // resizes the image to these dimensions.
.into(imageViewResize);
Or you can use any cropping technique like .CenterCrop().
You can read more about this https://futurestud.io/tutorials/picasso-image-resizing-scaling-and-fit
Ok, the problem is already solve. It seems that there is a problem loading images in Android 9.0. So the solution is in this link: Picasso image loading issue with Android 9.0 Pie.
Thanks to everyone for your responses.

How to make move and scale activity

I want the user to upload his profile picture but I want to force him to crop it before .
How can I do that ?
I try to do it from scratch but it will take a lot of time i guess , Is there any library can do the job for me ?
There are a few way to do, First and suggested way use library or handle it with android intent
com.android.camera.action.CROP
at your own
I'v use this library for a while and it's great
https://github.com/ArthurHub/Android-Image-Cropper
You can use Picasso library. It is very easy to use.
For example:
Picasso.with(mContext)
.load(url)
.centerCrop()
.resize(yourImageView.getMeasuredWidth(),yourImageView.getMeasuredHeight())
.error(R.drawable.error)
.placeholder(R.drawable.blank_img)
.into(yourImageView);

Picasso not loading image for some url

I have an image that I want to download from online.
http://luxproperty.kaytami.com/platform/media/image/jpeg/2014/12/24 Repulse Bay Road.jpg
I have replaced the space with %20, so it becomes
http://luxproperty.kaytami.com/platform/media/image/jpeg/2014/12/24%20Repulse%20Bay%20Road.jpg
The image is not large and so I assume Picasso should be able to load it.
To fit my imageview, I have fit() the image, and the code is as follow:
Picasso.with(mContext).load(UrlEncoder.encode(district.getImage_urls().get(0))).fit().centerCrop().into(holder.image);
However, the image does not appear.
There is a list of 4 items, each containing an image that I load from online. three of them were loaded properly, and the remaining one (http://luxproperty.kaytami.com/platform/media/image/jpeg/2014/12/24%20Repulse%20Bay%20Road.jpg) just does not show up.
Any idea?
I am using Picasso 2.4.0, okhttp-2.1.0 have a look this
It's a bug that is reported to be fixed in the next release of the lib.
You can clone the repo of the lib and compile your own jar or wait.
I recommend you to take a look at Glide. Migrating from Picasso is quite trivial, it has better performance and it makes a nice smooth scrolling on lists.
I had the same problemm when i tried to use Picasso to load images but i used this Library
Its the simplest thing so far by me.
Compile:
compile 'com.koushikdutta.ion:ion:2.+'
Then:
//for activity
ImageView myImage = (ImageView)findViewById(R.id.my_image);
//for fragment
ImageView myImage = (ImageView)rootView.findViewById(R.id.my_image);
Ion.with(myImage)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.load("http://example.com/image.png");
Hope it helps!!!

Categories

Resources