以下は、 Viewクラスを拡張する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);
}