I want to draw a circle when ever user touches any place on the screen.I know to draw the circle using canvas,but how the drawn circles should be positioned to screen?
Does the surface view help for me?
Thanks in advance.
You can use the Activity.onTouchEvent event to determine where the user touched the screen. Then you can draw your circle on the canvas at that position.
Here's an example that handles a simple touch event.
#Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
// draw circle at x,y
}
}
Related
I am creating a POC on how to resize an imageView in android. I want to put 4 squares in the corner of the ImageView so I can drag the squares to resize. What should I use to get this result?
Get the coordinate of the corners
Draw rectangles at those coordinates with method like Canvas.drawRect(float left, float top, float right, float bottom, Paint paint)
Get the coordinates of the touch point from onTouch()
Resize the imageview with the new size
Get Coordinates of Touch Point
The class (suppose it is an Activity) should implements OnTouchListener, and register the listener to itself
public class MyActivity extends Activity implements OnTouchListener {
...
setOnTouchListener(MyActivity.this);
...
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
switch(action) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
// Get the coordinates
int x = (int)event.getRawX();
int y = (int)event.getRawY();
// Resize the imageView
resize();
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
Resize the ImageView
You can use ImageView.setMaxHeight(), ImageView.setMaxWidth() to resize the imageview
By the way, does the imageView you want to resize is in fact an image? In that case, using drawable might be a better idea
Maybe an overlay is what you need.
Create a 9-patch drawable with the squares and corners and put that layer on top of your imageview.
I'm working on an app that plots nodes on a map, and each node has edges that are represented by a line between them. I've drawn the edges using Canvas and drawLine(), but it would be useful if the lines themselves could be clickable. By that I mean a method of allowing the user to touch the line or think they're touching the line and an event can trigger. (like display edge info, etc...)
I can't rightly attach a touch event to a line I've drawn with Canvas, so I was thinking of placing ImageViews inbetween the ends of each edge line that's drawn. The ImageView could be a dot so it's clear where the touch event triggers.
Does anyone have any other suggestions? I'm mainly looking for ideas that I've missed. Maybe there's something in the Android API that can help with this that I'm unaware of.
Thanks in advance for any tips!
Use a path to draw the line:
Path linePath;
Paint p;
RectF rectF;
float point1X, point1Y, point2X, point2Y;
// initialize components
// draw the line
linePath.moveTo(point1X, point1Y);
linePath.lineTo(point2X, point2Y);
canvas.drawPath(linePath, p);
linePath.computeBounds(rectF, true);
Override onTouchEvent(MotionEvent):
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (rectF.contains(touchX, touchY)) {
// line has been clicked
}
break;
}
return true;
}
I have created my own component which extends RelativeLayout, inside it I have a field of View type which is being positioned with margins:
Blue quad represents my parent view, and a red circle is a child.
Now I want to allow user to move this red circle while moving a finger on a screen.
This is my onTouchEvent() method:
#Override
public boolean onTouchEvent(MotionEvent event) {
int eventAction = event.getAction();
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
// finger touches the screen
break;
case MotionEvent.ACTION_MOVE:
marginX = (int)event.getX();
marginY = (int)event.getY();
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)pointer.getLayoutParams(); // pointer is this red circle
lp.setMargins(marginX, marginY, 0, 0);
break;
case MotionEvent.ACTION_UP:
// finger leaves the screen
break;
}
return true;
}
But I quickly found out that it doesn't refresh it's position while user is moving his finger on a screen, but instead it does it only when user releases his finger from the screen. So basically, when I move my finger, circle stays in a same position, but when I stop moving, circle is drawn in new position. I tried using invalidate() method on both, parent and child views, but it didn't help. I assume that it may have something to do with UI thread, but I don't have access to runOnUiThread()' method as it's not anActivity`.
EDIT:
I passed an activity to my class, so that I can use runOnUiThread(). My case: ACTION_MOVE now looks like this:
case MotionEvent.ACTION_MOVE:
marginX = (int)event.getX();
marginY = (int)event.getY();
activity.runOnUiThread(new Runnable() {
public void run() {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)pointer.getLayoutParams();
lp.setMargins(marginX, marginY, 0, 0);
pointer.invalidate();
}
});
break;
If it's simple pointer you can draw it on Canvas as Drawable:
#Override
protected void onDraw(Canvas canvas) {
pointer.setBounds(marginX, marginY, marginX + pointerWidth, marginY + pointerHeight);
pointer.draw(canvas);
super.onDraw(canvas);
}
And then call invalidate() on pointer in onTouchEvent() method.
I think you need drag and drop function
Refer http://www.vogella.com/articles/AndroidDragAndDrop/article.html
I have an ImageView with an image loaded,
I want that when the user clicks in a point of the image, another little image(used as pin) is overlapped in this point and the coordinates of the point are returned.
But I haven't idea of how could I do this.
To place a image at a certain coordinates you will have to draw the image on the canvas .
To get the coordinates of the touch event use the following code:
#Override
public void onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mTouchX = event.getX();
mTouchY = event.getY();//stores touch event
} else {
mTouchX = -1;
mTouchY = -1;
}
super.onTouchEvent(event);
}
Here is the code for drawing the image on canvas Image in Canvas with touch events
hope it helps.
Try this,
It will help you to place image based on the co-ordinates. So it can be overlapped
Link 1
I have a table-top style game board consisting of 10x10 squares. Each square is a PNG image. On top of these squares I want to place tiles which can be drag and dropped on top of the squares.
What would be my best approach concerning Views?
Is it possible to have two layers where layer one is a grid of ImageView's which represents the board. Would it then be possible to let the tile be an ImageView also which could be "stacked" on top of the ImageView's which represents the board?
Any good design ideas are welcome!
Thanks.
/ Henrik
Override onTouchEvent in your View:
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (!mEnable) {
return false;
}
int action = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
onTouchActionDown(x, y);
break;
case MotionEvent.ACTION_MOVE:
onTouchActionMove(x, y);
break;
case MotionEvent.ACTION_UP:
onTouchActionUp(x, y);
break;
}
return true;
}
Then in either onTouchActionUp/Down just use the co-ordinates and redraw your image resource, i.e. invalidate() your view.
Override onDraw to draw the dragged graphic:
#Override
protected void onDraw(Canvas canvas)
drawMyDragGfx();
}
I did a override on onDraw and onTouchEvent in my View. I.e. I did the drag and drop drawing all by myself.
Worked like a charm :-)