Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

C#
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();


    }

            }


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.
Posted
Comments
Sergey Alexandrovich Kryukov 14-May-15 17:10pm    
Does it hang the application? It can.
—SA
Member 11492419 14-May-15 17:33pm    
What do you mean by does it hang the application? The application works fine if that's what your asking.
Sergey Alexandrovich Kryukov 14-May-15 17:37pm    
Probably this is because onDraw is never called. Please check it up under the debugger.
—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
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900