下面是扩展View类的CustomView类。静态效果很好。
我正在单击一个按钮,它显示了我要去的目标架子。然后我点击另一个按钮,它清除了目标架子,地图又回到了初始状态。
需要做的是,这个婴儿必须在xx秒后使用一些异步调用自行清除目标架子。以同样的方式,当我开始用“ X ”在地图上显示我的位置时,它必须在我行走时刷新那个“ X ”。
AsyncTask似乎不是解决这个问题的完美解决方案。你知道地图应该如何自我更新吗?
提前致谢。
自定义视图:
public class CustomView extends View {
ShapeDrawable roomFrame, targetShelfFrame, me;
int halfMe;
ArrayList<ShapeDrawable> shelfFrames;
//(...)
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
if(roomFrame != null)
roomFrame.draw(canvas);
for (ShapeDrawable shelfFrame : shelfFrames)
shelfFrame.draw(canvas);
if(targetShelfFrame != null)
targetShelfFrame.draw(canvas);
if(me != null)
me.draw(canvas);
}
public void setRoom(Room room){
roomFrame = new ShapeDrawable(new RectShape());
roomFrame.getPaint().setColor(0xff74AC23);
roomFrame.getPaint().setStyle(Style.STROKE);
roomFrame.setBounds(10, 10, room.getWidth(), room.getHeight());
invalidate();
}
public void setShelves(ArrayList<Shelf> shelves){
shelfFrames = new ArrayList<ShapeDrawable>();
for(int i = 0; i<shelves.size(); i++){
ShapeDrawable shelfFrame = new ShapeDrawable(new RectShape());
shelfFrame.getPaint().setColor(0xff74AC23);
shelfFrame.setBounds(shelves.get(i).getXPosition(), shelves.get(i).getYPosition(), shelves.get(i).getWidth(), shelves.get(i).getHeight());
shelfFrames.add(shelfFrame);
}
invalidate();
}
public void setTargetShelf(Shelf shelf){
targetShelfFrame = new ShapeDrawable(new RectShape());
targetShelfFrame.getPaint().setColor(Color.RED);
targetShelfFrame.setBounds((int)(shelf.getXPosition()), (int)(shelf.getYPosition()),
(int)((shelf.getXPosition() + shelf.getWidth())),
(int)((shelf.getYPosition() + shelf.getHeight())));
invalidate();
}
public void clearTargetShelf(){
targetShelfFrame = null;
invalidate();
}
public void updateMyPosition(Position position){
me = new ShapeDrawable(new OvalShape());
me.getPaint().setColor(Color.GREEN);
me.setBounds(position.getX() - halfMe, position.getY() - halfMe,
position.getX() + halfMe, position.getY() + halfMe);
}
}
我怎么称呼它:
public void loadRoomPlan(Room room, ArrayList<Shelf> shelves){
CustomView exampleView = (CustomView)findViewById(R.id.roomplan);
exampleView.setRoom(room);
exampleView.setShelves(shelves);
}