안드로이드 개발 질문/답변
(글 수 45,052)
public class SoketChat extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView vw = new MyView(this);
setContentView(vw);
// -- Thread for Connection
Thread cThread = new Thread(){public void run(){
try{
connect(IP,PORT);
} catch(Exception e){} }
};
cThread.start();
}
protected class MyView extends View{
public MyView(Context context){
super(context);
}
public void onDraw(Canvas canvas){
Paint Pnt = new Paint();
Pnt.setColor(Color.BLUE);
canvas.drawColor(Color.WHITE);
canvas.drawCircle(intxdata,intydata,2,Pnt);
}
}
public void connect(String ip, int port){
int size;
byte[] w = new byte[1024];
txtReceive="";
try{
socket = new Socket(ip,port);
if(socket != null) {
in = socket.getInputStream();
out = socket.getOutputStream();
PrintWriter pw = new PrintWriter( new OutputStreamWriter( out ) );
pw.println(LOGIN_ID);
pw.flush() ;
while(socket != null && socket.isConnected()){
size = in.read(w);
if(size<=0)continue;
txtReceive = new String(w,0,size,"UTF-8");
handler.post(new Runnable(){
public void run() {
StringTokenizer codeName = new StringTokenizer(txtReceive, ",");
xdata = codeName.nextToken();
ydata = codeName.nextToken();
intxdata = Integer.parseInt(xdata);
intydata = Integer.parseInt(ydata);
}
});
}
}
} catch (Exception ex) {Log.e("socket",ex.toString());}
}intxdata와 intydata안에 x,y좌표가 들어가있는데
이걸 ondraw함수를 이용해서 좌표가 바뀔때마다
계속적으로 화면에 동그란점으로 표시하고싶습니다.
ondraw함수를 어떻게 사용해야 연속적으로 표시가 가능한가요?




MyView 의 변수를 로컬이 아닌 멤버로 빼주시고, 좌표값이 바뀔때마다 vw.invalidate()나 vw.postInvalidate()를 호출해보세요...