我目前正在构建一个Android应用程序,该应用程序在连接到信标时会显示信息。我需要根据onBeaconServiceConnect()
事件来更新文本和位置。由于初始活动已经由onDraw方法绘制,因此我不能依靠它进行更新。
我试图向Activity添加一个TextView并使Canvas对象公开,但都没有帮助我。
CustomView:
public class CustomView extends View {
private Rect rectangle,
positionRect;
private Paint paint,
paintPoint,
textPaint,
positionPaint;
public CustomView(Context context) {
super(context);
int x = 100, y= 100, sideLength = 100;
// create a rectangle that we'll draw later
rectangle = new Rect(x, y, sideLength, sideLength);
positionRect = new Rect(400, 540, 360, 590);
// create the Paint and set its color
paint = new Paint();
paint.setColor(Color.GRAY);
paintPoint = new Paint();
paintPoint.setColor(Color.RED);
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(40);
positionPaint = new Paint();
positionPaint.setColor(Color.GREEN);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.drawRect(rectangle, paint);
double[][] positions = new double[][] { { 0, 0 }, { 0, 10 }, { 10, 10 }, { 10, 0 } };
double[] distances = new double[] { 8.06, 13.97, 23.32, 15.31 };
int amountOfBeacons = positions.length;
for(int i = 0; i < amountOfBeacons; i++){
if (i == 0){
canvas.drawCircle(100,100, 20, paintPoint);
canvas.drawText("Beacon 1", 100, 60, textPaint);
}
else if (i == 1){
canvas.drawCircle(100,1000, 20, paintPoint);
canvas.drawText("Beacon 2", 100, 1060, textPaint);
}
else if (i == 2){
canvas.drawCircle(1000, 1000, 20, paintPoint);
canvas.drawText("Beacon 3", 900, 1060, textPaint);
}
else if (i == 3){
canvas.drawCircle(1000,100, 20, paintPoint);
canvas.drawText("Beacon 4", 900, 60, textPaint);
}
}
canvas.drawRect(positionRect, positionPaint);
canvas.drawText("my position", 350, 650, textPaint);
canvas.drawText(amountOfBeacons + " beacons are currently active", 100, 1200, textPaint);
String distanceString = "";
for (int i = 0; i < distances.length; i++) {
distanceString += String.valueOf(distances[i] + "m ");
}
}
}
MainActivity:
public class MainActivity extends Activity implements BeaconConsumer{
//initialize stuff
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customView = new CustomView(this);
setContentView(customView);
checkPermissions(MainActivity.this, this);
//some beacon logic
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.removeAllRangeNotifiers();
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
}
//Need to change text that was set in the onDraw Method of CustomView here
//Also need to change the location of a rect that was set in the onDraw here
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
}
我尝试过的每种方法都以错误结束或什么都没显示。我可以对逻辑的位置非常灵活。如果有帮助,它既可以在“活动”中又可以在“视图”中工作。