我有一个扩展BitmapField(称为AnimatedGIFField)的字段和一个AnimatorThread(扩展Thread),它通过GIF帧循环,递增当前帧并使字段无效,调用paint()方法绘制下一帧(产生动画)。动画代码工作正常,但我的问题是在AnimatedGIFField类的paint()方法中。我正在调用'graphics.drawImage()'并且我无法获得x和y的正确位置(drawImage()的前两个参数)。定位AnimatedGIFField是有效的,可以通过覆盖'getPreferredWidth()'和'getPreferredHeight()'来完成。相关代码在这里:
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);
}
}
}
答案 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);
答案 1 :(得分:0)
通过剥离所有getPreferredWidth / Height内容来修复问题,删除了我的自定义字段上的sublayout()方法,并在我的自定义管理器中覆盖了layout()以正确定位所有字段(只有一个)。这导致image.getFrameLeft()和image.getFrameTop()返回正确的值,这是我以前定位黑客的地方。
感谢您的回复。我让它变得比它需要的更复杂。