Android:在ontouch上使用drawtext显示坐标x,y

时间:2011-09-13 02:45:06

标签: android

我想用ontouch事件显示放置在视图上的节点的坐标,坐标放在圆形节点旁边,暂时我正在使用(0,0)但是我不知道如何正确实现节点上的真实坐标,需要一些帮助。谢谢

public class DrawView extends View implements OnTouchListener {
List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();

public DrawView(Draw context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);

}
@Override
public void onDraw(Canvas canvas) {
    Paint rectanglePaint = new Paint();
    rectanglePaint.setColor(Color.WHITE);
    Paint layoutColor = new Paint();
    layoutColor.setColor(Color.BLACK);      
    canvas.drawRect(new Rect(10,10,465,800), rectanglePaint);
    canvas.drawLine(300, 10, 300, 800, layoutColor);
    canvas.drawLine(0, 300, 465, 300, layoutColor);


//  Paint textPaint = new Paint();
//  textPaint.setColor(Color.CYAN);
//  textPaint.setTextSize(30);
//  canvas.drawText("Room", 220, 400, textPaint);




    for (Point point : points) {


        canvas.drawText("(0,0)", point.x+5, point.y+5, paint);
        canvas.drawCircle(point.x, point.y, 5, paint);
      //  Log.d(TAG, "Painting: "+point);
    }
}
final TextView textView = ( TextView )findViewById(R.id.textView1);
public boolean onTouch(View view, MotionEvent event) {
    // return super.onTouchEvent(event);
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    points.add(point);
    invalidate();



    return true;
}

}

1 个答案:

答案 0 :(得分:0)

啊,你真的没有关于android的问题。你有一个java问题。您想知道如何将浮点数转换为字符串。

此链接将向您显示您需要知道的所有内容:http://download.oracle.com/javase/tutorial/java/data/converting.html

String s = "(" + point.x + "," + point.y + ")";