How to change the position of a view? - java

I have two views called x and y they are both black lines (for example I made the height of the x line is 1dp and width 230dp and as background filled with the color black).
Now i want to move the position of the lines programmatically (for example I want the y line 50dp to the right of the orginal position).
Can someone help me how to do this?
I have tried things such as setpadding but the line doesn't move.
Thanks in advance!
(ps: my minimum sdk is set for 7 so i can't use the newest api's).

Old Answer
Have a look at the Absolute Layout, it allows you to position
child elements using x, y coordinates. It is deprecated but it's the
only way in Android to do real x,y coordinate positioning.
I would ask what the main point behind what you are trying to do is
though? It sounds like you started with a goal, were led down a path
and now are asking how to get to the end of that path, rather than
asking how to do what you need to do.
Edited
For drawing graphs have a look instead at https://stackoverflow.com/questions/2271248/how-to-draw-charts-in-android.
Using a Layout class to draw a chart will only lead to a really slow app, since the layout classes are designed for creating relatively static layouts, not drawing full graphics.
Instead use either the Canvas and draw your drawables yourself or use the graphing packages listed in the SO question I linked above.

Related

How do I make 2d- Flashlight effect in Processing 3

For reference the effect I'm going for is this:
I'm working in Processing 3, NOT p5.js.
I've looked around processing forums, but i can't find anything that works in the current version or doesn't use PGraphics and a mask which from what I've read can be expensive to use.
My current ideas and implementations have resulted to drawing shapes around the player and filling the gaps in with a circles with no fill that has a large stroke weight.
Does anyone know of any methods to easily and inexpensively draw a black background over everything except a small circular area?
If this is the wrong place to ask this question just send me on my way I guess, but please be nice. Thank you:)
You could create an image (or PGraphics) that consists of mostly black, with a transparent circle in it. This is called image masking or alpha compositing. Doing a Google image search for "alpha composite" returns a bunch of the images I'm talking about.
Anyway, after you have the image, it's just a matter of drawing it on top of your scene wherever the player is. You might also use the PImage#mask() function. More info can be found in the reference.

Java - Screen Coordinates to Image Coordinates

I am using a Java application to display an image on the screen. I also am using an eye-tracker device which records the absolute pixel X,Y locations where the person is looking on the screen.
However, what I need to do is convert these X,Y coordinates from the screen positions into the X,Y locations of the image. In other words, somehow I need to figure out that (just an example) 482, 458 translates to pixel 1,1 (the upper left pixel) of the image.
How can I determine the image's placement on the screen (not relative to anything)?
I saw a few posts about "getComponentLocation" and some other APIs, but in my experimentation with these, they seem to be giving coordinates relative to the window. I have also had problems with that because the 1,1 coordinate that they give is within the window, and there is actually a bar at the top of the window (that has the title and the close and minimize buttons) whose width I do not know, so I cannot easily translate.
Surely there must be a way to get the absolute pixel location on the screen of a component?
If we are talking about Swing/AWT application than class java.awt.Component has method getLocationOnScreen which seemed to do what you want
And yes as #RealSkeptic mentioned in comments to question:
SwingUtilities.html#convertPointFromScreen
will do all this work for you considering components hierarchy

Confused with image scaling and positioning in libgdx

I'm having quite a bit of difficulty wrapping my head around the actual display side of things with libgdx. That is, it just seems fairly jumbled in terms of what needs to be done in order to actually put something up onto the screen. I guess my confusion can sort of be separated into two parts:
What exactly needs to be done in terms of creating an image? There's
Texture, TextureRegion, TextureAtlas, Sprite, Batch, and probably a
few other art related assets that I'm missing. How do these all
relate and tie into each other? What's the "production chain" among
these I guess would be a way of putting it.
In terms of putting
whatever is created from the stuff above onto the monitor or
display, how do the different coordinate and sizing measures relate
and translate to and from each other? Say there's some image X that
I want to put on the screen. IT's got it's own set of dimensions and
coordinates, but then there's also a viewport size (is there a
viewport position?) and a camera position (is there a camera size?).
On top of all that, there's also the overall dispaly size that's
from Gdx.graphics. A few examples of things I might want to do could
be as follow:
X is my "global map" that is bigger than my screen
size. I want to be able to scroll/pan across it. What are the
coordinates/positions I should use when displaying it?
Y is bigger
than my screen size. I want to scale it down and have it always be
in the center of the screen/display. What scaling factor do I use
here, and which coordinates/positions?
Z is smaller than my screen
size. I want to stick it in the upper left corner of my screen and
have it "stick" to the global map I mentioned earlier. Which
positioning system do I use?
Sorry if that was a bunch of stuff... I guess the tl;dr of that second part is just which set of positions/coordinates, sizes, and scales am I supposed to do everything in terms of?
I know this might be a lot to ask at once, and I also know that most of this stuff can be found online, but after sifting through tutorial after tutorial, I can't seem to get a straight answer as to how these things all relate to each other. Any help would be appreciated.
Texture is essentially the raw image data.
TextureRegion allows you to grab smaller areas from a larger texture. For example, it is common practice to pack all of the images for your game/app into a single large texture (the LibGDX “TexturePacker” is a separate program that does this) and then use regions of the larger texture for your individual graphics. This is done because switching textures is a heavy and slow operation and you want to minimize this process.
When you pack your images into a single large image with the TexturePacker it creates a “.atlas” file which stores the names and locations of your individual images. TextureAtlas allows you to load the .atlas file and then extract your original images to use in your program.
Sprite adds position and color capabilities to the texture. Notice that the Texture API has no methods for setting/getting position or color. Sprites will be your characters and other objects that you can actually move around and position on the screen.
Batch/SpriteBatch is an efficient way of drawing multiple sprites to the screen. Instead of making drawing calls for each sprite one at a time the Batch does multiple drawing calls at once.
And hopefully I’m not adding to the confusion, but another I option I really like is using the “Actor” and “Stage” classes over the “Sprite” and “SpriteBatch” classes. Actor is similar to Sprite but adds additional functionality for moving/animating, via the act method. The Stage replaces the SpriteBatch as it uses its own internal SpriteBatch so you do not need to use the SpriteBatch explicitly.
There is also an entire set of UI components (table, button, textfield, slider, progress bar, etc) which are all based off of Actor and work with the Stage.
I can’t really help with question 2. I stick to UI-based apps, so I don’t know the best practices for working with large game worlds. But hopefully someone more knowledgeable in that area can help you with that.
This was to long to reply as a comment so I’m responding as another answer...
I think both Sprite/SpriteBatch and Actor/Stage are equally powerful as you can still animate and move with Sprite/SpriteBatch, but Actor/Stage is easier to work with. The stage has two methods called “act” and “draw” which allows the stage to update and draw every actor it contains very easily. You override the act method for each of your actors to specify what kind of action you want it to do. Look up a few different tutorials for Stage/Actor with sample code and it should become clear how to use it.
Also, I was slightly incorrect before that “Actor” is equivalent to Sprite, because Sprite includes a texture, but Actor by itself does not have any kind of graphical component. There is an extension of Actor called “Image” that includes a Drawable, so the Image class is actually the equivalent to Sprite. Actor is the base class that provides the methods for acting (or “updating”), but it doesn’t have to be graphical. I've used Actors for other purposes such as triggering audio sounds at specific times.
Atlas creates the large Texture containing all of your png files and then allows you to get regions from it for individual png's. So the pipeline for getting a specific png graphic would be Atlas > Region > Sprite/Image. Both Image and Sprite classes have constructors that take a region.

Multiple color/stroke gesture and persist drawing in same GestureOverlayView [duplicate]

This question already has an answer here:
Regarding Android Paint drawing color
(1 answer)
Closed 9 years ago.
I have seen many examples of drawing using gesture. My requirement is that, I want to draw multiple colored and multiple stroked gesture in same GestureOverlayView. To be more clear, please have a look at below image.
If user select different colors and different strokes, drawing should be of respective color/stroke. Now, its drawing different colored and different stroke gestures but if I select different color or different stroke, previously drawn gesture also takes new color or new stroke.
One similar question is answered here but has same issue.
Kindly suggest.
<android.gesture.GestureOverlayView
android:id="#+id/signaturePad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:eventsInterceptionEnabled="false"
android:fadeEnabled="false"
android:fadeOffset="122000"
android:gestureStrokeLengthThreshold="0.1"
android:gestureStrokeWidth="6"
android:gestureStrokeType="multiple"
android:orientation="vertical" >
</android.gesture.GestureOverlayView>
In java code, changing color code
mGestureOverlayView.setGestureColor(Color.BLUE);
Thanks.
Edit : Thought to implement same with Canvas and got code from here. Its drawing multiple lines but again problem is when I change color/stroke, recently drawn lines also taking new color/stroke.
Please suggest whats going wrong here.
Edit (Answer) : Finally got solution. Have a look at this link.
I'm afraid this wouldn't be possible without rolling your own version of GestureOverlayView with considerable modification. Here's a run through of what you'd need to change.
Consider what would be needed for this to work:
You would need an iterable (or, in fact, List of some kind) of the past color settings- or in Android a stack of the past Paints.
The Gestures would need to be stored in a separable way. Inside GestureOverlayView, these are stored using Android's Path, so these would need to be seperable- given path's api, the only way to do this would be as different path instances that could be overlaid seperately.
Finally, there would have to be an ordering on the paths, so that the association between colours and paths could be made.
The above would, in very rough terms, enable you to iterate through to the latest path and colour and use the instance of View's draw(Canvas canvas) method to thisViewsCanvas.drawPath(mLatestAddedPaths.next(), mLatestPaintColor.next()) foreach path and paint.
*Finally, a little optimization would be to create each available paint colour when the view is created, and to hash it against its color, to save on paint creation. Your mLatestPaintColor list would then contain the hashes for the paints.
To see what's missing if you view the source, it's pretty clear that GestureOverlayView is along way from providing this:
Look at the setGesutreColor(color), which you reference in your question. All this does is set a field mCertainGestureColor which is then used to set the colour for all draws.
Rather wisely, when you call setGesture(Gesture gesture) (that's every time you make a gesture) the developers of this class extract the path on line 286 of this particular version:
final Path path = mCurrentGesture.toPath();
And then instead of storing every path that was ever extracted, they combine that new path ("path") with the master path currently existing ("mPath"), on line 292:
mPath.addPath(path, -bounds.left + (getWidth() - bounds.width()) / 2.0f, -bounds.top + (getHeight() - bounds.height()) / 2.0f);
This means that when it comes to draw(Canvas), we have no idea which paths came in which order.
3 . Which finally takes me to why 3 doesn't work- the information available to the draw method on line 394 is only enough to draw each path the same colour:
canvas.drawPath(mPath, mGesturePaint);
So, it should be pretty possible to just reimplement this class yourself to achieve your goals, but it is pretty much impossible to try and achieve what you want to with the current class.

Mapping coordinates with respect to different screen sizes

I heard SO people like diagrams so I took the time to draw one :D
I need to design an activity that roughly looks like the following:
The map is just an image file of city, terrain etc. Users are able to enter values inside the x-coord and y-coord textbox and when both of them have been entered, a triangular icon (also an image file) will appear on top of the map with respect to the coordinates on the map. The min,max of coordinates are fixed as 0,0 to 10000,10000 for all maps. Users can click the triangular icon (button) and it just takes them to another activity.
What is a sensible approach of designing something as above?
As far as I'm concerned, there are several things I need to take into consideration:
1)Mapping the coordinates with respect to the screen size. An obvious workaround for this is to use absolutelayout and use pixels for positioning the triangular icon, but this solution is terrible for obvious reasons.
2)Laying a button on top of the map (image file). After playing around with bunch of layouts, I couldn't figure out how to do this via Java.
I'm just looking for a really simple guidance, just to get myself going in the right direction.
I would use a SurfaceView. You can draw anything in the onDraw method. Youll get a Canvas object to draw on so it shouldn't be difficult to transform your coordinates and do what you want.
take a look at this tutorial:
http://www.mindfiresolutions.com/Using-Surface-View-for-Android-1659.php

Categories

Resources