How to fit image fully on screen? I need to have WRAP_CONTENT, not MATCH_PARENT
im.setImageResource(resource);
im.setAdjustViewBounds(true);
im.setScaleType(ImageView.ScaleType.CENTER_CROP);
im.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
This codes places the image on upper part so bottom is grey.
Remove
im.setAdjustViewBounds(true);
It adjusts bounds to preserve the aspect ratio the drawable are you displaying.
Related
In my App. I have a RelativeLayout containing an ImageView.
The RelativeLayout is set to MATCH_PARENT horizontally and WRAP_CONTENT vertically.
The ImageView is set to WRAP_CONTENT both horizontally and vertically. The image Drawable is a 24x24 Vector icon.
Because the icon is too small to be seen, I want to double its size. That's where the issue is:
// Parent RelativeLayout
RelativeLayout titleLayout = new RelativeLayout(MyApplication.getContext());
titleLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//Icon
RelativeLayout.LayoutParams information_icon_layout = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
information_icon_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView information_icon = new ImageView(MyApplication.getContext());
information_icon.setLayoutParams(information_icon_layout);
information_icon.setPadding(
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_left),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_top),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_right),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_bottom)
);
information_icon.setImageDrawable(MyApplication.getContext().getResources().getDrawable(R.drawable.ic_info_outline_black_24dp));
information_icon.setScaleX(2);
information_icon.setScaleY(2);
information_icon.setAdjustViewBounds(true);
The icon scales as expected, but the parent RelativeLayout does not seem to take into account this change, and still calculates its vertical size using the original size of the Icon (24x24).
As a result, the icon is scaling outside of the parent, and only part of it is visible.
My question: How can I tell the parent RelativeLayout to take into account the scaled size of the icon?
Use scaletype property of an imageview it has main 3 property
center,
centercrop,
centerinside,
And if you want to scratch the image fully it also has property as fit center,fit end,fit start,fitxy.You can use this property in xml as well as java.
here is the link
scaletyper
I have a picture of fixed height (60 px by 40 px).
I am trying to position the image by programmatically by adding margins to the image so that the image aligns to the center of the view
How I can get it done correctly to work for all different size and devices?
Make the image have a width and height of wrap content and a layout_gravity of centerHorizontal
You can make image params to match parent and then add margins of your choice.
ImageView image = new ImageView(this);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.setMargins(left, top, right, bottom);
image.setLayoutParams(params);
Welcome all.
i have a LinearLayout that must show a ImageView. The imageView must fit all the width of the LinearLayout, so i used match_parent for the width, and it must keep the aspect ratio, so i used wrap_content for the height and setAdjustViewBounds to true. But something is not working because the width of the ImageView is correct (it fits the width of the linear layout) but the height of the imageView is wrong (the image is being cutted in the upper and the lower part)
I tryed with all the scale types (fit_center, fit_xy, center_crop, center_inside etc...) and i have problems with all of them (for example, if i use fit_center, the image is does not fit the full width of the ImageView, i can see two black spaces on left and right of the image, also i tried using FIT_CENTER without setAdjustViewBounds and i have the same problem)
HERE YOU HAVE AN SNAPSHOT: http://i.stack.imgur.com/8hrKF.png
this is my code:
LinearLayout onlineHolder = this.holders.get(i);
onlineHolder.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
onlineHolder.removeAllViews();
ImageView imgv = new ImageView(onlineHolder.getContext());
imgv.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
imgv.setAdjustViewBounds(true);
imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap b=res.getCachedBitmapWithWidth(this.width); //obtenemos el recurso cacheado con el width correspondiente
imageElement.setBitmap(b);
imgv.setImageBitmap(b);
onlineHolder.addView(imgv);
Thanks
EDIT: it only works if i specify the width and the height of the imageview with absolute numbers. For example, if i put 400 (400 is the 50% of the screen, the layout width) width and 583 height, it works, but if i put MATCH_PARENT for width and WRAP CONTENT for height... it not works... why?
I have this one in XML and it works in the way you want. take a look.
<ImageView
android:id="#+id/attachedImage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
But if you want to do it on the runtime, I suggest to create an XML this way and inflate the View on the runTime and add it to your parent layout. That will work for sure.
EDIT : How to inflate the ImageView.
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.your_layout_contains_image_view, null, false);
ImageView imageView = view.findViewById(R.id.attachedImage);
I have a Horizontal Scroll View, and inside it i have a ImageView. This imageview has a Bitmap with high width and low height.
I want the bitmap to fill the height of the horizontal scrollview, and I want to scroll the bitmap from left to right.
I tried to use this code, but it doesn't work. It creates two spaces on left and right, these spaces are black. These spaces are from the ImageView... so... the bitmap has less width than the ImageView
HorizontalScrollView wvScroll = new HorizontalScrollView(this);
wvScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
iv.setImageBitmap(Util.getRemoteImage("http://mywebsite.com/90.gif"));
//iv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
iv.setScaleType(ScaleType.CENTER_INSIDE);
iv.setBackgroundColor(Color.BLACK);
wvScroll.addView(iv);
wvScroll.setBackgroundColor(Color.GREEN);
mainLayout.addView(wvScroll);
what am i doing wrong? I also tryed with iv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); but it doesn't works, Same result.
EDIT:
I also tryed with ScaleType.CENTER_CROP and it doesn't works fine. The bitmap has higher height than the ImageView, because some pixels lost on top and bottom of the bitmap.
If I had to approach this in the simplest way. I'd set the width of the scroll view the same as the screen width. The height doesn't matter as much because you're assuming the height will fit in the screen(At least that's what I gather from your question).
From what I remember scroll-views will scroll anything bigger than than the actual screen or anything that is bigger than the specified scroll-width or scroll-height its given.
This might help you get started with getting the respectable screen sizes - Get Screen width and height
Of course since different devices have different screen sizes - you'd have to do this "pro-grammatically".
Would this sort of implementation work for you?
I have a rectangular image in an image view. I want to fit the image in the screen for different screen sizes while maintaining the aspect ratio.
In smaller screen its working fine, but its not getting stretched in biggerscreens. Some gap remains in the bottom of the image.
This is my code:
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
LayoutParams params1 = new LayoutParam(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mMainLayout = new LinearLayout(context);
mMainLayout.setLayoutParams(params);
backgroundImage = new ImageView(context);
backgroundImage.setId(0);
backgroundImage.setAdjustViewBounds(true);
backgroundImage.setScaleType(ScaleType.FIT_CENTER);
backgroundImage.setLayoutParams(params1);
backgroundImage.setImageResource(R.drawable.background_circles_en);
mMainLayout.addView(backgroundImage);
I have used many combinations of fill_parent, wrap_content with multiple scaleTypes: fitCenter, centerInsideand they all draw the images in the right aspect ratio, but none of them actually scale the images up and the ImageView itself, resulting in either the TextViews get pushed all the way down off the screen, blank spaces inside the ImageView, or image not scaled.
Please give a right combination so that it will work properly for different screen sizes.
Privide ScaleType FIT_XY, it will scale image to x and y dimensions, irrespective of aspect ratio.