I’m trying to calculate the width and height of child Layouts/Views based on the dimensions
of the ViewGroup that contains them.
I’m trying to factor in each childs’s margin value, but cannot seem to get this quite right.
I’ve tried every conversion under the sun (dip, sp, px, in, mm), but still come to the same conclusion, which is that margin values get converted to a much smaller pixel value than any of the other dimensional values
(ie, width & height).
I need this conversion factor. I’ve tried many things to calculate it (Context.Resources.DisplayMetrics - Density, DensityDpi, ScaledDensity), but haven’t been successful.
I don't believe that this conversion factor can be taken or derived from any values available at runtime,
and so was wondering if maybe someone knew what it was (or maybe where to find or calculate it).
To give some context, I'm implementing a custom calendar which contains a 5x7 grid of child-layout's (LinearLists), where each child has margins surrounding them.
The outer-most view is a RelativeLayout, which has a width and height of W and H respectively, and the margin width is M. Therefore, you'd think that
CELL_WIDTH = W/7 - M*7
CELL_HEIGHT = H/5 - M*5
However the margin width factors M*7 and M*5 end up being much too large, causing the overall grid to be smaller than the parent view (which is a problem, b/c I need to fill it).
I've tried a ton of different conversion techniques from posts here and elsewhere with no success. I'm convinced that there is some conversion factor that android uses to internally store the margin values (which I'm assuming is in pixels). It is this conversion factor I'm after (unless of course someone knows of a better/correct way to perform the calculation that I'm attempting here).
Here are the links I've tried, and I even converted one of the Java solutions to C# and am currently using it (though it did not solve this particular problem for me, it helped to shed a lot of light on it, and is helping elsewhere):
How to parse a dimension string and convert it to a dimension value
Oh my God I'm an idiot!
I've been tooling around for so long that I wasn't even considering the equation as being flawed.
For each cell size, I should only shave off 1 margin width/height, not all of them, which the current equation is doing.
So, the correct equations are:
CELL_WIDTH = W/7 - M
CELL_HEIGHT = H/5 - M
Ugh, I feel silly.
Related
I want to create a plot that dynamically displays active elements as rectangles. I have achieved a first version that is actually ok using OxyPlot.Annotations.RectangleAnnotation which I add to myPlotModel.Annotations, you can see it in the image hereafter:
Example of wanted display
The thing is that after a while, the amount of drawn rectangles make the update not smooth as I update the shown timewindow (which is set to 15 seconds). I have already set a maximum of drawn elements that suffice to cover the displayed window (i.e. the rectangles get removed as they are too far in the past), but the rendering is still jerky. I draw the rectangles by allocating them to an equal fraction of the Y-axis, that is the third one from the top gets:
rowNumber= 3.0
minimumY = maximalY - maximalY / totalElements * rowNumber
maximumY = maximalY - maximalY / totalElements * (rowNumber + 1.0)
And the Y-axis is hidden.
My question:
Is there a smarter way of creating such a display that would be less computationally heavy, and therefore allow a smoother update? I do not have to stick to OxyPlot, it is simply the easiest way that I found to obtain what I wanted.
Thanks for your answers!
Technically, the answer to your question is "Yes".
There are a number of ways to do this.
You could have a vertical itemscontrol that had an itemscontrol in it's template. That could have a canvas as it's itemspresenter and you could bind canvas.top and canvas.left to properties in it's content. Template each into a rectangle and bind height and width.
And of course do something about the scale on the bottom and the column of activity labels or whatever you want to call them there.
Unless you're using an absolutely ancient machine, that'd just fly.
It's quite a lot of work but it would probably be quicker to write that than to search through a load of alternative packages and decide which was optimal.
I generate a HTML-Page from C#. In the HTML-Page there are a lot of elements. All of them have a absolute position.
One of these elements is a table. This table represents a object that keeps a double[]. Every double value is a new cell in a new row.
I iterate over double[] and create my table:
for (int i = 0; i < dbl.Length; i++)
{
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
htmlTextWriter.Write(dbl[i]);
htmlTextWriter.RenderEndTag(); // td
htmlTextWriter.RenderEndTag(); // tr
}
If the table has so much elements, that it cross an element that is below this table, I have to be responsive to this issue.
This means, I need to know how many pixels this table is long.
Of course I do know how many cells I generate and I also know BorderSize, Padding, Margin, etc.
But there are two problems. First although I know FontSize, FontFamily, FontWeight, I do not know how to include these information into a mathematical calculation.
Second I think in every browser the actual size is also different. I created a dummy table and recognized, that in one screen height I already have one cell difference between Opera and Firefox.
So I think in C# I am only able to approximate the actual height?!
The next idea I have is to include a JavaScript into my HTML. I've no experience with JavaScript, but my approach would be to find my tables and read out size. Then iterate over all elements and find all overlappings.
My questions are:
Are my consideration true or do I miss some aspects?
Are my approaches the right way (in C# I only will get an
approximated result?!, JavaScript I do not know if it is really
possible what I want to do)
Are there other possibilities I do not see right now?
Hint: Other script languages than JavaSript are not applicable for my solution. JavaScript I only use if really necessary.
I think javascript is the answer, not c#.
http://api.jquery.com/height/
You cannot calculate the height in C#. Please use javascript to do this.
Basically I'm reading vehicle license plates using tessract OCR, however despite being able to emphasise text easily enough via changing contrast, reducing noise an so on, some 'parts' of the vehicle remain on the image which does cause the OCR to throw bad results.
For example take:
I can change this easily enough, such as:
I'm looking to eliminate the edges off each plate, here's another example:
I could remove the edges using pixel manipulation algorithm, however I don't feel it's the right method, and would cause quite a lot of problems.
I've been using the following application to test various methods such as morphology and eliminating the unwanted data, so far I haven't been successful.
http://www.codeproject.com/KB/GDI-plus/Image_Processing_Lab.aspx
However someone with knowledge of this could use the application on the article above to achieve want I'm trying, so feel free to give it a try.
Thanks
Please try to use stroke width transformation concept.
This concept use to segment text from natural images.....
I already did such an algorithm. I just can say that it works great. The secret is, that you need to know that the light is coming just from one side perhaps. You cannot set the image to "black/white" just by using ONE threshold.
Detect the average luminance of parts of the image and use this luminance calculation to set the threshold for each region.
For example, if the left top is lighter, you need a lower threshold to make these parts not to bright. And if the bottom right has low light, you need to set the threshold higher to receive all existing light information.
Then, you need just to drive into the image from each side by using the method:
IsPixelAboveThreshold ?
If it is below, you are on the border, if it is above, you can say you are on the middle of the image with more brightness.
Regards
I have a bar graph displaying a number of different series (stacked on each other) and I'm trying to find a way to dynamically change the y-axis interval if the values go above a set value.
If the bars only go up to a maximum of 50, I'd like the interval to be 25 so the bars still 'look' rather small. But if a large spike comes through, the interval needs to be set to 0 so the large spike is more noticeable.
Oh, and this is in C# .NET 3.5
I hope that makes sense :)
Thanks in advance
This is pretty much feasible by tweaking both the size of the axis and the relative intervals dynamically by updating the right properties. Default control behavior is rescaling the axis to adapt to the dataset so this should be no problem (works well for me)
Take a look at the Axis Class MSDN Reference, especially the Interval and IntervalAutoMode Properties. (the MSDN in pretty exhaustive on chart control if you dig enough you'll find everything you might need).
besides I HEAVILY advice you to download the very complete sample application
and play around with it locally. It is pretty exhaustive and you'll have the complete source at hand.
Set IntervalAutoMode="Variable Count" and dint mention any axis interval in the Axis Y element of the chart Area of that chart,it will adjust according to the maximum value.
I'm currently using a semi-transparent WPF form with no border as a camera style device in a program of mine. However, when I access the position (top, left) and dimension (height, width) properties of the form, it would appear they are wrong.
The top property reported to be roughly 26 pixels higher than it actually is in relation to the desktop. IE if I put the forms top and left at (0,0) the properties will report (0, -26).
The height is also incorrect, reporting about 50-60 pixels shorter than it actually is.
Has anybody experienced this problem in the past and have a solution?
Regards,
Andy Hunt
if I understand correctly, this is no problem at all but the expected behavior.
WPF uses DPI-independent virtual units for measuring size and position, not actual pixels.
There are many sources like this explaining this.
If I understood the question wrong (its late and I am tired ;) could you provide a code sample illustrating the issue?
Andrej