За основу по тач событиям взял урок 102:
http://startandroid.ru/ru/uroki/vse-uro ... anija.html
За основу по канвасу использовался урок 141:
http://startandroid.ru/ru/uroki/vse-uro ... anvas.html
В итоге получил:
Код: Выделить всё
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawView(this));
}
//...
//Класс DrawView не менял, полностью идентичен тому что в уроке 141
//...
class DrawThread extends Thread implements View.OnTouchListener {
float x;
float y;
String sDown="";
String sMove="";
String sUp="";
@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // нажатие
sDown = "Down: " + x + "," + y;
sMove = ""; sUp = "";
break;
case MotionEvent.ACTION_MOVE: // движение
sMove = "Move: " + x + "," + y;
break;
case MotionEvent.ACTION_UP: // отпускание
case MotionEvent.ACTION_CANCEL:
sMove = "";
sUp = "Up: " + x + "," + y;
break;
}
return true;
}
private boolean running = false;
private SurfaceHolder surfaceHolder;
public DrawThread(SurfaceHolder surfaceHolder) {
this.surfaceHolder = surfaceHolder;
}
public void setRunning(boolean running) {
this.running = running;
}
@Override
public void run() {
Canvas canvas;
Paint BackgroundStyle = new Paint();
Paint TextStyle = new Paint();
BackgroundStyle.setStyle(Paint.Style.FILL);
BackgroundStyle.setColor(Color.rgb(0, 0, 0));
TextStyle.setColor(Color.rgb(255, 255, 255));
TextStyle.setTextSize(50);
TextStyle.setTextAlign(Paint.Align.CENTER);
while (running) {
canvas = null;
try {
canvas = surfaceHolder.lockCanvas(null);
if (canvas == null)
continue;
//вывод координат события
canvas.drawColor(Color.BLACK);
canvas.drawText(sDown, canvas.getWidth() / 2, canvas.getHeight() / 2 - 100, TextStyle);
canvas.drawText(sMove, canvas.getWidth() / 2, canvas.getHeight() / 2, TextStyle);
canvas.drawText(sUp, canvas.getWidth() / 2, canvas.getHeight() / 2 + 100, TextStyle);
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
Код: Выделить всё
...
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setOnTouchListener(this);
setContentView(tv);
}
...