开发者

Android : Display coordinates x,y using drawtext when ontouch

I want to display the coordinates of nodes which is placed on the view with the ontouch event, the coordinates are to be placed beside the circle nodes,temporarily i'm using (0,0) but i have no idea how to implement the real coordinates on the nodes properly,need some help.thanks

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;
}

}


Ahh, you don't really have a question about android. You have a java question. You want to know how to convert floats to strings.

This link will show you all you need to know: http://download.oracle.com/javase/tutorial/java/data/converting.html

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜