Where/how 监听蓝牙时放置绘图函数
Where/how to place drawing functions when listening bluetooth
我正在使用蓝牙串行协议从微控制器成功接收数据,例如,在本教程中所述:
http://solderer.tv/data-transfer-between-android-and-arduino-via-bluetooth/
我能够在 textView 中显示我收到的值,但是一旦我尝试通过添加长度取决于值的行以图形方式显示这些值,应用程序就会崩溃。
我认为我可以使用绘图功能本身,因为我可以在应用程序启动时绘制静态图片:
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bg);
Paint paint = new Paint();
paint.setColor(Color.parseColor(#6b8728));
paint.setXfermode(null);
canvas.drawLine(x, y, x2, y2, paint);
也许最后我应该用另一种方式。
总之,介绍到这里,我的问题来了。当我在数据监听线程中实现这些绘图功能时,应用程序崩溃了。
因此我不知道如何进行画线:需要新线吗?(以及如何做?),必须在特定部分进行绘画?具体要使用的功能?...
我希望我的问题很清楚,否则我会很乐意添加更多详细信息。
预先感谢您的帮助。
亲切的问候,
西尔万
编辑:
添加我的函数:
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
final TextView t = (TextView)findViewById(R.id.textView);
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
Toast.makeText(MainActivity.this, data,
Toast.LENGTH_SHORT).show();
handler.post(new Runnable()
{
public void run() {
t.setText(data);
String[] separated = data.split(":");
separated[1].trim();
if ((separated[0].length() != 0)&& (separated[1].length() != 0)) {
t.append("\n" + separated[0]);
t.append("\n" + separated[1]);
t.append("\ntest");
String s_angle = separated[0];
String s_value = separated[1];
final Integer angle = Integer.parseInt(s_angle);
final Integer value = Integer.parseInt(s_value)/1000;
// Crashing here
Paint paint = new Paint();
paint.setColor(Color.parseColor("#6b8728"));
int width = canvas.getWidth()/2;
int height = canvas.getHeight()/2;
canvas.drawLine(width,height,150,50, paint);
}
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
else
{
//t.append(bytesAvailable+"\n");
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
所有 UI 相关活动必须在 UI/Main 线程中完成。方法之一:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//your UI code goes here
}
});
我用过 runOnUiThread:
runOnUiThread(new Runnable() {
@Override
public void run() {
Paint paint = new Paint();
paint.setColor(Color.parseColor("#6b8728"));
//Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
//Canvas canvas = new Canvas(bg);
int width = canvas.getWidth() / 2;
int height = canvas.getHeight() / 2;
canvas.drawLine(width, height, 150, 50, paint);
}
});
我正在使用蓝牙串行协议从微控制器成功接收数据,例如,在本教程中所述:
http://solderer.tv/data-transfer-between-android-and-arduino-via-bluetooth/
我能够在 textView 中显示我收到的值,但是一旦我尝试通过添加长度取决于值的行以图形方式显示这些值,应用程序就会崩溃。
我认为我可以使用绘图功能本身,因为我可以在应用程序启动时绘制静态图片:
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bg);
Paint paint = new Paint();
paint.setColor(Color.parseColor(#6b8728));
paint.setXfermode(null);
canvas.drawLine(x, y, x2, y2, paint);
也许最后我应该用另一种方式。
总之,介绍到这里,我的问题来了。当我在数据监听线程中实现这些绘图功能时,应用程序崩溃了。 因此我不知道如何进行画线:需要新线吗?(以及如何做?),必须在特定部分进行绘画?具体要使用的功能?...
我希望我的问题很清楚,否则我会很乐意添加更多详细信息。
预先感谢您的帮助。
亲切的问候,
西尔万
编辑:
添加我的函数:
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
final TextView t = (TextView)findViewById(R.id.textView);
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
Toast.makeText(MainActivity.this, data,
Toast.LENGTH_SHORT).show();
handler.post(new Runnable()
{
public void run() {
t.setText(data);
String[] separated = data.split(":");
separated[1].trim();
if ((separated[0].length() != 0)&& (separated[1].length() != 0)) {
t.append("\n" + separated[0]);
t.append("\n" + separated[1]);
t.append("\ntest");
String s_angle = separated[0];
String s_value = separated[1];
final Integer angle = Integer.parseInt(s_angle);
final Integer value = Integer.parseInt(s_value)/1000;
// Crashing here
Paint paint = new Paint();
paint.setColor(Color.parseColor("#6b8728"));
int width = canvas.getWidth()/2;
int height = canvas.getHeight()/2;
canvas.drawLine(width,height,150,50, paint);
}
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
else
{
//t.append(bytesAvailable+"\n");
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
所有 UI 相关活动必须在 UI/Main 线程中完成。方法之一:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//your UI code goes here
}
});
我用过 runOnUiThread:
runOnUiThread(new Runnable() {
@Override
public void run() {
Paint paint = new Paint();
paint.setColor(Color.parseColor("#6b8728"));
//Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
//Canvas canvas = new Canvas(bg);
int width = canvas.getWidth() / 2;
int height = canvas.getHeight() / 2;
canvas.drawLine(width, height, 150, 50, paint);
}
});