开发者

Positioning animated image frames

I have a field which extends BitmapField (called AnimatedGIFField) and an AnimatorThread (extending Thread) which does the work of looping through the GIF frames, incrementing the current frame and i开发者_如何学运维nvalidating the field, which calls the paint() method to draw the next frame (resulting in animation). The animation code works fine, but my issue is in the paint() method of the AnimatedGIFField class. I'm calling 'graphics.drawImage()' and I'm having trouble getting proper positions for x and y (the first two args to drawImage()). Positioning the AnimatedGIFField is working and is accomplished by overriding 'getPreferredWidth()' and 'getPreferredHeight()'. The relevant code is here:

public class AnimatedGIFField extends BitmapField {

    /**
      The current frame in the animation sequence; the AnimatorThread
      increments this so paint() knows which frame to draw.
    */
    private int currentFrame;

    public AnimatedGIFField(GIFEncodedImage image, long style) {
        super(image.getBitmap(), style);
        this.image = image;
        this.preferredWidth  = this.image.getWidth();
        this.preferredHeight = -(this.image.getHeight() * 4);
    }

    protected void paint(Graphics graphics) {
        // Calling super draws the first background frame.
        super.paint(graphics);

        // Don't redraw if this is the first frame.
        if (this.currentFrame != 0) {
            // Draw the animation frame.

            /* getFrameLeft() and getFrameTop() both return the top left-most position (0, 0). */
            /* graphics.drawImage(this.image.getFrameLeft(this.currentFrame), */
            /*                    this.image.getFrameTop(this.currentFrame), */
            /*                    this.image.getFrameWidth(this.currentFrame), */
            /*                    this.image.getFrameHeight(this.currentFrame), */
            /*                    this.image, this.currentFrame, 0, 0); */

            /*
              Currently trying some hackish nonsense like this to position the frame, although
              it probably won't scale properly across different devices/screen sizes.
            */
            int x = (this.getManager().getWidth() / 2) - 45;
            int y = (this.getManager().getHeight() / 2) + 83;

            graphics.drawImage(x, y,
                               this.image.getFrameWidth(this.currentFrame),
                               this.image.getFrameHeight(this.currentFrame),
                               this.image, this.currentFrame, 0, 0);
        }
    }
}


What about something like this?

         graphics.drawImage(this.image.getFrameLeft(this.currentFrame,
                           this.image.getFrameTop(this.currentFrame),
                           this.image.getFrameWidth(this.currentFrame),
                           this.image.getFrameHeight(this.currentFrame),
                           this.image, this.currentFrame, 0, 0);


Fixed the problem by stripping out all getPreferredWidth/Height stuff, removed the sublayout() method on my custom field and just overwrote layout() inside of my custom manager to position all fields (just the one) properly. That caused image.getFrameLeft() and image.getFrameTop() return proper values, which is where I had my positioning hacks before.

Thanks for the responses. I was making it way more complicated than it needed to be.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜