As finger moves on screen i want to track continuously x and y coordinates on MotionEvent.ACTION_MOVE Event.
meanwhile i should be able to know whether x,y values are increasing or decreasing.
Is it possible to do this, any help would be appreciated .
You just have to capture the starting coordinates during the ACTION_DOWN event and compare them to the the coordinates during ACTION_MOVE
Something like:
public class MyView extends ViewGroup {
private float startX;
private float startY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float dx = startX - event.getX();
float dy = startY - event.getY();
break;
}
return true;
}
}
If you want to track it even more then you can create a helper class which tracks a sample of the events to give you a direction, etc
Related
I made an android custom keyboard.
I want to use swiping on Space key on the keyboard for changing keyboard layout to show next language layout.
How can i do that?
I used bellow class:
public class KeyboardIMS extends InputMethodService implements KeyboardView.OnKeyboardActionListener
{ ...}
You can do this by override touchEvent like this :
#Override
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
mIsDown = true;
break;
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;
float dy = y - mPreviousY;
// Here you can try to detect the swipe. It will be necessary to
// store more than the previous value to check that the user move constantly in the same direction
detectSwipe(dx, dy);
case MotionEvent.ACTION_UP:
mIsDown = false;
break;
}
mPreviousX = x;
mPreviousY = y;
return true;}
I have drawn piechart using canvas method of view , but now i want get click of individual pie ? how can i dot that?
I got perfect answer for this question:
get color code of click area and check if color match with your color code this will get click you want.
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
Logger.debug("X-->"+touchX+" Y---->"+touchY);
//get drawing cache of your view
Bitmap bitmap = getDrawingCache(true);
//Get color code of pixle where you have tap
int colorCode=bitmap.getPixel((int)touchX,(int)touchY);
if(colorCode == context.getResources().getColor(R.color.pie_blue)) {
Logger.debug("Color blue");
onPieClick.onBluePieClick(touchX,touchY);
}else if(colorCode == context.getResources().getColor(R.color.pie_green)) {
Logger.debug("Color green");
onPieClick.onGreenPieClick(touchX,touchY);
}
return super.onTouchEvent(event);
}
What you can do is,
Override onTouch event & You will get Motion event,
You will get x & y co-ordinates of the click by event.getX() &
event.getY() respectively.
identify where this x & y intersect in pie.
Sample code:
1)Simple
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
float xCord=event.getX();
float yCord = event.getY();
....
Write condition to identify where this x & y intersect in pie.
...
}
return true;
}
2) Another way getting touch (good way)
OnGestureListener mGestureListener=new GestureDetector.SimpleOnGestureListener(){
public boolean onSingleTapConfirmed(MotionEvent e) {
float xCord=e.getX();
float yCord = e.getY();
....
identify where this x & y intersect in pie.
...
};
};
GestureDetector gestureDetector=new GestureDetector(context, mGestureListener);
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
You can't. Well, at least not directly.
You could do the following though:
In the click handler for the view, determine the xy coordinates of the click
Compare the drawing code you wrote, thereby determining in which piece of the pie the click was
I am guessing this means making the whole screen touchable. How exactly is that done?
and secondly calculating if an X,Y is within the EditText.
override the onTouchEvent in the activity you want to implement this....and get the X, Y co-ordinates using event.getX() and event.getY()
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
but i suggest you must search thoroughly before posting a question.
How to handle touchscreen events for OpenGl programs in Android? I have checked out several posts related to this question, like-
How to intercept touchscreen events in Android OpenGL ES?
But my problem is that I am only able to handle MotionEvent.ACTION_DOWN events, even if I write the handler for other type of events, those handlers are not called.Android docs sample also uses this event only.Are the other events supported??
My onTouch handler looks something like this-
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
Log.d(TAG, "mode=DOWN");
break;
case MotionEvent.ACTION_UP :
case MotionEvent.ACTION_POINTER_UP :
Log.d(TAG, "mode=UP");
break;
}
return true;
}
Try doing a series of if/else statements and see if they work, then try converting it to a switch statement. Maybe take out break and instead return true.
This is how I do it:
#Override
public boolean onTouch(View v, MotionEvent event) {
final float mouseSensitivity = 0.5f;
if(event.getAction()==MotionEvent.ACTION_DOWN){
startX = event.getX();
startY = event.getY();
} else if(event.getAction()==MotionEvent.ACTION_UP){
startX = 0.0f;
startY = 0.0f;
} else if(event.getAction()==MotionEvent.ACTION_MOVE){
graphicsRenderer.rotate((startY - event.getY()) * mouseSensitivity,(startX - event.getX()) * mouseSensitivity);
startX = event.getX();
startY = event.getY();
}
return true;
}
// Where startX and startY are global int variables;
can any one help me how to drag and drop the text view on entire image view. i found from this drag n drop textview in android here the text view is static, in my app textview and image view's are dynamically change. `#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember our initial down event location.
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float x = event.getRawX();
float y = event.getRawY();
// Calculate move update. This will happen many times
// during the course of a single movement gesture.
scrollByX = x - startX; //move update x increment
scrollByY = y - startY; //move update y increment
startX = x; //reset initial values to latest
startY = y;
invalidate(); //force a redraw
break;
}
return true; //done with this event so consume it
}
i wright this code, but it's not working properly.
You can always change the image and text of existing view programatically using setText sort of methods.
There are many drag and drop tutrials if you google it. strongly suggest you try them out.
Basically what you do is implement a OnTouchListener on the TextView like
textview.setOntouchListener(listen);
OnTouchListener onThumbTouch = new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch(v.getId())
{
case R.id.slider_thumb_id:
{
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
}
case MotionEvent.ACTION_UP:
{
}
}
break;
}
}
return true;
}
};
Here from the event attribute you get the value of x and y and the touch event and
use that to alter the position of the textview. I did it by add add/subtracting the margin values.
EDIT:
This is what I did to update the position of my view
Look event.getRawY(); is going to give you the distance from the parent view. So if I get event.getRawY() = 50. I know I need the text view to be 50 pixels from the top.
LinearLayout.LayoutParams iconParams = (LinearLayout.LayoutParams)
v.getLayoutParams();
iconParams.topMargin = 50;
v.setLayoutParams(iconParams);
So now the view that i selected is 50 px from the top of the parent view.