java - 如何在 android 中加载 5 行 5 列顶 View 的 Sprite 表?

标签 java android animation sprite

我有一个 612x864 尺寸的 Sprite 表,有 5 行和 5 列。我的问题是如何加载它并为其设置动画?我只想在 y 轴上移动猫 Sprite 。我已经尝试过了,但我的代码无法正常工作。这是我的代码。

在 GameView.java 中

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private Sprite sprite;

public GameView(Context context) {
    super(context);
    gameLoopThread = new GameLoopThread(this);
    holder = getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);
            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                                   int width, int height) {
        }
    });
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.catsprite);
    sprite = new Sprite(this,bmp);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    sprite.onDraw(canvas);
}
}

游戏循环线程.java

import android.graphics.Canvas;

public class GameLoopThread extends Thread {
static final long FPS = 10;
private GameView view;
private boolean running = false;

public GameLoopThread(GameView view) {
    this.view = view;
}

public void setRunning(boolean run) {
    running = run;
}

@Override
public void run() {
    long ticksPS = 1000 / FPS;
    long startTime;
    long sleepTime;
    while (running) {
        Canvas c = null;
        startTime = System.currentTimeMillis();
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
        sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
        try {
            if (sleepTime > 0)
                sleep(sleepTime);
            else
                sleep(10);
        } catch (Exception e) {}
    }
}
}

Sprite .java

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {
private static final int BMP_ROWS = 5;
private static final int BMP_COLUMNS = 5;
private int x = 0;
private int y = 0;
private int ySpeed = 3;
private GameView gameView;
private Bitmap bmp;
private int currentFrame = 1;
private int width;
private int height;

public Sprite(GameView gameView, Bitmap bmp) {
    this.gameView = gameView;
    this.bmp = bmp;
    this.width = bmp.getWidth() / BMP_COLUMNS;
    this.height = bmp.getHeight() / BMP_ROWS;
}

private void update() {
    if (y > gameView.getWidth() - width - y) {
        ySpeed = -5;
    }
    if (y + y < 0) {
        ySpeed = 5;
    }
    y = y + ySpeed;
    currentFrame = ++currentFrame % BMP_COLUMNS;
}

public void onDraw(Canvas canvas) {
    update();
    int srcX = currentFrame * width;
    int srcY = 1 * height;
    Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
    Rect dst = new Rect(x, y, x + width, y + height);
    canvas.drawBitmap(bmp, src, dst, null);
}
}

最佳答案

我会推荐你​​使用 this图书馆。它非常适合 Sprite 动画。虽然它有一些限制,但它工作正常。

这是我如何完成的代码。

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.runningcat);
int width = bitmap.getWidth();
int height = bitmap.getHeight();

int frameWidth = width / 5;    //you have 5 columns
int frameHeight = height / 5;  //and 5 rows
int frameNum = 25;             //there would be 25 images


SpriteSheetDrawer spriteSheetDrawer = new SpriteSheetDrawer(
        bitmap,
        frameWidth,
        frameHeight,
        frameNum)
        .spriteLoop(true)
        .frequency(2);   //change it as per your need


DisplayObject displayObject = new DisplayObject();
displayObject
        .with(spriteSheetDrawer)
        .tween()
        .tweenLoop(true)
        .transform(0, 0) //I have changed it according to my need, you can also do this by changing these values
        .toX(4000, 0)    //this one too.
        .end();
//In actual example, it's set as animation starts from one end of the screen and goes till the other one.


FPSTextureView textureView = (FPSTextureView) findViewById(R.id.fpsAnimation);
textureView.addChild(displayObject).tickStart();

关于java - 如何在 android 中加载 5 行 5 列顶 View 的 Sprite 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42288556/

相关文章:

css - 旋转图像无法正常工作

ios - 为什么我的 UISlider 没有动画?

java - Intellij spring boot PropertiesLauncher

java - 使用 UDP(数据报)在 Java 中实现 TCP

java - Android 简单计算器应用程序崩溃和逻辑

android - Android 上 Firebase 磁盘持久性中的错误?只能推送30条新记录

java - 序列化和反序列化未实现 Serializable 的对象

java - 如何捕获 Spring MVC 中的整数溢出错误?

java - 如何将 OSMDroid 与私有(private)提供商使用的网络 map 服务 (WMS) 一起使用?

ios - 在等待 Alamofire 请求完成时使用弹出动画对 UIView 进行动画处理