Resize and Rotate Rectangle by using its line touch event - android

Move rectangle on finger touch event and also Resize rectangle by moving it left ,right, top or bottom line finger touch and rotate it when I touch or move on bottom right corner of the rectangle(show in figure) and redraw it (canvas) android please help if anyone can.

If you have all in a child class from View you could override
#Override
public boolean onTouchEvent(MotionEvent event) {
final Point point = new Point((int) event.getX(), (int) event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
press(point);
break;
case MotionEvent.ACTION_MOVE:
drag(point);
break;
case MotionEvent.ACTION_UP:
release(point);
break;
}
return true;
}
In the method press(Point point) you need to identify with point to point or point to line distance which feature is the most near to be selected. And the method drag(Point point) helps you to drag the rect.
Drawing
Drawing should take place in the onDraw(Canvas canvas) method but the instantiation should be outside this method. So you create the Rect in your constructor or so
Rect rect = new rect(10, 10, 100, 100)
Resize
When you try to resize you don't need to create a new Rect you'll just use
rect.set(x, y, dx, dy)
Rotate
When you want to rotate you need to rotate the Canvas then draw and afterwards rotate back to draw something else.
canvas.save();
canvas.rotate(45);
canvas.drawRect(rect, paint);
canvas.restore();

Related

Create a square on the corners of the imageView

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.

Technique to make a canvas drawLine() clickable?

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;
}

Redraw view during MotionEvent.ACTION_MOVE in onTouchEvent()

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

Draw circle on view

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
}
}

Drag and drop overlapping ImageView in Android

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 :-)

Categories

Resources