Resize image to fit into LinearLayout - android

I need to dynamically add some images to a LinearLayout based on what the user selects but I cannot figure out how to resize the images before adding them. I have tried the setWidth() and setHeight() methods but it seems to do nothing. I would like to set them to a certain dp.
Thanks to anyone looking at this.
ImageView image = new ImageView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setImageResource(R.drawable.icon1);
LinearLayout layout = (LinearLayout) findViewById(R.id.iconLayout);
layout.addView(image, cart.size()-1, params);

problem:
ImageView.ScaleType.CENTER
What it is doing is that it will scale your image but it will only fit on either x or y axis.
from documentation:
Compute a scale that will maintain the original src aspect ratio,
but will also ensure that src fits entirely inside dst. At least
one axis (X or Y) will fit exactly. The result is centered inside dst.
solution:
You can use the ImageView.ScaleType.FILL to scale your image in x and y axis depends on the height and width of your ImageView.
image.setScaleType(ImageView.ScaleType.FILL);

OK. I figured it out thanks to being steered in the correct direction. I was able to use FIT_CENTER and that done it.

Related

Size and position of GLSurfaceView

Actually I have project in which I'm using GLSurfaceView. At this moment this component is placed as main control on whole screen. In this configuration I'm able proper handling drawing functionality.
In next step I want to change size and position of GLSurfaceView. I want to place it in the center of the screen and set width and hight to exact phicical dimmension for example 20mm x 20mm.
Do you have any advices or hints how should I start to introduce this kind of changes to GLSurfaceView?
I want to place it in the center of the screen
You need to set gravity to the parent element like Gravity.CENTER. For example if it's LinearLayout then call linearLayout.setGravity(Gravity.CENTER).
Set width and hight to exact phicical dimmension for example 20mm x
20mm
To calculate size from mm to pixels use:
float mmInPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 20,
getResources().getDisplayMetrics());
And to set size for GLSurfaceView use LayoutParams:
ViewGroup.LayoutParams layoutParams=glSurfaceView.getLayoutParams();
layoutParams.width=mmInPx;
layoutParams.height=mmInPx;
glSurfaceView.setLayoutParams(layoutParams);

Bitmap ImageView, inside a HorizontalScrollView. How to fit the HorizontalScrollView Height with the bitmap?

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?

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.

Using RelativeLayout dynamically and setting margins in px, dp, inch, mm

My application is just a modified ImageViewer with the options of zooming and dragging.
Containing that modified Imageviewer there is a RelativeLayout (that I want to use as an AbsoluteLayout).
Then, new elements can be added to the layout to situate them in certain points of the image. The position (x, y) in pixels of the elements is in a database so I guess there is no other way that adding them programmatically, and not with XML.
Then the position of those elements is updated every time that a dragging or zooming action is performed.
The problem is that somehow the pixels of the image are not matching with the pixels in the RelativeLayout! So the items are "more or less" situated but not in the proper place (the further from (0,0) the bigger is the error).
Things I've tried already:
Try different conversions because maybe the problem is related to the difference of densities between the Bitmap and the Layout:
Resources r = getResources();
float x = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics());
Try to use different methods in the layout to set the margins:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) newPosition.x;
params.topMargin = (int) newPosition.y;
element.setLayoutParams(params);
I also wanted to use the options available for the RelativeLayout in XML but I really cant find it programmatically but I can find the way to specify the dimension, I mean:
android:layout_marginLeft ="50px" // or "50mm", or dp, or inches...
You can set the height, width and the margin by below code:
//headView
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)headView.getLayoutParams();
head_params.setMargins(80, 0, 0, 0); //substitute parameters for left, top, right, bottom
head_params.height = 60; head_params.width = 200;
headView.setLayoutParams(head_params);
Where headView is the view of your app.
Try this: for Relative Layout you can set the margins like this: params.setMargins(left, top, right, bottom);
I've found the answer, the problem was in fact with the density, so I made:
DisplayMetrics metrics = getResources().getDisplayMetrics();
float density = metrics.density;
//And then I add a new element like: realX*density, realY*density
I don't understand so much what it is doing but it works... Actually what I don't understand the value metrics.density
Any explanation is welcome.

dynamically increase the relative layout size in android

i have a relative layout on which i am displaying a page and some content. when i zoom my page...the layout size is not increasing. i want my layout to increase its size dynamically. how do i set it??
i tried doing it in java code.
contentLayout.getLayoutParams().height = x (some value which is equal to the page size)
contentLayout.requestLayout()
but this is nt working. i have also set the layout params android:layout_width and android:layout_height to wrap-content in the xml file.
Kindly, help me out. thank you
You can do it this way. I also added setting of the margins:
RelativeLayout targetItem = (RelativeLayout) findViewById(R.id.RelativeLayout01);
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(mWidth, mHeight);
adaptLayout.setMargins(marginLeft, marginTop, marginRight, marginBottom);
targetItem.setLayoutParams(adaptLayout);
For mWidth and mHeight you also may set dip values to make it adapt to the different screen sizes. Easy way to do that is to define Dimension in your strings.xml and work with them, not with absolute values.
So, if you define relative_width as 120dip, and relative_height as 100 dip then in code you have
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(getResources().getDimension(R.dimen.relative_width), getResources().getDimension(R.dimen.relative_height));

Categories

Resources