如何将背景位图绘制到画布? [英] How to I draw a Background Bitmap to a canvas?

查看:102
本文介绍了如何将背景位图绘制到画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想知道是否有人可以帮我解决这个问题。我目前正在绘制一个画布的位图并使它们具有动画效果。我想将我在Photoshop中制作的这个背景图像绘制到画布上并让它填满整个屏幕,但我还没有找到办法。无论是小到大都可以,有人可以帮我解决这个问题,谢谢!



这是代码:



Hello everyone,

I was wondering if anyone could help me with this problem I am having. I am currently drawing bitmaps to a canvas and making them animate. I want to draw this background image I made in Photoshop to the canvas and have it fill the whole screen but I haven't found a way to do that. It either ends up being to small or to big, can anyone help me with this problem please, thank you!

Here is the code:

public class Fish extends View {
    Bitmap bitmap, mBackground;
    float x, y;

    public Fish(Context context) {
        super(context);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fish1);
        mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.tank);

        x = 0;
        y = 0;
    }


    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBackground, 0, 0, null);
        canvas.drawBitmap(bitmap, x, y, null);
        if (x < canvas.getWidth())
        {
            x += 10;
        }else{
            x = 0;
        }
        invalidate();


    }

            }





mBackground是后台我试图填充屏幕的图像,我已经完成了canvas.getWidth()和canvas.getHeight(),但这些都没有。



The mBackground is the background image that I am trying to fill the screen with, I have done canvas.getWidth() and canvas.getHeight(), but neither of those worked.

推荐答案

I从来没有为Android开发,但即使我可以看到在 onDraw 内部无效的尝试可以进行无限递归。事件,如果这个代码没有交给应用程序,这只是因为你可能有一些其他的bug,所以你的 onDraw 实际上并不处理图形渲染(如果它,它可以被覆盖的方法,或添加到事件的调用列表的方法,可能不是这种情况)。当处理处理并且 invalidate 被调用时,它将导致另一个 onDraw 调用,然后是另一个无效调用,依此类推 - 间接无限 相互递归



请参阅: http://developer.android.com/training/custom-views/custom-drawing.html [ ^ ]。



注意 onDraw 应该被覆盖。



并阅读View如何失效:

http://developer.android.com/reference/android/view/View.html#invalidate%28%29 [ ^ ],

http://developer.android.com/reference/android/view/View.html [ ^ ]。



-SA
I never developed for Android, but even I can see that the attempt to invalidate inside onDraw could go in infinite recursion. Event if this code does not hand to application, this is only because you could have some other bug, so your onDraw does not actually handle graphics rendering (if it does, it could be overridden method, or a method added to the invocation list of the event, which might not be the case). When rendering in handled and invalidate is called, it would lead to another onDraw call, and then on another invalidate call, and so on — indirect "infinite" mutual recursion.

Please see: http://developer.android.com/training/custom-views/custom-drawing.html[^].

Pay attention that onDraw should be overridden.

And read on how View is invalidated:
http://developer.android.com/reference/android/view/View.html#invalidate%28%29[^],
http://developer.android.com/reference/android/view/View.html[^].

—SA


有两种方法。

1.通过layout.xml文件:只需从资源为你的图像设置背景属性的路径(有没有编程)。

2.通过你的代码:使用画布.drawBitmap(Bitmap位图,Rect src,Rect dst,Paint paint)。

使用canvas size作为Rect dst。

和位图大小为Rect src。

Paint可以为null。
There is 2 methods.
1. By layout.xml file: just set path to "background" attribute to your image from resource (there is no programming).
2.By your code: use canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint).
Use canvas size as Rect dst.
And bitmap size as Rect src.
Paint may be null.


这篇关于如何将背景位图绘制到画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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