Drawing to a new Bitmap and then using that in my onDraw - android

I would like to display several rings with different coloured sections (see below). The colour of the sections, however, cannot be know in advance, so I will need to draw these dynamically.
I know I could draw directly to the canvas but, once I have them, I would like to animate these rings, rotate them, have them overlap etc. It seemed, therefore, that the easiest and possibly least expensive approach would be to create them in advance, in memory, as transparent pngs and then just draw them in onDraw.
My problem is the only methods I can find to do this are setPixel. Is there not a way I could use drawing tools, like in Canvas, to draw to an empty bitmap, once, then use that bitmap with my canvas in onDraw?
I feel like I am missing a piece in the puzzle. Any help would be greatly appreciated.

You can create a Bitmap that is the size you want the ring to be and then create a Canvas the same size. Call setBitmap() on the Canvas and it will draw on to that for you. Then you can build your circle and have a bitmap to hold onto and use just like any other resource.

Related

How to select the image by drawing a line around it in android?

I am creating a simple free hand drawing app. In my layout, i have two images placed. I need to select the correct image, by drawing a free hand circle around the image. As i am new to android, i don't know the correct way to do it. I tried some ways to draw free hand drawings like, calling a drawing view class and using the onDraw method to paint the path using canvas, but for that i had to kept my setContentView() to drawing view class. But I want to use my layout with images as setContentView() and make free hand drawings to draw around the images. Can somebody help me? Thank you in advance.

Android: Custom masking 2 bitmaps layered one above the other

easy now.
What I want eventually:
I want to have 2 bitmaps overlayed on a view. Same bitmaps with the one above have higher brightness than the below one.
Now when the user strokes(with touch event (like paint brush)) on the upper bitmap, I want those parts of the upper bitmap to go invisible.
For those who are familiar with adobe photoshop perhaps this will make more sense:
I want to draw a mask on an image being display so that only the unmasked parts remain visible. But the mask can be drawn from a brush with variable hardness/size.
How do I achieve this functionality? Direct me in in the line where I should research or give sample code.
Also, is it possible to draw strokes on an imageview with a brush which has variable hardness? I know that I can drawPath and drawArc on a canvas, but I do not know how to achieve the different brush strokes/styles.
Please pardon me if I haven't phrased my question right, or wasn't able to find similar duplicates.
You can use FrameLayout to overlay one image over other in Android and for Custom Masking search FingerPaint on google.
I think the best way is to do your own off-screen compositing, then render the composited image using an ImageView or perhaps a subclass with custom interaction. See this sample code for an example of how to do such compositing using the Porter-Duff transfer modes.

animation on canvas

I am working on application in which i am drawing a image on the canvas now i want o draw the second image in that canvas.
for example see the image ......
the first image s looking as follow ....
when i click on some place then it will show as follow
but the other thing which is shown in the image (thymin) it should display as fadein in the canvas
how to do it i am not getting any thing can any one help me .....
That's quite complicated by the sounds of it though not impossible to achieve. I think it would take more than the standard View class to achieve this, more like you would need to use the SurfaceView class where you have greater control over what gets drawn and where and when. If you're not familiar with Android 2D graphics then a good place to start would be here.
For drawing the bitmap, you could use canvasObject.drawBitmap() method of Canvas class. drawBitmap takes a paint object as one of its parameters, you could programmatically vary the alpha value of the paint object to create a fade-in effect. Paint class has a setAlpha(value) method to do this. I haven't tried this out myself but this should work.

Canvas Dynamically change a bitmap's z-index

I am creating an Android app and in my app I have a canvas which I draw numerous bitmaps to the canvas via the canvas.drawBitmap () function. From my understanding the z-index on these bitmaps are set based on the order in which they are being drawn to the canvas. What I am trying to figure out is after drawing these bitmaps if I can dynamically change the z-index on a bitmap to push it to the top? This seems like a very simple problem, but I have had not luck in finding a solution yet.
Not really possible: after you call drawBitmap the contents of the bitmap are rendered onto the canvas, but the canvas does not store any references to the original bitmap, it only stores the results of applying the bitmap's contents to the canvas. There is no way to dynamically say that bitmap you drew 1st out of 50, I want you to make that the 50th bitmap and automatically redraw every single other bitmap to reflect the change.
So you'll need to order your drawing operations before hand.

On the fly bitmap editing in Android

I'm creating a scene for a game.
There's a background which is a bitmap and player objects. I want to have the player objects to be able to "eat" away at the background with a transparent color as they move around the board. Basically if an object is at a certain point a circle around that object is alpha'ed out of the bitmap. The edits of the bitmap need to persist.
What's the best way to accomplish this? I was thinking of modifying the pixels for the bitmap using Bitmap.setPixel but that would probably be very expensive.
I figured it out.
I need to basically use Canvas's draw functions which are applied to a bitmap.
IE
Canvas.drawBitmap()

Categories

Resources