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.
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;
}
I have set the onTouchListener on the blue part of the screen. It will only obtain the x,y coordinates when finger is touched on it. The white part does not do anything at all. The problem is that if at the beginning the finger is touched on the blue part and when it "moves out" to the white part, it is still continuing to obtain the x, y coordinates. Ideally I want the device to stop detecting the motion event as the finger leaves the screen. Hope you can help.
The easiest solution is to check if your finger is touching the View by simply comparing its x,y coordinates with the Views bounding rect. You can stop receiving events by returning false from dispatchTouchEvent then.
edit:
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
Rect outRect = new Rect();
getDrawingRect(outRect);
if (outRect.contains((int) event.getX(), (int) event.getY())) {
return true; // I want more events
} else {
return false; // stop events until next ACTION_DOWN on me
}
}
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.
So I'm making an application where you can click on the screen and create an object on the fly. How should I go about doing this? I've created a seperate class that holds the properties of the object you want to create, a class for each specific shape I want to render, and an abstract class for the previous classes. But basically I just want to be able to click on a specific spot and it renders a specified shape centered on that point.
First, let your activity implement the OnTouchListener and overriding the folllowing method where you can retrieve the touch-coordinates:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
touchX = (int) event.getX();
touchY = (int) event.getY();
}
return true;
}
On the touch event, you can then call your method to draw the desired shape passing the touchX and touchY values and do a translation.
I have a large image on the screen and I want to display a small image on that image where I touch the screen. but I do not know how to change the position of the image when I touch on the screen and the small image must display where ever I touch on screen.
any suggestions or hint will be appreciative.
thanks
Suppose you have your moving bitmap already in an ImageView which is part of your RelativeLayout.
Whenever the user touches the screen, you just have to change the position of the ImageView, by changing its margins.
You should try something like this:
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
RelativeLayout.LayoutParams params = myImageView.getLayoutParams();
params.setMargins(event.getX(),event.getY(), 0, 0);
myImageView.setLayoutParams(params);
}
}
User first the method onTouchEvent() to get the position of your touch.
Then i suppose you have your Bitmap placed in a UIElement like for example ImageView? And if you're using AbsoluteLayout you can set the ImageView object at the same coordinates you reveiced in the ontouchEvent.
#Override
public boolean onTouchEvent(MotionEvent event) {
event.getX();
event.getY();
}