Selecting and editing different paths drawn on canvas differently - android

In android I am drawing path creating a custom view and using onDraw() method. But after drawing many paths, I want to identify the paths differently and transform each of them differently. I have identified whether a point is on path or not using rectF and region, but to get contained path (using getBoundaryParh()) in the region is not helping me. So can someone help me with this, I just want to get the object of the selected path by touching on the screen. Thanks.

Related

Draw only part of a path

I have a fullscreen custom view in my activity where I have to draw paths that are formed by a line each one. Since my view supports scaling, depending on how zoomed in the view is, the paths get to big and therefore the view becomes slow, even crashing sometimes. What I want to know is if there's some built-in function where I can draw only the part of the path that is actually inside the canvas.
I've tried using Path.op with a rect the size of the screen, but didn't get any results; it either returns an empty path or a path that contours the screen. Same thing with Canvas.clipPath(). Passing a rect on through invalidate() also doesn't work for me since the said path IS insterecting the drawing area.
It's also worth metioning that I'm using paths because I need to use a PathDashPath effect. So, if there's a way os doing that with drawLine, it'd also help.

Fitting Matrix Scale of one Path to another later drawn path

I've got the following problem:
Currently, I draw a picture within a SurfaceView. Moreover, the user is allowed to
draw on this picture. Thereby the draw "figures" are handled as path-objects. In addition, a user can pan as well as zoom the image. The handling of the zoom and pan is done by using a matrix. The picture as well as every path object has the its own matrix.
So, if the user for example started the app, a picture is shown. Then, the user can add one path object after the other and so on. We create for every object a new matrix. For example, if a user zooms in then the path objects as well as the picture are zoomed.
Now my goal is that if a path object is added while the picture is zoomed in or zoomed out, then the path object should be added at the touched position but it should be scaled to the scale of the other already drawn objects. In other, simpler words: If I add a path object it should have the same scale as the already exisitng objects, but it should appear at its new position:
Until now I tried different ways for solving the problem, but nothing did work:
First, of all I scaled the Canvas with one single matrix instead of scaling every path object on its own. But then, the added path objects appeared at a wrong position while the picture was zoomed in.
Next, I tried to get the current scale and afterwards, I scaled the path object when I created it. This did not work either. For non of the methodes preScale,postScale,setScale ... Either the whole path was wrong or it appeared again at a wrong position.
Does anyone know a solution?

Creating a Region from a Path in Android

I'm a new Android developer that's developing a simple twist on the Tron game with GPS movement, but I'm having trouble implementing player's intersecting.
Right now, my player's trails are Paths that I move to co-ordinates and draw the co-ordinate difference as a line on the canvas.
The path class offers no such intersection method that I can find, so I resorted to using Regions which I've tested an intersection to work with 2 regular Rectangles, but I can't make a Region using Region.setPath for some reason. From my understanding, the path needs to be closed to form an area for it to create a Region, which isn't exactly what I need.
Is there a way to create a region off a path, and not the area the path creates? ie: If the path were a straight line of 10px thick, how do I make a region that is a line of 10px thick?
Here's a short code sample I'm doing:
Path p1path = new Path();
p1path.moveTo(startPos,startPos);
p1path.lineTo(newPos,newPos);
p1path.moveTo(newPos, newPos);
Region p1region = new Region();
p1region.setPath(p1path, new Region(0,0,480,800); // this is where the region isn't setting as I thought it would...
// do same for p2
if(p1.quickReject(p2)) // checks for intersection
Thanks :)
So I've solved this quite some time ago, but to help those who eventually stumble onto this and want some similar functionality:
This post is quite some time ago - so let me remember what happened.
Creating a Region around a path actually did work but for a very limited set of Paths. What I mean by "Creating a region around a Path" is that for a Path that goes from x1,y1 to x2,y2 create a rectangular Region that covers (for example) x1-50,y1-50 to x2+50,y2+50 where 50 is the pixel weight stroke of the Path.
If you can visualise it, it basically creates a rectangular region that covers the Path and it's 50px stroke so you can "fake" Path intersection using Regions. Wherever a Path is, a Region is and so when 2 Paths "intersect", you can check for Region intersection (which you can do but I've forgotten the method names).
This however proved to work only for a few Paths. Though I'd like to think my Math proficiency is adequate, I could not get it so that for whichever direction the Path went the Region would work. Different angles, different directions etc. caused the Region not properly drawing under the Path. My above example of using the 50 stroke width would only work for going a particular direction.
The solution my parter and I stumbled onto was creating a 2D integer array that mapped onto the screen. Wherever a Path went into a certain direction, we would fill every array cell the Path mapped onto with a specific value (1). We would do the same for the other Path, but with a different value (2). Each move you make you would check the 2D array against the Path co-ordinate to see if it has been occupied or not. There was an important mathematical formula that would extrapolate which cells were visited when you go from x1,y2 to x2,y2 that proved very helpful - I believe it was called something along Brasenheim's formula, or something.
It's not the most elegant solution, but it ended up faking Path intersection well. If anyone is interested in a better explanation, you can message me.
Good luck!

Trying to modify simple Android shape tutorial to use images, having trouble

I am following the below tutorial:
http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_2D.html
In it,
Canvas canvas.drawOval();
is called in order to have a default circle drawn, that bounces around. My normal way of learning a new graphics framework is to build on this, and then upgrade to images. Normally, this is very simple, but I"m having trouble here. There is no equivalent "drawImage" to the drawOval command (which I'm more used to).
Instead, I'm trying to figure out "drawables".
So, following another tutorial (specifically the default Android "snake" game), I tried doing:
Resources r = this.getContext().getResources();
in my view, then passed the resource object to my ball object, to get:
img = r.getDrawable(R.drawable.ball);
where ball is a file stored in /res/drawable/ball.png
In the ball objects draw method (which I call in the view's onDraw method), I have:
img.draw(canvas);
canvas is passed from onDraw, which is itself passed a canvas. I don't really understand who is calling onDraw (I assume some internal process), but this differs from the snake example where they make their own canvas?
Either way, this doesn't seem to work. I am not getting any image, or any errors. I know that img is at least populated (its not null or anything), but other than that I don't really know what to do...
If this were Ruby (my most familiar language), I'd call inspect on the img to see if actually has anything in it...is there an equivalent for Java (or should I fool around with break points)?
Is there anything obvious that I'm doing wrong? Can I not use the default canvas I'm being passed, even though I clearly can for drawing simple shapes?
Ah, I figured it out:
With the draw oval method, I needed to set the bounds like so:
RectF bounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
But for the drawable object, I have to go one step further and say
img.setBounds(bounds);
and make bounds be a Rect instead of a RectF.
Once that is done, voila, things are rendering.
It didn't occur to me at first that the bounds are how things know where to render themselves, and while you pass the bounds to an oval, you have to SET them to a drawable.

Place imageviews along a curved path

I am trying to develop a custom gallery like application in which, i would like to place Image views along an elliptical path whose size varies with position.
All ideas are welcomed.
Thanks & Regards,
Sen
I have only two ideas:
create a path, do the math and position the ImageViews inside a RelativeLayout/AbsoluteLayout on the calculated position. So you only use the path to do the math, not to really position the views on that
Use 2D drawing (on a canvas) there you can draw images along a path... how to draw a bitmap repeatedly on a path can be seen here: http://www.mail-archive.com/android-developers#googlegroups.com/msg14567.html

Categories

Resources