首先,我对 Java 编程语言非常陌生,我尝试搜索我想要的东西,但没有什么是具体的。其次,我正在尝试从蓝牙设备绘制动态数据,
Sensorgraph
可以使用应用程序(开源应用程序)简单地绘制数据,但是绘图不是很清楚(我不能放大或缩小,没有 x 或 y 轴......)。因此,我需要另一种方法来尝试绘制数据以更好地显示它。这是SensorGraph
代码:
public class SensorGraph extends Activity {
Button Start, Stop, Pause;
private static final String TAG = "SensorGraph";
// change this to your Bluetooth device address
private static final String DEVICE_ADDRESS = "00:06:66:4B:E2:B7"; //"00:06:66:03:73:7B";
private GraphView mGraph;
private TextView mValueTV;
private ArduinoReceiver arduinoReceiver = new ArduinoReceiver();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get handles to Views defined in our layout file
mGraph = (GraphView)findViewById(R.id.graph);
mValueTV = (TextView) findViewById(R.id.value);
mGraph.setMaxValue(1024);
}
@Override
protected void onStart() {
super.onStart();
// in order to receive broadcasted intents we need to register our receiver
registerReceiver(arduinoReceiver, new IntentFilter(AmarinoIntent.ACTION_RECEIVED));
// this is how you tell Amarino to connect to a specific BT device from within your own code
Amarino.connect(this, DEVICE_ADDRESS);
}
@Override
protected void onStop() {
super.onStop();
// if you connect in onStart() you must not forget to disconnect when your app is closed
Amarino.disconnect(this, DEVICE_ADDRESS);
// do never forget to unregister a registered receiver
unregisterReceiver(arduinoReceiver);
}
/**
* ArduinoReceiver is responsible for catching broadcasted Amarino
* events.
*
* It extracts data from the intent and updates the graph accordingly.
*/
public class ArduinoReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String data = null;
// the device address from which the data was sent, we don't need it here but to demonstrate how you retrieve it
final String address = intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS);
// the type of data which is added to the intent
final int dataType = intent.getIntExtra(AmarinoIntent.EXTRA_DATA_TYPE, -1);
// we only expect String data though, but it is better to check if really string was sent
// later Amarino will support differnt data types, so far data comes always as string and
// you have to parse the data to the type you have sent from Arduino, like it is shown below
if (dataType == AmarinoIntent.STRING_EXTRA){
data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA);
if (data != null){
mValueTV.setText(data);
try {
// since we know that our string value is an int number we can parse it to an integer
final int sensorReading = Integer.parseInt(data);
mGraph.addDataPoint(sensorReading);
}
catch (NumberFormatException e) { /* oh data was not an integer */ }
}
}
}
}
}
图视图代码如下:
public class GraphView extends View {
private Bitmap mBitmap;
private Paint mPaint = new Paint();
private Canvas mCanvas = new Canvas();
private float mSpeed = 1.0f;
private float mLastX;
private float mScale;
private float mLastValue;
private float mYOffset;
private int mColor;
private float mWidth;
private float maxValue = 1024f;
public GraphView(Context context) {
super(context);
init();
}
public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
mColor = Color.argb(192, 64, 128, 64);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
}
public void addDataPoint(float value){
final Paint paint = mPaint;
float newX = mLastX + mSpeed;
final float v = mYOffset + value * mScale;
paint.setColor(mColor);
mCanvas.drawLine(mLastX, mLastValue, newX, v, paint);
mLastValue = v;
mLastX += mSpeed;
invalidate();
}
public void setMaxValue(int max){
maxValue = max;
mScale = - (mYOffset * (1.0f / maxValue));
}
public void setSpeed(float speed){
mSpeed = speed;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
mCanvas.setBitmap(mBitmap);
mCanvas.drawColor(0xFFFFFFFF);
mYOffset = h;
mScale = - (mYOffset * (1.0f / maxValue));
mWidth = w;
mLastX = mWidth;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
synchronized (this) {
if (mBitmap != null) {
if (mLastX >= mWidth) {
mLastX = 0;
final Canvas cavas = mCanvas;
cavas.drawColor(0xFFFFFFFF);
mPaint.setColor(0xFF777777);
cavas.drawLine(0, mYOffset, mWidth, mYOffset, mPaint);
}
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
}
}
我需要更改的部分主要是传感器读数并将其绘制在另一个图表上!非常感谢你,我很感激任何帮助。如果您有任何问题,请告诉我。