I've been fighting this problem for a while now and thought I'd ask for some advice.
The purpose of this code is to generate a Json file with the information about how the user moved in the app. Everytime I get a touch event, I write to an arraylist of Json objects with the following information:
{"action", "timestamp", "index", "y", "x"}
The action is what kind of action it is (down, up, etc.), timestamp is the time from the start of the touch recording, index is supposed to be what finger, (x,y) is the position of the finger.
The problem however, is that I can't seem to make it work with multi-touch. Eventually this Json file will be used to make a video, showing the movements of the user. One finger works perfectly but when there's several fingers involved, I need to have the information about what finger this specific data comes from.
Currently, it tracks a finger and if I use another finger, it will start to track that instead, ignoring the first one.
public void writeTouchJSON(MotionEvent event, float time) {
int pointerCount = event.getPointerCount();
JSONObject object = new JSONObject();
try {
String action;
action = new String();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
action = "down";
break;
case MotionEvent.ACTION_UP:
action = "up";
break;
default:
action = "" + event.getActionMasked();
}
for (int i = 0; i < pointerCount; i++) {
//int index = event.getPointerId(i);
object.put("x", (int) event.getX(i)); // X-pos of finger
object.put("y", (int) event.getY(i)); // Y-pos of finger
object.put("timestamp", (int) (0.000001*(time - timeAtAppLaunch))); // Time since recording started
object.put("action", action); // What kind of action ("up", "down" or a constant)
object.put("index", i + 1); // What finger
}
Log.d("Wrote JSON to list", object.toString());
} catch (JSONException e) {
e.printStackTrace();
Log.d("ERROR: json.", ""+e);
}
jsonobjects.add(object);
}
I hope the code attached is enough. In the method
onTouchEvent(MotionEvent event)
I check if the app is recording, if it is I send event to logTouchEvents(event) which eventually ends up in the writeTouchJSON method.
My personal theory is that I mess up the meaning of pointers. From what I've understood a pointer is a finger? So an event has an array of pointers where 0 will give you information about the first finger who touched the screen, 1 about the second, etc. but maybe this is wrong?
If you're interested in why I'm doing this it's because I'm making a user experience testing apk for school where you can record how a person navigates through your app and get a video displaying this.
Thank you for your time! Appreciate any help I can get!
On the for loop each variable is getting overwriten by its previous value. You should create an array with one element for each pointer (finger).
Related
I need to draw graph like the image i have uploaded , i am using MP chart library and develop a graph but want to customize it according to my requirement but not able to find solution for my requirements my basic requirement is for x axis i want to show custom values at axis like 5-11 12-18 but i am passing value to x axis like this
private ArrayList<String> setXAxisValues() {
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("10");
xVals.add("20");
xVals.add("30");
xVals.add("30.5");
xVals.add("40");
return xVals;
}
So it is showing x values like this 10 20 30 so on so i want my graph to be built upon using these x value which is happening right now but want to show custom value at bottom like 5-11 etc and this value is dynamic coming from Api response so please help me about this , Waiting for positive and early response Thanks in Advance
To format the x-axis values, you should use the setValueFormatter method which takes a callback interface were you have to implement the getFormattedValue method which is called before drawing the x-axis value.
xAxis.setValueFormatter((value, axis) -> {
String res = "";
try {
// get current position of x-axis
int currentPosition = (int) value;
// check if position between array bounds
if (currentPosition > -1 && currentPosition < maxSize) {
// get value from formatted values array
res = xVals.get(currentPosition);
}
} catch (Exception e) {
// handle exception
}
return res;
});
I have created a project where users voice get replicated http://androidsourcecode.blogspot.in/2013/07/android-audio-demo-audiotrack.html
and I want to perform animation according to voice level, I got to know about voice level through this tutorial http://www.doepiccoding.com/blog/?p=195#comment-45
now I want to integrate both of this code so that at certain voice level animation should get played at the same time animation should played ...
I am stuck here since long time...
I am calling a animation method at the time of writing track and that animation perform all the time which I don't want.
Please help me in this
So Finally I got the solution:
animpo.speak(i_Ravan);
while (isRecording) {
double lastLevel =1;
num = record.read(lin, 0, 2160);
double sumLevel = 0;
for (int i=0;i<num;i++){
sumLevel += lin[i];
}
lastLevel = Math.abs((sumLevel/num));
String ll = String.valueOf(lastLevel);
track.write(lin, 0, num);
Log.d("LastLevel",ll);
if(lastLevel > 5){
ClassA.speak(image); //Method which perform animation
}
}
An easy way to make an android calculator would be to have 3 separate edit text boxes and have the user in put a number, a function, and then another number like 3 + 3. This would make it easier for the app dev to store the number(s) and function(s) and perform a calculation.
Now... my calculator app has the ability to out put all the input real-time, the down side is that when I retrieve what's in the input box, i retrieve it as string (to make sure i include all the functions input-ed). I know how to retrieve numbers (by using int parse) but how do I retrieve the functions such as + - / * ? (They're the main bit!! :O). Any help would me much appreciated thanks :)
Try to use a switch that analyze and identify the correct operation. Something like this:
(I suppose the content of function EditText in a string named functionSign
...
switch(functionSign)
{
case "+": return op1+op2;
case "-": return op1-op2;
...
EDIT 2:
I suppose that user can put only the functions simbols + - / * and the operations are organized in a method:
public double calculate()
{
String operations= inputEditText.getText().toString();
StringTokenizer st= new StringTokenizer(operations);
//to calculate in input must have at last one operation and two operands
//the first token must be a number (the operation scheme is (number)(function)(numeber)...)
double result=Double.parseDouble(st.nextToken());
while(st.hasMoreTokens())
{
String s=st.nextToken();
if(s.equals("+"))
result += Double.parseDouble(st.nextToken());
else if(s.equals("-"))
result -= Double.parseDouble(st.nextToken());
else if(s.equals("*"))
result *= Double.parseDouble(st.nextToken());
else if(s.equals("/"))
result /= Double.parseDouble(st.nextToken());
else
throw new Exception();
}
return result;
}
This code is a really simple example, you must be sure that the user don't try to calculate something incomplete like:
3 + 3 -
/ 3 * 5
and similar. What the user should be able to do is your decision
You can get the operator as a string and use if statements to determine what to do:
String operator=operatorEditText.getText().toString();
if (operator.equals("+")){
//addition code here
}
else if (operator.equals("-")){
//subtraction code here
}
...
I am currently building a game in AndEngine, but my collision detection seems a bit off. It works.. most of the time, but seems like when there is a collision with one object, it wont do the other. It's hard to explain and it's very unpredictable.
for (int i = 0; i < rManager.carArray.length; i++)
{
if (rManager.getInstance().snowArray[0].getSnowSprite().collidesWith(rManager.getInstance().carArray[i].getCarSprite()))
{
Log.e("SNOW", "snow 0 collided with " + rManager.getInstance().carArray[i].ToString());
rManager.getInstance().carArray[i].setCarSpeed(0.1f);
break;
}
if (rManager.getInstance().iceArray[0].getIceSprite().collidesWith(rManager.getInstance().carArray[i].getCarSprite()))
{
Log.e("ICE", "ice 0 collided with " + rManager.getInstance().carArray[i].ToString());
rManager.getInstance().carArray[i].setCarSpeed(1f);
break;
}
else
{
rManager.getInstance().carArray[i].setCarSpeed(0.5f);
}
}
Is there anything wrong with my code? Currently, both enemy arrays only have 1 element. That is why I am only checking 0.
It is because of the "break" keyword. When that is called it "breaks out" of the surrounding for loop. So if the first if statement detects a collision, it will not ever call the second if statement.
I am using a Pool to manage Bullets in my game. The only problem is when a Bullet is obtained from the pool having just been recycled because it was involved in a collision, although it's Body's location is reset using Body.setTransform() when it is initialized, the Bullet's Sprite's location (which is used to detect collisions using Sprite.collidesWith(otherSprite)) is not reset quick enough (as it's updated in a Physics thread). This means the newly created bullet causes a collision the instant it is created, resulting in a single bullet causing more than one collision.
I tried calling Bullet.sprite.setPosition(0,0) when it is initialized, but this clearly interferes, as Bullets fail to be displayed at all with that line of code in place. What should I do to prevent this problem?
Bullet Creation:
bullets[bulletCounter] = bulletPool.obtainPoolItem();
bullets[bulletCounter].getBody().setTransform(shipBody.getTransform().getPosition(),0);
bullets[bulletCounter].getBody().setLinearVelocity(shipBody.getLinearVelocity());
bullets[bulletCounter].activate();
Collision Detection:
for(int i = 0; i < BULLET_MAX; i++){
if(bullets[i] != null && bullets[i].isActive()){
for(int j = 0; j < enemies.size(); j++){
//check for collision!
if(bullets[i].getSprite().collidesWith(enemies.get(j).getSprite())){
//-snip-
break;
}
}
}
}
I'd be interested to see what you're doing around the time you invoke onHandleObtainItem to get a bullet from the pool.
Would it not make sense to set the sprite position to some offscreen value when you invoke onHandleRecycleItem, something like:
#Override
protected void onHandleRecycleItem(final Bullet pBullet) {
pBullet.disable();
pBullet.getSprite().setPosition(-1, -1);
}