Java的:Android的:不断变化的位图的颜色在code帆布 [英] Java: Android: changing color of bitmap on canvas in code

查看:94
本文介绍了Java的:Android的:不断变化的位图的颜色在code帆布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些事情,我觉得很简单,但它显然不是!

I'm trying to do something that I thought was simple but apparently it's not!

我做一个简单的应用程序来学习Android的游戏开发。此刻,用户点击在屏幕上并显示的子画面。精灵只是一个纯白色的正方形。我想改变,在code。

I'm making a simple app to learn Android game dev. At the moment, the user clicks on the screen and a sprite is displayed. The sprite is just a solid white square. I want to change that in the code.

我觉得我用C#/ XNA的地方,如果精灵是纯白色,可以设置在绘制方法的颜色被宠坏了。这似乎并不在这里工作,甚至当我用油漆的功能。我做了一些研究,但所有的解决方案似乎并不相当有什么我要找的,也很复杂。我希望找到的东西,至少简单的使用,如果我不能找到确切的解决方案一塌糊涂。下面是相关的code迄今:

I think I'm spoiled by C#/XNA where, if a sprite is solid white, you can set the colour in the draw method. That doesn't seem to work here, even when I used the paint function. I've done a bit of research but all the solutions don't seem to be quite what I'm looking for, and also very complicated. I'm hoping to find something at least simpler to mess with if I can't find the exact solution. Here's the relevant code so far:

从视图:

    private Sprite createSprite(int resouce) {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
    return new Sprite(this, bmp, touchX, touchY);

视图的onDraw方法:

the view onDraw method:

        if (touched == true)
    {
        canvas.drawColor(colors[nextColor]);
        lastColor = colors[nextColor];

        //add bitmap to array
        sprites.add(createSprite(R.drawable.test));
        //draw sprites
        for (Sprite sprite : sprites) {
            sprite.onDraw(canvas);
        }

这是精灵类相关code:

relevant code from the sprite class:

    private Paint paint = new Paint();

public Sprite(DaphnyView daphView, Bitmap bmp, int positionX, int positionY) {
    width = bmp.getWidth();
    height = bmp.getHeight();
    DaphView = daphView;
    Bmp = bmp;
    PositionX = positionX;
    PositionY= positionY;
    paint.setColor(Color.GREEN);
}

public void onDraw(Canvas canvas) {
    canvas.drawBitmap(Bmp, PositionX, PositionY, paint);

}

如果有人能帮助我,或至少指向我一个很好的起点,这将是一个很大的帮助。谢谢!

If anyone can help me out or at least point me to a good starting point, it would be a great help. Thanks!

推荐答案

我找到了一个解决方案,通过创建一个新的画布来修改位图,然后通过这个修改的位图回到原来的画布上。

I found a solution by creating a new canvas to modify the bitmap, then passing this modified bitmap back to the original canvas.

这篇关于Java的:Android的:不断变化的位图的颜色在code帆布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆