Im trying to change an image lager than the screen so that the image partly(boundary parts) gets out of the screen. The purpose Im doing this is to make the whole screen filled with the image. I tried simply changing the height and width bigger than the height and width of screen which I got but it didn't work well. I used code below to get the size of the screen.
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
How should I do for this?
In the xml match the height and width of the image to parent and change the scaleType attribute to centerCrop -> android:scaleType="centerCrop"
If you want to fill the image to entire screen like a zoomed view, I think you can try image loading libraries like Picasso or Glide to crop the image to fit it inside the layout and also it will handle the caching for large sized images.
For Picasso you can try something like this
Picasso.with(this).load(R.drawable.image).centerCrop().into(imageView);
Related
I need to load some images from URI to Bitmap variables and perform some operation with them togheter. I need the bitmaps to be squared images with fixes size, scaled down and cropped. By now I use this code:
return Picasso.with(c).load(imageUri).resize(size, size).get();
but, obviously, the image will be resized without keep its aspect ratio.
I want to resize the image with these requirements:
the smaller dimension (width or height) should be equals to size
the greater dimension should be cropped to size, keep image centered
The key is using centerInside after resize. See link
Picasso.with(c).load(imageUri).resize(size, size).centerInside().get()
set your imageview height and width fix inside xml and then set image to imageview like
Picasso.with(YourActivityName.this)
.load(imageUri)
.into(imageview_id);
So I have this task to create a horizontal scrolling array of image buttons that are basically photo avatars of users. These avatars aren't constrained by aspect ratio or size, and so I've been playing with ways to scale them and format them. I've gotten them scaling via the scaletype="fitCenter" and using static width and height. But what I really want them to do is to butt up against one another. Currently if an image is taller than it is high, you get the kind of letterboxing but on the sides vs. the top (blank areas). I've tried all the different scaling values, wrapping each imagemap within a linearlayout, etc., but nothing I try seems to get rid of those (while displaying the entire image to scale). Is there any way to do this?
Just to reiterate what I think you're doing, you have three image scenarios:
Square image
Landscape image (wider than tall)
Portrait image (taller than wide)
Laying out a row of fixed-size ImageViews (or ImageButtons) using FIT_CENTER works great for what you need if all the images were either square or landscape, because the scaling will always make the image stretch to the horizontal bounds of the view (the largest dimension). However, with portrait images, the scaling causes the view to be inside the bounds of your fixed-size view so that the entire image height can be visible.
If you need to maintain the aspect ratio of the image, there really is no ScaleType to help with this because the logic would be circular (fit the view to the image, while simultaneously fitting the image to the view). The solution is to adjust the size (specifically, the width) of each ImageView to match what the image will be scaled to. Here's a sample of a factory method you might use to generate the ImageView to fit the image you want to put inside it. You could also modify this slightly to reset parameters on an existing ImageView if you like:
private ImageView getImageViewForThumbnail(Bitmap thumbnail) {
float viewHeight = //Your chosen fixed view height
float scale = ((float)thumbnail.getHeight()) / viewHeight;
float viewWidth = thumbnail.getHeight() / scale;
ImageView view = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)viewWidth, (int)viewHeight);
view.setLayoutParams(params);
view.setScaleType(ScaleType.FIT_XY);
view.setImageBitmap(thumbnail);
return view;
}
You're basically just calculating what the aspect width of the ImageView should be to match the fixed height you've chosen for all of them.
HTH
Use the scaleType fitXY, it stretches the image to the layout params you assigned, if the image has less dimensions and also shrinks the image to the layout params you assigned, if the image is large. The key point is to mention the image layout params to the imageView , that is the width and height of the image.
I'd like to scale an image up to take up the entire size of an ImageView. This is subtly different than using scaleType=fit_center because fit_center will leave bands around the image if the image aspect ratio does not exactly match the ImageView's aspect ratio. Instead, I would like the image to get centered and scaled up to completely fill the enclosing view, with any excess chopped off.
I'm able to accomplish this by computing my own custom image matrix during onCreate():
final Display display = getWindowManager().getDefaultDisplay();
final float screenWidth = display.getWidth();
final float screenHeight = display.getHeight();
final float imageWidth = splashView.getDrawable().getIntrinsicWidth();
final float imageHeight = splashView.getDrawable().getIntrinsicHeight();
final Matrix splashMatrix = new Matrix();
final float scale = Math.max(screenHeight/imageHeight,screenWidth/imageWidth);
splashMatrix.postScale( scale, scale );
splashView.setImageMatrix(splashMatrix);
This works fine, but it seems like there must be an easier away. Does anyone know of a way to scale up an image in an ImageView while both preserving the aspect ratio of the image and fully filling in the ImageView?
(Note: in my case my ImageView is taking up the full screen, so I use getWindowManager().getDisplay() to find the desired image size. You can't use splashView.getWidth()/getHeight() because the view hasn't been laid out yet and doesn't have a size)
You can use android:scaleType="centerCrop".
Keeps the aspect ratio and scales the image just like you want it.
For more information please go through the below link
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
In some cases all you need is
android:adjustViewBounds="true"
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
This worked for me to fit the image inside the whole image view, plus it make sense that it says "ScaleType.FIT_XY" to fit the X and Y axis of the imageView.
From the xml file it would be:
android:scaleType="fitXY"
Try scaleType attribute for your ImageView. http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
I am very late but hope it helps in the future. Here is my solution to how to stretch the image to full screen or image view without scaleType="fitXY" as it causes damage to the image. Use the following library.
[A simple imageview which scales the width or height aspect with the given ratio]
https://github.com/santalu/aspect-ratio-imageview
To Find the aspect ratio of the device screen use the following Code.
DisplayMetrics metrics =this.getResources().getDisplayMetrics();
float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels);
you must set android:scaleType="centerCrop" to fill entire screen
but you noticed that scaletype property worked together with android:layout_width and android:layout_height property.
you must set to match_parent
I need something like<img width="100%" /> for Android <ImageView>. I mean resize width to all available space (shrink or enlarge width of image) and automatically change height to keep aspect ratio.
Something like <ImageView android:layout_width="match_parent" android:layout_height="auto">
Useandroid:scaleType="fitCenter"do with image what I want, but only with image, doesn't change height of View itself. When I set booth dimensions to match_parent I will get what I want, but only if I have only one image on the screen. But I need some texts under image.
It's only way create child of ImageView?
For exampleandroid:adjustViewBounds="true"does't do anything for me.
I found where is problem. Default implementation of ImageView can't enlarge image. If image is bigger than screen width android:adjustViewBounds="true" works correctly and adjust height to keep aspect ratio of image. But if image is smaller that screen width it don't upscale it.
This is cause by this code
if (newHeight <= heightSize) {
heightSize = newHeight;
}
in ImageView class (on line 688, API 10)
I made special child of ImageView which allow enlarge image to fit width.
Here is: https://gist.github.com/tprochazka/5486822
I have one question. I want to set one image to image button. I have one button image of size 90 x 48. I want to set this image on Image button.
Problems:
I need to set image on background parameter or src parameter?
If i set image on background parameter its not showing its actual size(smaller than actual size) if i have set the Layout width = wrap content and Layout Height= wrap content. If i give Layout width = 98dip and Layout Height= 48dip then its showing the same size. Is it a coorect way?
thanks
set image as background Parameter sometimes src makes problem
if you are set width and height as wrap_content then android will check the screen size and try to adjust to your screen if screen size is big then android displays your image as its actual height and width and if screen size is less then it will compress your image
so best way is to set height and width is as wrap_content if you have to use portrait and landscape mode both
Yes.
If you use Layout width = wrap content and Layout Height= wrap content, image will be as actual size. Maybe just the density of your monitor is less than the density of your phone?
Remember dip != pixels.