在android中移动带有重复位图图像的路径

发布于 2024-09-19 04:52:22 字数 319 浏览 4 评论 0原文

我目前正在编写一个 Android 横向卷轴游戏,但在使用重复位图图像填充路径时遇到问题。我正在从多个坐标创建一条路径来组成“地面”区域。我有一个角色固定在画布和屏幕的中间,并移动路径来代表角色的运动。我已经能够使用 BitmapShader 用重复图像填充路径。我还可以在屏幕上将路径形状从一侧移动到另一侧。但是,位图着色器似乎使用默认原点 0,0,这意味着着色器始终在同一位置绘制地面重复图像。这意味着即使路径在移动,地面重复的图像也永远不会移动。有谁知道如何更改着色器的原点或知道用重复图像填充路径的更好方法?

或者,有人可以建议一个更好的解决方案来用图像填充可绘制形状吗?

谢谢 安迪

I'm currently writing an android side-scrolling game and am having trouble filling a path with a repeating bitmap image. I'm creating a path from a number of coordinates to make up the "ground" area. I have a character who is fixed in the middle of the canvas and screen and am moving the path to represent the movement of the character. I've been able to fill the path with a repeating image using a BitmapShader. Also I can move the path shape from side to side on the screen. However, the Bitmapshader seems to be using a default origin of 0,0 which means the shader is always drawing the ground repeating image in the same place. This means that even though the path is moving the ground repeated image never appears to move. Does anyone have any idea how to change the origin of the shader or know of a better way to fill the path with a repeating image?

Alternatively, can anyone suggest a better solution for filling a drawable shape with an image?

Thanks
Andy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神仙妹妹 2024-09-26 04:52:22

谢谢,看看那些...Replica Island 似乎经常使用 OpenGL,目前这有点超出了我的能力,而 Snake 并没有完全做到我想要的...最终到达了那里..

    //Shape pathShape = this.getPathShape();
    Bitmap groundImage = ImageHandler.getmGroundImage();
    int offset = groundImage.getWidth()-(xPosition%groundImage.getWidth());
    Path path = new Path();
    path.moveTo(coordinates.get(0).getmX(), coordinates.get(0).getmY());

    for ( ShapeCoordinate coordinate : coordinates ) {
        path.lineTo(coordinate.getmX(), coordinate.getmY());
    }

    path.lineTo(coordinates.get(coordinates.size()-1).getmX(), mYBase);
    path.lineTo(coordinates.get(0).getmX(), mYBase);
    path.lineTo(coordinates.get(0).getmX(), coordinates.get(0).getmY());
    path.close();

    PathShape shape = new PathShape(path,canvas.getWidth(),canvas.getHeight());

    BitmapShader bs = new BitmapShader(groundImage, Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);

    Matrix matrix = new Matrix();
    matrix.reset();
    matrix.setScale(1,1);
    matrix.preTranslate(offset, 0);
    bs.setLocalMatrix(matrix);

    ShapeDrawable sd = new ShapeDrawable(shape);
    sd.setColorFilter(Color.argb(255, 50*(mLevel+1), 50*(mLevel+1), 50*(mLevel+1)), Mode.LIGHTEN);
    sd.getPaint().setShader(bs);
    sd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    sd.draw(canvas);   

Thanks, had a look at those...Replica Island seems to use OpenGL quite a lot which is a bit beyond me at present and Snake didn't quite do what I was looking for...eventually got there..

    //Shape pathShape = this.getPathShape();
    Bitmap groundImage = ImageHandler.getmGroundImage();
    int offset = groundImage.getWidth()-(xPosition%groundImage.getWidth());
    Path path = new Path();
    path.moveTo(coordinates.get(0).getmX(), coordinates.get(0).getmY());

    for ( ShapeCoordinate coordinate : coordinates ) {
        path.lineTo(coordinate.getmX(), coordinate.getmY());
    }

    path.lineTo(coordinates.get(coordinates.size()-1).getmX(), mYBase);
    path.lineTo(coordinates.get(0).getmX(), mYBase);
    path.lineTo(coordinates.get(0).getmX(), coordinates.get(0).getmY());
    path.close();

    PathShape shape = new PathShape(path,canvas.getWidth(),canvas.getHeight());

    BitmapShader bs = new BitmapShader(groundImage, Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);

    Matrix matrix = new Matrix();
    matrix.reset();
    matrix.setScale(1,1);
    matrix.preTranslate(offset, 0);
    bs.setLocalMatrix(matrix);

    ShapeDrawable sd = new ShapeDrawable(shape);
    sd.setColorFilter(Color.argb(255, 50*(mLevel+1), 50*(mLevel+1), 50*(mLevel+1)), Mode.LIGHTEN);
    sd.getPaint().setShader(bs);
    sd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    sd.draw(canvas);   
拍不死你 2024-09-26 04:52:22

您看过 android sdk 中的 Snake 示例吗?另外,Replica Island 是如何在 Android 中创建图块引擎的另一个示例。

Have you looked at the Snake example in the android sdk? Also Replica Island is another example of how to do a tile engine in android.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文