J2ME like Sprite on Android

旧巷老猫 提交于 2019-12-05 16:45:00

You do know that Microemulator, an open source project, makes it possible to run J2ME code on Android, right?

http://www.microemu.org/

You could always have a look and see what they do.

Went with splitting sprite into frames and using transformations with single image:

public final void paint(final Canvas canvas) {
  final Bitmap painted = images[frame];
  final Matrix matrix = createTransformationMatrix(transform);
  matrix.postTranslate(spriteX, spriteY);
  canvas.drawBitmap(painted, matrix, null);
}

private Matrix createTransformationMatrix(final int transform2) {
  final Matrix result = new Matrix();
  switch (transform2) {
  case TRANS_NONE:
    break;
  case TRANS_MIRROR_ROT180:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    result.postRotate(180);
    break;
  case TRANS_MIRROR:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    break;
  case TRANS_ROT180:
    result.postRotate(180);
    break;
  case TRANS_MIRROR_ROT270:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    result.postRotate(270);
    break;
  case TRANS_ROT90:
    result.postRotate(90);
    break;
  case TRANS_ROT270:
    result.postRotate(270);
    break;
  case TRANS_MIRROR_ROT90:
    result.setScale(-1, 1);
    result.postTranslate(getWidth(), 0);
    result.postRotate(90);
    break;
  }
  return result;
}

Works like a charm :)

I haven't done any Android development, but a lot of mobile and a lot of Java in that mobile development. So take this with that in mind.

What I would do, after taking a look at the Android class docs (linked below), is the following:

Rect src = new Rect(frameX, frameY, frameX + spriteWidth, frameY + spriteHeight);
Rect dst = new Rect(paintX, paintY, paintX + spriteWidth, paintY + spriteHeight);
Matrix orig = canvas.getMatrix();
canvas.rotate(90.0f);
canvas.drawBitmap(image, src, dst, null);
canvas.setMatrix(orig);

Or you can do it like so:

RectF src = new RectF(frameX, frameY, frameX + spriteWidth, frameY + spriteHeight);
RectF dst = new RectF(paintX, paintY, paintX + spriteWidth, paintY + spriteHeight);
Matrix matrix = canvas.getMatrix();
matrix.rotate(90.0f);
matrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
canvas.drawBitmap(image, matrix, null);

Both methods seem good to me. I'm not sure if either is faster. The latter solution is a bit more modular since you never have to change the canvas's matrix. So, that might be considered the better solution.

Android Class Listing

Android Canvas Class

Android Matrix Class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!