Android ImageView, maintain aspect ratio and crop top of image - android

I have spent a very long time researching this issue to no avail. I am attempting to automatically resize an ImageView to match the parent views width (maintaining aspect ratio), while at the same time only allowing the top of the image to be cropped. The left, right, and bottom portions of the image cannot be cut off, so centerCrop does not accomplish what I need.
The top of the image fades into the background color of the app, which is the only part of the image that can be cropped to achieve a proper aspect ratio.
I have also tried using fitXY, however, the aspect ratio is not kept and it looks very bad on some devices.

I have solved this issue. Using the custom ImageView class posted by Mark Martinsson (answer here https://stackoverflow.com/a/17424313/2040690), and setting the ImageView to alignParentBottom, the image will fit perfectly inside the view while maintaining aspect ratio, and the excess portion of the image on the top will simply extend out of the screen boundary, essentially "cropping" it.

Using Glide, I created a transformation to simply make the bitmap square
Glide.with(view.getContext()).load(someUrl))
.transform(new BitmapTransformation(view.getContext()) {
#Override
protected Bitmap transform(BitmapPool pool,
Bitmap toTransform, int outWidth,
int outHeight) {
return Bitmap.createBitmap(toTransform, 0, 0,
toTransform.getWidth(), toTransform.getWidth());
}
#Override
public String getId() {
return "some_unique_id";
}
}).into(imageView);

Related

How to crop visible part of a bitmap in an ImageView if it is scaled

Assume I have an image which is larger than an ImageView size (width and height) and the scaleType is set to CENTER or the scaleType is set to MATRIX and the bitmap is translated and scaled. Therefore parts of the image are not visible. How may I calculate the visible part of the bitmap and crop exact that part. I want to crop and store only the visible part.
How may I do that?
Any help will be appreciated.
So you want exactly what the image view shows? I'd enable the drawing cache and grab it from the view. That should be exactly what's on screen.

Getting the top padding of a scaled bitmap in an imageview that maintains aspect ratio

I have a bitmap that is being rendered inside of an imageview.
The scaling is set so the bitmap maintains it's aspect ratio. As such, there is padding on the top and bottom relative to the imageview.
What I'd like to do is get that padding. I'm assuming this is possible no?

How to get width of what image really occupies on the ImageView android

I have an activity which only has an ImageView on it. I'm setting an image bitmap for this view that is much taller than wider. So, for example, my activity has 720x950 and my image is 918x2077.
When I add the image in the imageView, both side parts of imageView stays white (read: no image in there).
If I call mImage.getWidth() it returns me the width of the entire imageView of 720 (which is the activity wide).
I would like to get the width of the image in specific, not of the view.. is that possible?
It sounds like the aspect ratio of your image is being kept. You want to know how much width the image is actually occupying.
You can calculate it!
if your bitmap is 918x2077 and your window size is 720x950, then to calculate your width, simply do:
(950/2077) * 918 = 419.8844...
Hope this helps :)

scaling/formatting multiple imagebutton array in Android

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.

Image doesnt cover image view fully

I am trying to apply image on an image view instance...but it doesnt cover it properly...
please advise
here it is my image view code:
android:id="#+id/imageViewVessel"
android:layout_width="fill_parent"
android:scaleType="fitStart"
android:layout_height="170dip"
android:src="#drawable/vessel"
EDIT by kcoppock: Adding code from devaditya's comment below
TableRow rowImage = new TableRow(this);
rowImage.setBackgroundColor(Color.GRAY);
rowImage.setMinimumHeight(150);
rowImage.setGravity(Gravity.CENTER);
rowImage.setMinimumWidth(LayoutParams.FILL_PARENT);
ImageView imgViewVessel=new ImageView(this);
imgViewVessel.setImageResource(R.drawable.vessel);
imgViewVessel.setMinimumHeight(150);
imgViewVessel.setMinimumWidth(LayoutParams.FILL_PARENT);
imgViewVessel.setScaleType(ScaleType.FIT_XY);
rowImage.addView(imgViewVessel);
To expand on Gao's answer, you do need to set a scaleType for your ImageView, but it is unlikely that fitXY is the scaleType that you are looking for. You can find the complete list at the above link, but a few of the most common are:
centerCrop: This will maintain the aspect ratio of the image, filling the frame entirely, but cropping off either the left and right, or top and bottom of the if the aspect ratio of the frame and source image are different.
centerInside: This also maintains the aspect ratio, but the image is scaled to fit entirely within the view, so that the longest edge is the same size as the frame. This can give you a letterbox type of effect if the aspect ratios of the frame and source image are different. fitStart and fitEnd are the same scaling method, but they have different placement of the image (top-left and bottom-right, respectively).
fitXY: This one should only be used if disproportionate scaling does not affect the graphic. In the case of bitmap graphics, this is almost always bad to use. This sets the width of the source image to the width of the view, and the height of the source image to the height of the view, without maintaining the aspect ratio of the source image.
You can set scale type in the layout file : android:scaleType="fitXY" or call setScaleType with fitXY.

Categories

Resources