Friday, 6 September 2013

ACTION_UP is triggered before finger is removed from screen

ACTION_UP is triggered before finger is removed from screen

I am playing around with onTouchEvent and have written some simple code
that prints to the system output when the screen is being touched and when
the screen isn't using ACTION_DOWN and ACTION_UP. My plan is to use a
boolean to indicate when its being pressed and when its not. set boolean
to true when its first touched and set it to false when the finger is
removed.
When I run my code however both the DOWN and UP code is run when I touch
the screen and leave my finger on it. When I remove my finger the UP code
is run again.
Can anyone see where I am going wrong and explain it to me.
My Code
package com.mr.mwood.thumbinput;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
class SampleView extends SurfaceView {
protected int splashTime = 3000;
int timer =0;
private final SurfaceHolder surfaceHolder;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint text = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint HighlightedLetter = new
Paint(Paint.ANTI_ALIAS_FLAG);
boolean screenIsBeingTouched = false;
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
surfaceHolder = getHolder();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
text.setColor(Color.RED);
text.setTextSize(50);
text.setTextAlign(Align.CENTER);
HighlightedLetter.setColor(Color.RED);
HighlightedLetter.setTextSize(70);
HighlightedLetter.setTextAlign(Align.CENTER);
}
public void WriteText(){
//Draw Debug Text
if (screenIsBeingTouched == true){
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawText("This is a test to see if the text
appears", 100, 100, HighlightedLetter);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (screenIsBeingTouched == true){
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawText("This is a test to see if the text
appears", 100, 100, HighlightedLetter);
surfaceHolder.unlockCanvasAndPost(canvas);
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:{
System.out.println("Screen is being Touched");
screenIsBeingTouched = true;
}
case MotionEvent.ACTION_UP:{
screenIsBeingTouched = false;
System.out.println("Screen is NOT being Touched");
}
}
return true;
}
}
}
The System Output
Screen is being touched
Screen is NOT being touched
[Surface Touch Event]..........
Screen is NOT being touched

No comments:

Post a Comment