I'm working with an ImageView and I want to resize it programmatically passing (for example) from a full-screen ImageView to a 50x50. Is there a way to do that?
Different from the one you suggested me to see because I don't need to fit the image in the ImageView but to resize the dimension of the ImageView.
My suggestion use a relative layout and put image inside that has with weight and able to auto resize which depends on the weight. The best way to auto resize even your screen rotates.
I didn't remember which one layout has weight format
You can use LayoutParams to set height width programmatically -
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
imageView.setLayoutParams(layoutParams);
ConstraintLayout provides a mechanism for fixing the aspect ratio of a child view. Select the child view that we want to control, and then set the ratio value, we can manually put the ratio that we want to set.
Using constraintLayout makes UI shrink or expand according to screen size without distorting the UI. This is the most important advantage of using ConstraintLayout in UI.
Related
I have a bunch of images with different sizes. Each of them should be presented on the top part of the screen and must take the space in height equal to 60% of screen height. Width of the image will be dependent on it's height to save initial proportions. I tried to use weightsum property in layout and weight property in ImageView, but I don't know what to put in the height property of my image view. If it is "wrap_content", every image resizes my ImageView and all mark-up crushes.
Any advices?
If you want, you can set the image's dimensions by code.
Just set the width with the weight_sum method and then do something like:
WindowManager manager = (WindowManager) context.getSystemService(Activity.WINDOW_SERVICE);
int screenHeight = manager.getDefaultDisplay().getHeight();
YOUR_VIEW.getLayoutParams().height = (int) (screenWidth * 0.6);
Please mind that you can do so only AFTER your ImageView has been drawn on screen - so calling it within the onCreate() method will not work.
You can either call it delayed (postDelayed) or set a layout listener to one of your view.
Hope this helps.
I want to resize the image of the CheckBox programmatically, but i want to use the original images of the phone. I just want to scale it.
I don't wand to supply own images, because i want to keep the phone's look, and i would still have to scale them.
I already tried to get the systems image resources using
Setting Android CheckBox to a different image... and then back to the original images
but i still could not scale the drawable. (I tried drawable.setBounds()).
I'll probably also have this issue with radio buttons.
Thanks for your help!
you should just be able to set the layout parameters on the checkBox:
LayoutParams lp = findViewById(R.id.chk_id).getLayoutParams();
lp.width = width;
lp.height=height;
findViewById(R.id.chk_id).setLayoutParams(lp);
By getting the layoutparams before you presever any other layout setting associated with the view
width and height are in pixels, to scale it for density use:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = (int)(150*dm.density);
you need to use the Container class for the Layout params this example is for a FrameLayout but just replace that with tyhe type of the parent container.
I'm trying to make a LinearLayout that has height = LayoutParams.WRAP_CONTENT.
I am receiving an arbitrarily sized bitmap from a server to set as the background image for the LinearLayout.
How do I set the background image of the LinearLayout without resizing the view if the received bitmap is larger than the contents of the linear layout?
It would be nice if the background image could maintain its aspect ratio, and scale to match the width of the screen.
I tried overriding onMeasure as a temporary solution, but that just wound up biting me.
how about using an imageView inside your layout , which also has its adjustViewBounds set to true in order to keep aspect ratio?
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.
Is there a way to proportionally set the height of an ImageView if the width is set to android:layout_width="fill_parent"?
It appears there is no direct way of adjusting an ImageView without skewing one of it's dimensions. This seems bizarre given the application is expected to support a slew of screen sizes and resolutions.
You can use wrap_content for the height and set the correct scale type (either in code or via the android:scaleType attribute in XML). You are probably looking for CENTER_INSIDE/android:scaleType="centerInside".
See ImageView.ScaleType