开发者

Android: Animation and multithreading

i am trying to implement a multithreading in my app and i would like to know how i can do this. Basically in a single view i have 2 blocks i want these two block to bounce around the screen however i want each block to be running on its own thread. So single view multi-thread...if that's possible. Here's what i have so far:

public class mainActivity extends Activity {


protected static final int GUIUPDATEIDENTIFIER = 0x101;

//Thread myRefreshThread = null;

BounceView myBounceView = null;

Handler myGUIUpdateHandler = new Handler() {

    @Override
    public void handleMessage(Message msg){
        switch (msg.what){
        case mainActivity.GUIUPDATEIDENTIFIER:
            myBounceView.invalidate();
            break;
        }
        super.handleMessage(msg);
    }
};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    this.myBounceView = new BounceView(this);
    this.setContentView(this.myBounceView);

    new Thread (new RefreshRunner()).start();

}

class RefreshRunner implements Runnable {
    // @Override
    public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                    // Send Message to the Handler which will call the invalidate() method of the BoucneView
                    Message message = new Message();
                    message.what = mainActivity.GUIUPDATEIDENTIFIER;
                    mainActivity.this.myGUIUpdateHandler.sendMessage(message);

                    try {
                            Thread.sleep(100); // a 10th of a second
                    } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                    }
            }
    }
}


}

My BounceView is:

public class BounceView extends View {


protected Drawable mySprite;
protected Point mySpritePos = new Point(0,0);

protected enum HorizontalDirection {LEFT, RIGHT}
protected enum VerticalDirection {UP, DOWN}
protected HorizontalDirection myXDirection = HorizontalDirection.RIGHT;
protected VerticalDirection myYDirection = VerticalDirection.UP;


public BounceView(Context context) {
        super(context);

        this.mySprite = this.getResources().getDrawable(R.drawable.block);
}

@Override
protected void onDra开发者_如何学Cw(Canvas canvas) {
        /* Check if the Ball started to leave
         * the screen on left or right side */
        if (mySpritePos.x >= this.getWidth() - mySprite.getBounds().width()) {
                this.myXDirection = HorizontalDirection.LEFT;
        } else if (mySpritePos.x <= 0) {
                this.myXDirection = HorizontalDirection.RIGHT;
        }

        /* Check if the Ball started to leave
         * the screen on bottom or upper side */
        if (mySpritePos.y >= this.getHeight() - mySprite.getBounds().height()) {
                this.myYDirection = VerticalDirection.UP;
        } else if (mySpritePos.y <= 0) {
                this.myYDirection = VerticalDirection.DOWN;
        }

        /* Move the ball left or right */
        if (this.myXDirection == HorizontalDirection.RIGHT) {
                this.mySpritePos.x += 10;
        } else {
                this.mySpritePos.x -= 10;
        }
        /* Move the ball up or down */
        if (this.myYDirection == VerticalDirection.DOWN) {
                this.mySpritePos.y += 10;
        } else {
                this.mySpritePos.y -= 10;
        }

        /* Set the location, where the sprite
         * will draw itself to the canvas */
        this.mySprite.setBounds(this.mySpritePos.x, this.mySpritePos.y,
                        this.mySpritePos.x + 50, this.mySpritePos.y + 50);

        /* Make the sprite draw itself to the canvas */
        this.mySprite.draw(canvas);
 }
}

So this works perfectly however this only handles a single block. I would like to add a secondary block (using the same graphic) that is handled by a separate thread. How can i do this? do i have to make a separate view for the other block?


The looper that is used to read the messages you are sending from your thread is threadsafe but also sequences the received messages, meaning it can only be processing 1 msg at any given time. While you could, in theory, submit messages to this handler very easy from two separate threads, I'm not sure its going to accomplish what you want. I'm assuming you are seeking some sort of performance boost from using multiple threads?

If you are trying to get a performance gain, there are two things working against you. In the above code, the UI updating is being done by a single thread. No matter how many threads you have sending messages to the myGUIUpdateHandler, only one message is being processed at a time. This may cause tons and tons of invalidates, but your entire scene is being redrawn. Second, you can only ever have one thread modifying Android UI elements. If you were hoping to have two threads independently redrawing two different boxes using a View, it is not possible. In fact, I don't know if its possible to do so even using the opengl based views, since i think opengl also depends on a looper concept.

My answer would be that it is not possible, unless you care to clarify why you need two threads so that I can try to form an alternative solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜