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.
Related
As seen in this image:
I have a rounded rectangle that is the size of 1082 x 1796, and I used getWindowManager().getDefaultDisplay() to get and print the pixel values of the screen as seen in the circle bottom left. However, when I draw this bitmap, this happens:
Is the screen size different from the getDefaultDisplay() method? How do I fix this?
I think your shape is seen by the system as a 9patch. It use the first and last lines/columns in order to know how to resize the image in order to display it consistently.
That's why there is a 2px difference in width and height. I think you can correct it by modifying the png and removing the first and last lines/columns, or just use another way to draw the image. Maybe using another format (jpg ? bmp ?) for the image could do the job. I'm not sure.
you can do that with calculation to fit all screen devices based on width and height of the device screen:
DisplayMetrics dm = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * imageHeight / imageWidth;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
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.
My relative layout is named "highlight".
public static void selectText(float left, float right, float top, float bottom) {
highlight.getLayoutParams().width =(int) (right-left);
highlight.getLayoutParams().height=(int) (bottom - top);
highlight.setX(left);
highlight.setY(top);
}
This works great for highlighting text as far as setting the top left corner of the highlight box. But, the box expands all the way to the bottom right corner of the screen, no matter how small I make the .width and .height values.
You set your width and height as wrap_content. Your layout will have the size of it's content.
Instead of:
highlight.getLayoutParams().width =(int) (right-left);
highlight.getLayoutParams().height=(int) (bottom - top);
Try:
highlight.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Check this link:
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
I don't know why, but the simple 'get layout params' wasn't working. I needed to make a new layout params entirely.
int width = (int) (right-left);
int height = (int) (bottom - top);
RelativeLayout.LayoutParams rlMainlayoutParams = new RelativeLayout.LayoutParams((int) (right-left), (int) (bottom - top));
highlight.setLayoutParams(rlMainlayoutParams);
highlight.setX(left);
highlight.setY(top);
The above answer doesn't answer my question, perhaps because I was not clear that this relative layout doesn't hold any text, it only highlights certain text already present on the screen (in a PDF document, where I cannot necessarily just extract the text to a PDF document).
After you've modified a view's layout params, call requestLayout() on the view for the changes to take effect.
(Calling setLayoutParams() also implicity calls requestLayout().)
I have an Activity with relative layout on whole screen. In left top corner I have button. And on button click I want move layout from 0 X coord to 100 X coord. I tried something like this in onclick method:
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
param.leftMargin = 100;
meRelativeLayout.setLayoutParams(param);
But this thing just made my relative layout width smaller, its change left margine and thats it, but I don't want to change layout width, just want to change X coord, is there any way to do that?
To me your code seems to be perfect, only the matter is of FILL_PARENT. As you used FILL_PARENT then the system will assign the space left in the display(less margin) thats why you layout became small, So Try this.
int width= mActivity.getWindowManager().getDefaultDisplay().getWidth();
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams( width, LayoutParams.FILL_PARENT);
param.leftMargin = 100;
meRelativeLayout.setLayoutParams(param);
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));