Im making an app where I need to create lots of similar images made out of simple components images. The images don't change but I don't want to have to make all of the images by hand (and I think it could be useful later to have them procedurally generated). As a basic example of what I'm trying to do I have 2 drawables and am trying to combine them on a Canvas. The issue is that when I create the canvas like so:
Canvas canvas = new canvas(background);
The app crashes because background is null. I Don't understand why background is null.
Here is the rest of the code:
Bitmap background = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.background);
Bitmap foreground = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.foreground);
Canvas canvas = new Canvas(background);
canvas.drawBitmap(foreground,0,0,null);
If it's of any help this is all in a fragment.
Related
I'm making a simple side scroller game in Java but am having trouble making the camera move with the person. My plan was to create a buffered image that could adjust the region of the image that is displaying and simply have it follow my character. However, I couldn't find any functions in the API to change the region of the buffered image that's displayed, so how could I do this? Thanks for any help provided.
//The level is created in an 800x400 pixel size, is there any way I can change
//the region myImage displays?
myImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
Well while you will be drawing your image you have a lot of options for example this function can draw any rectangle from your original image on any rectangle of the surface you get Graphics object from.
OK so here is what I'm looking to do. I have an app that loads small bitmap images with a pattern that I'd like to use as my phones background (not the app background) Problem is when I set it as a wallpaper now using wallpapermanager it stretches the image and it looks horrible. Is there anyway I can either a) using wallpaper manager to tile the image so it looks nice or b) create another tiled bitmap from the small bitmap and use that. Suggestions?
find the dimensions of your screen. Edit the picture using http://pixlr.com/editor/ if you dont have photoshop.
I`m building an android App, and i got stuck with a simple thing: How do i draw (or "add") a Canvas object, to another Canvas object, like "merging" them?
If that`s not possible, what is the best solution for doing that?
Thanks!
This depends entirely on your implementation.
If each Canvas draws objects directly from an array (of shapes, etc.) each frame, you could simply append one array to the other. This way, your Canvas does not need to be drastically altered, it only has to add one array to another (possibly an ArrayList would be the way to go here).
If the above is not the case, you may have to make some more drastic changes. When I encountered a similar problem, I created a new method called commitChanges(), which added a series of changes to an existing Canvas (adding lines on top, etc.).
I first invalidated the affected area, then created a Bitmap with the size of the Canvas: Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGBA_8888);.
Next, I created a canvas from that Bitmap: Canvas workingDrawing = new Canvas(bmp);.
Then, I drew everything I needed onto that new Canvas. In this case, that would be the data from one of your Canvases.
Now, in your other Canvas, you have to get the Bitmap you just drew, then draw it onto this Canvas. Like so: canvas.drawBitmap(yourDrawnBitmap, 0.0f, 0.0f, null);.
I think the difficulty you'll face is transferring the data from one Canvas to another. But, regardless of your implementation, one of the above methods should work effectively for you.
I have an app that uses a Class with an extension SurfaceView to draw to a canvas, but i'm looking at moving over to openGL. I noticed that if I merely change the SurfaceView extension to a GLSurfaceView, the app still runs the same. I've also been able to start basic drawing in another app using openGL ES.
I took it the next step and instantiated another class for the renderer in GLSurfaceView, and I instruct the render to draw a triangle. This all compiles and runs fine, but it runs exactly like the original App, with no triangle rendered, but the rest of the canvas draws properly.
I can't think of why it's not rendering, or why it wouldn't render. Or maybe it is rendering, but it's simply being overridden by the canvas?
I know it seems odd to try to use both methods, Since my app is live, I would rather implement switching the rendering over to GL at my own pace so that it doesn't take me a month to get the next update out.
So Anybody ever tried do do this? Run a Gl renderer OVER a canvas?
You cannot use both OpenGL and a Canvas to render on a single SurfaceView. You can however put another View on top of the SurfaceView to achieve the desired effect.
Try the following:
addContentView(SurfaceView);
addContentView(GLSurfaceView);
or create a relative layout with both views.
Here my sample code when adding canvas over the OpenGL view, thanks to #Audrius Butkevicius !
mGLView = new MyOpenGLSurfaceView;
// SET OpenGL View
setContentView(myOpenGLView);
mCanvasOverlayView = new View(context)
mOverlayViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// ADD Canvas view overlay
addContentView(mCanvasOverlayView, mOverlayViewParams );
How does one go about doing this? Could somebody give me an outline?
From what I've found online, it seems like in my run() function:
create a bitmap
create a canvas and attach it to the bitmap
lockCanvas()
call draw(canvas) and draw bitmap into back buffer (how??)
unlockCanvasAndPost()
Is this correct? If so, could I get a bit of an explanation; what do these steps mean and how do I implement them? I've never programmed for Android before so I'm a real noob. And if it isn't correct, how DO I do this?
It's already double buffered, that's what the unlockCanvasAndPost() call does. There is no need to create a bitmap.
The steps from Android Developers Group say that you need a buffer-canvas, to which all the renders are drawn onto.
Bitmap buffCanvasBitmap;
Canvas buffCanvas;
// Creating bitmap with attaching it to the buffer-canvas, it means that all the changes // done with the canvas are captured into the attached bitmap
tempCanvasBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
tempCanvas = new Canvas();
tempCanvas.setBitmap(tempCanvasBitmap);
// and then you lock main canvas
canvas = getHolder().lockCanvas();
// draw everything you need into the buffer
tempCanvas.drawRect.... // and etc
// then you draw the attached bitmap into the main canvas
canvas.drawBitmap(tempCanvasBitmap, 0, 0, drawView.getPaint());
// then unlocking canvas to let it be drawn with main mechanisms
getHolder().unlockCanvasAndPost(canvas);
You are getting the main buffer, which you are drawing into without getting different double-buffer canvas' on each holder's lock.