下面这个应用是这样的,手机在屏幕上画一条线服务器收到数据后也同步的画一条一样的线。主要是应用了SOCKET链接,因为最后版本还没有弄好所以代码还有些要修改的(比如SOCKET还没有关闭。。)效果的话感觉wifi条件下表现非常不错,3G信号下也马马虎虎,但是2G网络下比较坑爹,SOCKET数据包延迟比较大,服务器不能及时响应,都是一下子收到之前几秒发的数据的,看看怎么优化吧。
package com.example.mytest;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;public class MainActivity extends Activity {private final String ip_address = "***";private ImageView iv;private Bitmap bm;private Canvas canvas;private Paint paint;private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.iv);paint = new Paint();paint.setColor(Color.BLACK);paint.setStrokeWidth(5);new Thread(new MyClass()).start();iv.setOnTouchListener(new OnTouchListener() {private int startX = 0;private int startY = 0;private int newX = 0;private int newY = 0;private boolean flag = true;@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {// 手指第一次接触屏幕case MotionEvent.ACTION_DOWN:bm = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(),Bitmap.Config.ARGB_8888);canvas = new Canvas(bm);canvas.drawColor(Color.WHITE);startX = (int) event.getX();startY = (int) event.getY();break;// 手指在移动case MotionEvent.ACTION_MOVE:// 降低采样频率if (flag == true) {flag = false;} else {flag = true;break;}newX = (int) event.getX();newY = (int) event.getY();canvas.drawLine(startX, startY, newX, newY, paint);iv.setImageBitmap(bm);String str = startX + "," + startY + "," + newX + ","+ newY + "$";Message msg = Message.obtain();msg.obj = str;handler.sendMessage(msg);startX = newX;startY = newY;break;// 手指移开屏幕case MotionEvent.ACTION_UP:break;default:break;}return true;}});}public class MyClass implements Runnable {public void run() {Looper.prepare();try {handler = new Handler() {Socket s = new Socket(ip_address, 10086);OutputStream stream = s.getOutputStream();@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);String str = (String) msg.obj;byte[] data = str.getBytes();try {stream.write(data);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}};} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Looper.loop();}}
}