I'm working on an Android app and I want to add some eyecandy to the UI. I have an activity (let's call it MainActivity) which has some text fields, buttons and a gallery on it.
What I would like to achieve is: When the user touches somewhere on this activity, there should be some visual effect at the point where he touched (e.g. something like sparkles etc.).
So the essential parts of my question are:
a) How can I determine where the user touched
b) how can I draw my effects on to the screen (as an 'overlay' to the activity).
Thanks in advance for every helpful answer.
(I've checked out this answer but it doesn't seem to be applicable to my situation. )
First, you have to add onTouchListener to your root Layout, then you can get coordinates, where the user has touched the screen.
example :
float x,y;
rl = (RelativeLayout) findViewById(R.id.root_layout);
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return super.onTouchEvent();
}
});
You should override the onTouchEvent method of your activity. You can then obtain the coordinates of the touch event:
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
break;
case MotionEvent.ACTION_MOVE:
// movement
break;
case MotionEvent.ACTION_UP:
// touch up
break;
default:
// default
}
return super.onTouchEvent(event);
}
To answer further on your question, to have a overlay, look at the APIDemos -> Graphics -> OpenGL ES -> Translucent GLSurfaceView. This will help you to create overlay on your activity. There may be some other example in API demos which will get you some help. Android's API Demos are good set of examples to address some known issues.
If you are working on ICS then I recommend you to look at the Developer Options in Settings application. If you are not working with ICS, I believe you can look at the APIDEMOS for touchevents. They draw lines on touches. The code in that, can get you started.
Related
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;
}
Can anybody point me to some classes or suggest anything for the following case?
I have SurfaceView which has a background image, on top of which I wish to paint other bitmaps. I would like to support following operations:
on single tap new bitmap is added on top of background,
on double tap bitmap at given position is removed,
on tap&move (like drag&drop) bitmap is moving,
on press and move surface view is scrolling,
on pinch out/pinch in surface view is scaling accordingly.
Is this doable only with GestureRezognizer? If not, how to handle all those cases?
To handle touch input, override onTouchEvent in your class that extends SurfaceView to handle a MotionEvent. Here is a sample code that gets the screen position when a user first touches the screen.
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
touchX = event.getX();
touchY = event.getY();
}
return true;
}
More information about the MotionEvent object can be found on the Android Developers website.
I have created panel with collection of different colors, pencil, eraser and different shapes similar to MS-Paint. I could be able to draw or write on a screen using Touch Event method. But when I draw something on a screen (when I touch the screen), MotionEvent.ACTION_Down method is calling. So it works fine. When I release my finger from the screen, MotionEvent.ACTION_up method is calling and works fine.
So, my problem is, like MS-PAINT I am not able to see what I drew or wrote before I release my finger from the screen. For an example, see this video. User can see when he dragged the shapes or trying to draw a pencil. Also, in this link user draws using pencil and it is visible without releasing a finger on the screen.
But, when I draw something on the screen, once I released the finger only it appears.
What I need is, when user touch the screen itself if he/she moves the finger on the screen, user must be able to see what they are trying to draw or write on the screen.
For an example: if I try to write some word like "Apple" on a screen, I am trying to put "A" . But when I write letter "A", it is invisible unless I take my finger from the screen. Once if I released my finger from the screen after I drew letter"A" then only the text or picture has been appeared on a screen what I drew.
So, I have done MotionEvent.ACTION_DOWN and MotionEVent.ACTION_UP. It works fine.
But, MotionEvent.ACTION_MOVE is not working properly at all.
This is my Code,
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(Shape == ShapeLine)
{
GraphicObject = new Line();
((Line) GraphicObject).getBegin().setX(event.getX());
((Line) GraphicObject).getBegin().setY(event.getY());
}
if(Shape== ShapeRect)
{
GraphicObject = new Rectangle();
Point temp = new Point(event.getX(), event.getY());
endPoint = new Point();
((Rectangle) GraphicObject).settemppointOfOneEndRectangle(temp);
}
else if(event.getAction() == MotionEvent.ACTION_MOVE){
if(Shape== ShapeLine)
{
final float x=event.getX();
final float y=event.getY();
}
if(Shape == ShapeRect)
{
endPoint.x=event.getX();
endPoint.y=event.getY();
invalidate();
}
Anyone suggest me, about ACTION_MOVE. I have tried a lot in a code but no changes and I didn't find any solution while moving.
Basic idea is when you tap record that point in a variable,then inside ACTION_MOVE record the current point and draw a line in between these 2 points.Once done save this point in the previous point. Sudo code:
Point last;
Point current;
...
case ACTION_DOWN:
last=mouse.position;
break;
case ACTION_MOVE:
current=mouse.position;
drawLine(current,last);
last=current;
break;
Do this way,your drawing should be fine.
N.B. Remember,this is a sudo code. :P
EDIT. Example from one of my app. Basically I pointed out what you should do:
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch(action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
initial.x=(int)event.getX();
initial.y=(int)event.getY();
break;
case MotionEvent.ACTION_MOVE:
current.x=(int)event.getX();
current.y=(int)event.getY();
//draw line using initial as start and current as end point
//sudo code: drawLine(initial,current)
//now set initial to current
initial=current// for the continuity of drawing.
break;
}
return true;
}
initial and current both are Point objects.
How to make picture in a Canvas onTouch, like a button. Just to be press and nothing else. I can insert picture in Canvas, now how can I make her onTouch? Can somebody pls give me easy exsample? Dont be mad, cuz this is probably stupid and easy question
Thank you
public boolean handleTouch(MotionEvent event)
{
float newX = event.getX();
float newY = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
if (isInRect((int) newX, (int) newY))
{
refreshView();
}
//....
}
}
You cannot add an onClick or onTouch listener to something you've drawn with the canvas. These listeners can only be applied to views.
Another method will be to apply the onTouchListener to the view, and find the touch position to find whether the touch was on the pictures position.
i am developing game.i am displaying gun object center bottom of the screen.when user tap on screen i need to rotate gun that direction.i done rotating image.but when user tap on screen i need to rotate image that direction.can you please help me
Thanks in advance
Aswan
is your image a bitmap? could you convert it to one? Would an onClick listener not work and when the user clicks do something like.. http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
Seeing some code as to what you've attempting would be nice also. The following will help you with checking if the bitmap has been clicked in case you're stuck on that also http://developer.android.com/reference/android/view/View.html .
I've never attemped this myself but I think that would work. Try redrawing the bitmap when you have resized it.
Just use onTouchListener for your View and use this code for that
#Override
public boolean onTouch(View v, MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
Log.i(TAG, "action type is"+event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
Log.i(TAG, "Entering in onTouch");
double rotationAngleRadians = Math.atan2(currentX - dialer.centerX, dialer.centerY - currentY);
dialer.rotationAngle = (int) Math.toDegrees(rotationAngleRadians);
Log.i(TAG, "rotaion angle"+dialer.rotationAngle);
dialer.invalidate();
return true;
}
}
return true;
}