J2ME like Sprite on Android

For my useless month project, I am working on an emulator to run J2ME programs on Android. But now I'm stuck in a J2ME Sprite implementation. In particular, the transformations used in it.

In my sprite, I have a bitmap with three characters. I would like to draw a second frame, flipped or rotated 90 degrees. What would be the best way to do this?

I have the following code that draws this frame without any transformations.

frameX, frameY - coordinates of the frame position when creating a sprite bitmap.

Rect src = new Rect(frameX, frameY, frameX + spriteWidth, frameY + spriteHeight);
Rect dst = new Rect(paintX, paintY, paintX + spriteWidth, paintY + spriteHeight);
canvas.drawBitmap(image, src, dst, null);

As I understand it, I need to do uterine magic on canvas, but I could not understand it :)

+3
source share
3 answers

You know that Microemulator, an open source project, allows you to run J2ME code on Android, right?

http://www.microemu.org/

You can always look and see what they do.

+4
source

I went with splitting the sprite into frames and using transformations with one 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;
}

It works like a charm :)

+3
source

Android, Java . .

, Android ( ), :

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);

:

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);

. , . , . , .

Android

Android Canvas Class

Android matrix class

+2
source

Source: https://habr.com/ru/post/1718568/


All Articles