下面是扩展CustomView类的视图类。它静态地工作很好。
我点击一个按钮,它显示的目标架,我要去。然后我点击另一个按钮,它清除目标的架子和地图回到它的初始状态。
需要做的是,这个婴儿必须清除目标货架本身使用一些异步调用,在xx秒后。以同样的方式,当我开始用"X“在地图上显示我的位置时,它必须在我走路的时候刷新"X”。
AsyncTask似乎不是这方面的完美解决方案。你对地图应该如何更新有什么想法吗?
提前谢谢。
CustomView
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);
}发布于 2012-07-30 19:38:53
实际上,解决问题的最简单方法(据我理解)是使用Handler在UI线程上执行延迟执行。所以你需要做的是:
postDelayed(Runnable, long)接口运行您的货架清理与一定的延迟。您的代码看起来应该是:
The CustomView:
public class CustomView extends View {
ShapeDrawable roomFrame, targetShelfFrame;
ArrayList<ShapeDrawable> shelfFrames;
Handler uiThread = new Handler();
//(...)
public void setTargetShelf(final 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();
uiThread.postDelayed(new Runnable()
{
@Override
public void run()
{
clearTargetShelf(shelf);
}
}, 10000); //10 secs delay
}关于你问题的第二部分(在地图上行走“X”),我不太明白--你用的是哪种类型的地图?是谷歌地图吗?是你自己的地图吗?“走路”是什么意思?这是真正的运动,还是某种游戏,你可以在地图上移动你的角色?请澄清
https://stackoverflow.com/questions/11728212
复制相似问题