1

以下は、 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);
}
4

1 に答える 1

0

実際、問題を解決する最も簡単な方法(私があなたの目標を理解している限り)は、ハンドラーを使用してUIスレッドで遅延実行を実行することです。したがって、あなたがする必要があるのは:

  • ハンドラーの作成(カスタムビュー用にグローバルに)
  • インターフェイスを使用postDelayed(Runnable, long)して、特定の遅延でシェルフのクリーンアップを実行します

コードは次のようになります。

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
}

あなたの質問の2番目の部分(地図上で「X」を歩く)に関して、私はよく理解していませんでした-どのタイプの地図を使用していますか?グーグルマップですか?それはあなた自身の地図ですか?「ウォーキング」とはどういう意味ですか?それは本当の動きですか、それともマップ上でキャラクターを動かすことができるある種のゲームですか?どうか明らかにしてください

于 2012-07-30T19:38:53.600 に答える