手机端
MainActivity
package com.example.xdsk;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<Integer> data = new ArrayList<Integer>();//存放服务器获得数据
private String ips="10.23.42.16";
private int dk=6543;
private String l="";
private TextView text,zt,xt;
private String key ="0";//表示当前是否连接状态
private Handler hd ;
private Button bt1;//开关连接按钮
private int js=0, MaxDataSize;//max最大的数据数
private drawplan view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
LinearLayout root =(LinearLayout)findViewById(R.id.plan);
view= new drawplan(this, null);
MaxDataSize=view.getMaxData();
root.addView(view);
bt1= (Button)findViewById(R.id.bt1);
text = (TextView)findViewById(R.id.t1);
text.setText("ip:"+ips+" 端口:"+dk);
zt=(TextView)findViewById(R.id.t2);
xt=(TextView)findViewById(R.id.t3);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(key.equals("0")){
key="1";
Star();
bt1.setText("断开连接");
}else{
key="0";
bt1.setText("开始连接");
}
Log.e("btn", "-----------");
}
});
hd = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case 1:
if(js==0){
String tt[]=l.split("\\|");
zt.setText(tt[0]);
xt.setText(tt[1]);
draw(tt[1]);
}else if(js==3){
js=0;
}else{
js++;
}
break;
default:break;
}
}
};
}
protected void draw(String tt) {
if(data.size() >= MaxDataSize){
data.remove(0);//如果存入的数据超过最大值那么就移除第一个
}
int a=Integer.parseInt(tt);
if(a>=150){a=150;}
data.add(a/10);
view.adddata(data);//刷新画板
}
public void Star(){
final Socket s = new Socket();
try {
s.connect(new InetSocketAddress(ips,dk),0);
} catch (IOException e1) {
e1.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
String m ;
while(true){
try {
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(key);
ObjectInputStream ois =new ObjectInputStream(s.getInputStream());
m=(String)ois.readObject();
l=m;
Message mes = hd.obtainMessage(1);
hd.sendMessage(mes);
Thread.sleep(100);
if(key.equals("0")){
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
Log.e("end", "end");
}
}
//画图
Drawplan
package com.example.xdsk;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
*
* [url=home.php?mod=space&uid=187600]@author[/url] BLQ-SSBN
* 继承View实现界面下方画图
*/
public class drawplan extends View {
private int XPoint = 40;
private int YPoint = 485;
private int XScale = 20; //刻度长度
private int YScale = 28;
private int XLength = 1000;
private int YLength = 420;
private int MaxDataSize = XLength / XScale;
private List<Integer> data = new ArrayList<Integer>(); //list存放从服务器获得的数据
//返回存储数据个数
public int getMaxData(){
return MaxDataSize;
}
public drawplan(Context context, AttributeSet attrs) {
super(context, attrs);
}
//画界面
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true); //去锯齿
paint.setColor(Color.BLUE); //画笔颜色
//画y轴
canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, paint);
//画y轴上的尖尖头
canvas.drawLine(XPoint, YPoint - YLength, XPoint - 3, YPoint-YLength + 6, paint);
canvas.drawLine(XPoint, YPoint - YLength, XPoint + 3, YPoint-YLength + 6 ,paint);
//画x轴
canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, paint);
//这里将list里的数据画出来
if(data.size() > 1){
for(int i=1; i<data.size(); i++){
canvas.drawLine(XPoint + (i-1) * XScale, YPoint - data.get(i-1) * YScale,
XPoint + i * XScale, YPoint - data.get(i) * YScale, paint);
}
}
}
//调用adddata 将数据传入并调用界面刷新
public void adddata(List<Integer> data2) {
this.data=data2;
drawplan.this.invalidate();
}
}
|