I have a customview and it's background color must be changed every one or two seconds between two colors for example black and white.I can use canvas.drawARGB() and invalidate() to fill canvas with any color.But there are two constant colors that canvas can be colored and calling invalidate every one or two seconds reduces speed.So I guess if I have two canvas that first is colored white and second black and I change whole canvas in onDraw() it may be better than calling canvas.drawARGB().Is my guess true?And is it possible?
Note:
My purpose is to avoid of calling canvas.drawARGB() or canvas.setBitmap() or some things else.
If you are trying to change the background color I think a better and simpler approach is which is presented here in the accepted answer:
v.setBackgroundColor(0xFF00FF00); // Where v is your view
Or:
v.setBackgroundColor(Color.GREEN); // Where v is your view
This should be faster than filling the Canvas View with an specific color and then call invalidate()
I don't think that changing the whole canvas is possible or wise since the canvas object in ondraw method is all ready prepared for the view and the overhead needed to create a copy of it may be inefficient. However you can use the setBackgroundColor which will apply the color before the ondraw method gets called, see here the drawing paragraph. I think this is the correct approach because you are letting android to do the background assuming that it will use the fastest and the more efficient method.
Hope this helps...
you should change color of your paint object and pass that paint object to your canvas object.
Related
I have a custom View where i draw something on it on onDraw.
On every onDraw i draw the background ( some basic triangle and circle ) and then i draw something that looks different every time i draw it.
How can i draw the background ( the static stuff ) once and just leave it as it is? Because drawing the background seems to put a lot of strain on the GPU since the other animations seem to slow down ( drawer pulling out and ripple animations )
How can i avoid not drawing it on every onDraw?
Thanks
Use a bitmap. Create a Bitmap object, then create a Canvas passing that Bitmap in as a parameter. Draw whatever you want to that canvas. That Bitmap now holds your static drawing. In your onDraw function, first draw the static bitmap to the canvas passed into onDraw. Then draw your dynamic data. This is actually a good way of doing things, since drawing a bitmap is relatively fast.
You can have two views- one view holds the background and other view on top of background-view.
You don't invalidate the view which holds the background each time you do animation.
You can use FrameLayout to have a view on top of each other.
I have created a custom view to be shown in the action bar. It mostly works except sometimes on start I see a mirror copy of whatever I draw. The copy overlaps the original one but a few pixels away.
My onDraw() override is quite simple. All it does is draws an image that is centered in the canvas.
I am a bit confused. Am I expected to clear the canvas first in onDraw() method? Thank you in advance for your help.
It is confusing, but you'll notice in the custom view samples (that come with the sdk), the onDraw() method first calls canvas.drawColor() before anything else.
I assume it's not done automatically because it would be wasteful in the case where what you were drawing filled the entire view anyway. I just wish it was more clear that it is necessary in most cases.
Here is what I found out that others may find useful. If you set a background using setBackground(), the framework will actually draw the background before calling onDraw(). This background is drawn in the center. I too was drawing the background in onDraw() but I was drawing it from top-left. Hence, I was seeing the ghost image.
As Krylez mentioned, you may wish to call drawColor() as your first call in onDraw().
What I did was overrode setBackground() and its variations, and stored the background bitmap in a local variable.
Regards,
Peter
I would like to have a scrollable textview on a canvas.
This post describes how to draw a textview onto a canvas and this post describes how to make a textview scrollable. If I combine them in the most straightforward way (i.e. just add function calls setMaxLines and setMovementMethod) it does not work.
What are you trying to accomplish exactly? The post about using a layout to draw into a Canvas seems unnecessary in most circumstances. I have a different suggestion...
Extend TextView and provide the Canvas to it as a member (pass it in a constructor or set method). Then override the draw method and use the member Canvas to pass to super.draw. Then do whatever else you want with the Canvas.
Or, if you aren't doing anything too crazy, simply extend TextView, override onDraw, and use the Canvas passed there to do your drawing. Only in very unusual circumstances should you really need to draw the View into a separate Canvas.
I have a custom view with two lines that had been drawn using drawLine function. I want to animate the alpha of second line but whenever i try to animate it whole canvas is animating. I try to find like anything so far but didn't get any luck.
Any help would be really appreciated.
Thanks.
I'd do like this:
Implement onDraw() in your custom View
Do not create any object in onDraw()
Provide public set and get methods for your alpha field that you use in onDraw()
Use ObjectAnimator to animate alpha property of your custom view
Probably use also this instead calling invalidate() to redraw only part of your 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.