Im making a program about the game Go and im trying to write some code to allow a person to place a bead, which really just shows an invisible bead. But after an hour of going back and forth with it i just cant seem to figure out why the Y coordinate of my motionevent always seems 2 rows down from where it needs to be.
My code for filling my array
for(int y=0; y< 9;y++)
{
for(int x=0;x<9;x++)
{
String name="Whitebead" + ((9*y)+x+2); //The +2 is because of a naming problem
WhitePieces[x][y]=(ImageView) findViewById(getResources().getIdentifier(name, "id", getPackageName()));
}
}
My code for handling motion events
#Override
public boolean onTouchEvent(MotionEvent e)
{
if(e.getAction() == MotionEvent.ACTION_DOWN)
{
float X=e.getRawX();
float Y=e.getRawY();
if(X>Board.getLeft() && X<Board.getRight())
if(Y< Board.getTop() && Y>Board.getBottom());
player1.placepiece(X, Y);
}
return super.onTouchEvent(e);
}
And finally my code that resolves which bead to what coordinate
public void placepiece(float X, float Y)
{
int[] pieceindex=resolvePiece(X,Y);
pieces[pieceindex[0]][pieceindex[1]].setVisibility(ImageView.VISIBLE);
}
private int[] resolvePiece(float x, float y) {
int Xindex=0;
int[] oldcoords= new int[2]; //cordinates are like so {xCoord, Ycoord}
int[] newcoords= new int[2];
oldcoords[0]=pieces[0][0].getLeft(); //set the oldcoordinates to the first index
oldcoords[1]=pieces[0][0].getTop();
for(int i=1; i<9;i++) //go through the 9 indexs to find the closest X value
{
newcoords[0]=pieces[i][0].getLeft();
newcoords[1]=pieces[i][0].getTop();
if(Math.abs((int)x-newcoords[0])<Math.abs((int)x-oldcoords[0]))
{
Xindex=i;
oldcoords[0]=newcoords[0];
}
}
int Yindex=0;
oldcoords[0]=pieces[0][0].getLeft(); //Reset oldcoords again
oldcoords[1]=pieces[0][0].getTop();
for(int n=1; n<9;n++) //Go through the 9 indexes for the closest Y value
{
newcoords[0]=pieces[0][n].getLeft();
newcoords[1]=pieces[0][n].getTop();
if(Math.abs((int)y-newcoords[1])<Math.abs((int)y-oldcoords[1]))
{
Yindex=n;
oldcoords[1]=newcoords[1];
}
}
int[] rInt= new int[]{Xindex,Yindex};
return rInt;
}
//////EDIT: Fixed
I figured it out, at the top of the android window is about and inch of space where the title and battery life and stuff go, when you get motion coordinates it takes in the whole screen, where .getTop() only gets from where linear layout Starts. SO instead of using .getTop or.getLeft i used .getLocationInWindow(oldcoord[]) and it places the info i needed into my oldcoord array.
//////EDIT: Fixed I figured it out, at the top of the android window is about and inch of space where the title and battery life and stuff go, when you get motion coordinates it takes in the whole screen, where .getTop() only gets from where linear layout Starts. SO instead of using .getTop or.getLeft i used .getLocationInWindow(oldcoord[]) and it places the info i needed into my oldcoord array.
Related
I am using MPAndroidChart for displaying a line chart for my application. What I already have is a double tap listener for the whole chart.
Is there any way to obtain double tap listener control for an individual point in line chart? My best guess is to directly obtain the View object of the point and implement a gesture listener for the same. If that is plausible, how can I achieve that?
If this is not possible using MPAndroidChart, is there any other library that might help me with this?
Your solution would involve creating a class that implements OnChartGestureListener
You can immediately see there is a method:
void onChartDoubleTapped(android.view.MotionEvent me);
You would have to implement this method with the functionality you want. It would probably involve getting the raw pixel touch points from the MotionEvent and converting them to x and y values on the chart. There is instructions on how to do that in this answer and in the code below:
#Override
public void onChartDoubleTapped(MotionEvent me) {
float tappedX = me.getX();
float tappedY = me.getY();
MPPointD point = mChart.getTransformer(YAxis.AxisDependency.LEFT).getValuesByTouchPoint(tappedX, tappedY);
Log.d(TAG, "tapped at: " + point.x + "," + point.y);
}
Further examples of a custom OnChartGestureListener are inside the example project here
David Rawson's answer is just perfect and answers the question leaving no stones unturned. However, for my situation, I found the answer by firegloves more useful. I actually get the Entry object using firegloves' solution to obtain values from. Following is the code for what solved my problem but please do note that this solution is hacky and is not advised until absolutely necessary. If you want a clean solution, David Rawson's answer already covers that.
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, Highlight h) {
final long selectionTime = System.currentTimeMillis();
final Highlight copyHighlight = h;
final Entry copyEntry = e;
final boolean[] isTapped = {false};
mChart.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!isTapped[0]) {
final long doubleTapTime = System.currentTimeMillis();
if (doubleTapTime - selectionTime < 300) {
if ((motionEvent.getX() <= copyHighlight.getDrawX() + 50
&& motionEvent.getX() >= copyHighlight.getDrawX() - 50)
&& (motionEvent.getY() <= copyHighlight.getDrawY() + 50
&& motionEvent.getY() >= motionEvent.getY() - 50)) {
if (copyEntry.getX() == 1.0f && copyEntry.getY() == 1.0f) {
Toast.makeText(MainActivity.this, "1,1", Toast.LENGTH_SHORT).show();
} else if (copyEntry.getX() == 2.0f && copyEntry.getY() == 2.0f) {
Toast.makeText(MainActivity.this, "2,2", Toast.LENGTH_SHORT).show();
} else if (copyEntry.getX() == 3.0f && copyEntry.getY() == 3.0f) {
Toast.makeText(MainActivity.this, "3,3", Toast.LENGTH_SHORT).show();
}
}
}
}
isTapped[0] = true;
return false;
}
});
}
#Override
public void onNothingSelected() {
}
});
The code snippet above let me work with the Entry object which was associated with the point that was double tapped. Also, I have set a range of 100 pixels while the point was double tapped for the convenience of fat-finger tapping.
I'd love to know if there is a clean solution for double tapping a value while obtaining its Entry data.
Deeping into LineChartRenderer I think it's not possible because graph rendering is reached drawing directly on Canvas like follows:
canvas.drawPath(mCirclePathBuffer, mRenderPaint);
You could try to print all your graph children to check if I'm wrong, something like:
int size =lineChart.getChildCount();
for (int i=0; i<size; i++) {
View v = lineChart.getChildAt(i);
Log.i("### LINE", "### INSTANCE OF " + v.getClass());
}
If there are children views they must be accessible it this manner.
Otherwise you could try to intercept double tap on your entire LineGraph, then you could try to check if tap position correspond to a mapped position but I think you must do the entire work on your own.
Another options could be to implement OnChartValueSelectedListener and emulate double tap with a timer mechanism.
I don't think there is a library that supports this functionality.
Let me know if you find a solution please
I am having a hard time trying to ensure no duplicates numbers appear in the following code below. I tried to create an ArrayList to add all the possible index of the array and then gradually remove the values of the array from the List as more elements are generated by the number generator. This; however, didnt work as I expected. I've been trying to think about it but Haven't been able to come up with a viable solution.
Random randGen = new Random();
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = touchevent.getX();
y2 = touchevent.getY();
int maxLength=exampleArray.length;
List<Integer>indices=new ArrayList<Integer>(maxLength);
for(int i=0;i<maxLength;i++){
indices.add(i);
}
int randomApple = randGen.nextInt(exampleArray.length);
int randomNum=indices.get(randomApple) ;
indices.remove(randomApple);
textView.setText(exampleArray[randomNum]);
if (x1 < x2) {
}
if (x1 > x2) {
//textView.setText(exampleArray[randomNum]);
}
}
}
return false;
}
I think you are missing some steps and adding extraneous ones....
One simple way (if you don't care whether the numbers keep going up) is to keep adding a random positive value to a counter. That way the new counter will always be different.
If you do:
Another way to do this is to creating a list of numbers beforehand (either sequentially [1, 2, 3...] or you randomly add some amount to each new index [rnd1, rnd1 + rnd2, rnd1 + rnd2 + rnd3...]), randomizing the ordering by shuffling the numbers (click this!), and then simply iterating through the created array.
After getting the calculator application to work I decided to try to create pong. There is a box in the center and two paddles on both ends. The phone is horizontal. I have the box bouncing off the walls and the paddle moves with me moving my finger down. My problem is i want to make it two player and i want to have multiple finger input for the game. I want one finger to move paddle 1 and the other to move paddle 2. So far this is my input code
#Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE: {
// Find the index of the active pointer and fetch its position
float p1y = ev.getY();
if(ev.getX()<300)
{
player1y = p1y;
}
if(ev.getX()>300)
{
player2y = p1y;
}
//player1y = p1y;
invalidate();
break;
}
}
return true;
}
it resides in my surfaceview class. How can i modify the input method or completely get rid of it and change it to accomplish my goal? Also sorry about my variables. Eclipse crashes a lot on me and my laptops touch panel tends to move my cursor so shorter variables seemed viable. p1y is the y of the touch. and player1y and player2y is the y positions of the player1 and player2 paddle.
A MotionEvent can hold multiple pointers. Use getPointerCount() to see how many pointers are touching the screen in the current event. There are alternate versions of getX and getY that take an index from 0-getPointerCount() - 1.
In a more complex app you would want to track fingers by pointer ID, but for something this simple where you are using a cutoff point on the screen you could do something like this in your ACTION_MOVE case:
int pointerCount = ev.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
float x = ev.getX(i);
float y = ev.getY(i);
if (x < 300) {
player1y = y;
} else if (x > 300) {
player2y = y;
}
}
This post from the Android Developers Blog might help if you'd like more information: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
Can anyone help please.
Im writing a small Android game where the player is able to select a "barrier" and drag it accross the screen with their finger. I have the barriers drawn on the screen and I am able to drag it accross the screen.
My problem however is when I add more than 1 barrier, eg 3 barriers, and drag a barrier accross the screen, they all drag and they all drag to the same position. That is to say they all lie on top of each other, making it look as though there is only 1 barrier.
Here is my code, can anyone please tell me where I am going wrong/explain where I am going wrong.
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener {
// Initialising the Barrier
private Barrier barrier[] = new Barrier[3];
// The Main Game Panel
public MainGamePanel(Context context) {
super(context);
// Adding a call-back (this) to the surfaceHolder to intercept events
getHolder().addCallback(this);
// Creating the Game items
// The starting coordinates of the Barrier
int x = 30;
int y = 270;
barrier[0] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.blue_barrier), x, y);
barrier[1] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.green_barrier), x + 15, y);
barrier[2] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.pink_barrier), x + 30, y);
// Create the Game Loop Thread
thread = new MainThread(getHolder(), this);
// Make the GamePanel focusable so it can handle events
setFocusable(true);
}
// Handles the touch events
public boolean onTouchEvent(MotionEvent event)
{
int eventAction = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
switch (eventAction)
{
// Touch down so check if finger is on Barrier
case MotionEvent.ACTION_DOWN:
if (x > barrier[0].getX() && x < barrier[0].getX() + 8
&& y > barrier[0].getX() && y < barrier[0].getY() + 8)
{
barrier[0].isTouched();
}
else if (x > barrier[1].getX() && x < barrier[1].getX() + 8
&& y > barrier[1].getX() && y < barrier[1].getY() + 8)
{
barrier[1].isTouched();
}
else if (x > barrier[2].getX() && x < barrier[2].getX() + 8
&& y > barrier[2].getX() && y < barrier[2].getY() + 8)
{
barrier[2].isTouched();
}
break;
// Touch-drag with the Barrier
case MotionEvent.ACTION_MOVE:
// Move the Barrier the same as the finger
for (int i = 0; i < barrier.length; i++)
{
if (barrier[i] == barrier[0])
{
barrier[0].setX(x);
barrier[0].setY(y);
} // end if
else if (barrier[i] == barrier[1])
{
barrier[1].setX(x);
barrier[1].setY(y);
}
else if (barrier[i] == barrier[2])
{
barrier[2].setX(x);
barrier[2].setY(y);
} // end else if
} // end for
break;
case MotionEvent.ACTION_UP:
// Finger no longer on Barrier - Do Nothing
break;
}
return true;
}
// Render - Draws the Game Item Bitmaps to the screen
public void render(Canvas canvas)
{
// Set the background to white
canvas.drawColor(Color.WHITE);
barrier[0].draw(canvas);
barrier[1].draw(canvas);
barrier[2].draw(canvas);
}
// Update
// This is the Game's update method
// It iterates through all the Objects and calls their update() methods (if they have one)
public void update()
{
} // end update
In your barrier move code, you aren't checking a particular barrier is touched, so you're moving all of them to the same coordinates. The following loop does exactly the same thing as your current code:
// Move the Barrier the same as the finger
for (int i = 0; i < barrier.length; i++)
{
barrier[i].setX(x);
barrier[i].setY(y);
} //end for
To fix this, you need to check if the current barrier in the loop is the one that was touched, and can change that whole loop to something like:
// Move the Barrier the same as the finger
for (int i = 0; i < barrier.length; i++)
{
if (barrier[i].isTouched())
{
barrier[i].setX(x);
barrier[i].setY(y);
} // end if
} // end for
You would then need to make sure that you un-set the touched property in the ACTION_UP section. If you post your Barrier class definition I can probably help more.
I'm pretty sure your problem could be solved by making the barriers three separate objects instead of in an array.
As already pointed out, your code in handling the move is wrong because it moves all the Barriers to the same X,Y when it should only move the one that is touched.
Also, you are never resetting the isTouched on the objects in the Action up. When the user lifts their finger you should set them all to isTouched == false. If you don't do that then once you touch one it will always move to the X,Y.
I want to know how I can detect child views if I move a view from one ViewGroup to another ViewGroup, particularly when doing a touch event. Is there a method I can call that will let me know which views i'm "hovering" over?
What I'm doing right now is when I detect an ACTION_MOVE event on my view i'm raising it to the top level parent so that it can move and be drawn within the entire window ( and not just inside it's original parent bounds ), then I want to move the view across to a different ViewGroup and on ACTION_UP attach the view to that ViewGroup.
Inspired by Ami's response, but discovering that MotionEvent#getX()/getY() along with View#getTop()/etc return coordinates wrt the parent View, I ended up doing the following below to operate in screen coordinates, allowing me to work across ViewGroups:
private boolean inRegion(float x, float y, View v) {
v.getLocationOnScreen(mCoordBuffer);
return mCoordBuffer[0] + v.getWidth() > x && // right edge
mCoordBuffer[1] + v.getHeight() > y && // bottom edge
mCoordBuffer[0] < x && // left edge
mCoordBuffer[1] < y; // top edge
}
whose usage inside an OnTouchListener is e.g.:
boolean inside = inRegion(event.getRawX(), event.getRawY(), targetView);
I think I found a simpler way to do this.
Create an ArrayList of possible targets
Call this method from your touch event, supplying your targets list and the coords
private View findView(float x, float y, ArrayList<View> targets)
{
final int count = targets.size();
for (int i = 0; i < count; i++) {
final View target = targets.get(i);
if (target.getRight() > x && target.getTop() < y
&& target.getBottom() > y && target.getLeft() < x) {
return target;
}
}
return null;
}
I found Sebastian Roth's answer very helpful with resources, but since it wasn't really an answer to my question, I thought I'd share what I came up with.
Here is the code I use to detect views ( only views that will accept a drop that is ) given a coordinate on the screen.
private DropView findDropTarget( int x, int y, int[] dropCoordinates ){
final Rect r = mRectTemp;
final ArrayList<DropView> dropTargets = ((main) context).getBoardDropTargets();
final int count = dropTargets.size();
for (int i=count-1; i>=0; i--) {
final DropView target = dropTargets.get(i);
target.getHitRect(r);
target.getLocationOnScreen(dropCoordinates);
r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
if (r.contains(x, y)) {
dropCoordinates[0] = x - dropCoordinates[0];
dropCoordinates[1] = y - dropCoordinates[1];
return target;
}
}
}
Ok, first off mRectTemp is just an allocated Rectangle so you don't have to keep creating new ones ( I.E. final Rect r = new Rect() )
The next line dropTargets is a list of views that will accept a drop in my app.
Next I loop through each view.
I then use getHitRect(r) to return the screen coordiantes of the view.
I then offset the coordiantes to account for the notification bar or any other view that could displace the coordiantes.
finally I see if x and y are inside the coordinates of the given rectangle r ( x and y are the event.rawX() and event.rawY() ).
It actually turned out to be simpler then expected and works very well.
Read this:
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)
http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)
I had implemented a Drag and Drop using that method.
I also highly recommend a read of the HomeScreen sourcecode, which contains this thing (kind of):
https://android.googlesource.com/platform/packages/apps/Launcher2