android get x-y co-ordinates from a Path
In my app I have an activity where I draw paths in canvas.and also I put all the paths into an ArrayList<Path>.
ArrayList<Path>
to another class to draw the same path in different canvas.
So there instead of using directly that Path I want to convert each Path to x,y Co-ordinates and from that I able to draw all path.
How can i do that? part of my code is below,private ArrayList<Path> _graphics = new ArrayList<Path>();
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println("i'm in ACTION_DOWN ");
path = new Path();
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
//_graphics.add(path);
path.addCircle(event.getX(), event.getY(), 2,
Path.Direction.CCW);
}
else if (event.getAction() == MotionEvent开发者_如何学JAVA.ACTION_MOVE) {
System.out.println("i'm in ACTION_MOVE ");
path.lineTo(event.getX(), event.getY());
path.moveTo(event.getX(), event.getY());
_graphics.add(path);
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("i'm in ACTION_UP ");
path.lineTo(event.getX(), event.getY());
}
return true;
}
}
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
if (onSelect == true) {
onSelect = false;
_graphics.clear();
}
for (Path path : _graphics) {
canvas.drawPath(path, mPaint);
}
}
_graphics
is an ArrayList<Path>
.
and another question is I also want to pass this Arraylist<path>
through Socket
, for that i have to convert this path to byte[]
. Is it possible to covert into byte[]?
Thank you
I know this is not exactly what your looking for but this should help. It will display the x and y.
package com.mike.touch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
public class TouchActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("touch screen");
setContentView(tv);
}
@Override
public boolean onTouchEvent(MotionEvent event){
TextView tv = new TextView(this);
tv.setText("x="+event.getX()+" y="+event.getY());
setContentView(tv);
return super.onTouchEvent(event);
}
}
精彩评论