Android canvas on layout (as opposed to view) - android

Is it an allowed practice to put a canvas on a layout, and draw one's content there?

If you are using a layout, I assume it's because it contains child views? You can override dispatchDraw to do some customization before or after you ask the children to draw themselves.
I have done that to add a bookshelf background to my GridView, for instance:
http://chiuki.github.io/fluid-android-layouts/#31
Another example is to rotate the canvas before drawing:
http://chiuki.github.io/android-custom-components/#46

Related

Android spinning wheel (pizza)

I am trying to create a circle wheel divided in a fixed number of sections. Each section should be clickable.
How I approach this? Should I make an image and set as a background or is there a way to draw all parts in java?
Shortly:
Create .png with pizza.
Create new widget, extending View.
Override onDraw() and draw it to canvas with some rotation. Optionally you can draw lines with java if it's margeritta not a pepperoni.
If necessary - change rotation call invalidate() to redraw view
Add onTouch() listener, get position ot tap, calculate what sector was touched.
One Solution would be to draw different paths for each piece on a Canvas (path.lineTp and path.addCircle - think so). Then you can add ClickListeners to the paths and they will be clickable....

Android scrollable textview on canvas

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.

Android - How to animate a single element of Canvas instead of whole canvas or view?

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.

Implementing shapes that can be dynamically changed

Am I approaching this correctly or is there a better way?
I would like to have various shapes such as lines, rectangles, etc, that a user would be able to re-size, rotate, and otherwise change its parameters by clicking on the shape and dragging.
So far, I've implemented this with shapes by drawing the shape into a view and then adding the view onto a layout. A user can then drag that view.
But is this the best way? By doing this, I am manipulating the view that contains the shape and not the shape itself.
Can the shape be re-sized/moved directly through user manipulation?
The best way to draw shapes in Android is extend class from view then draw shapes in onDraw method also you can resize and move shapes using onTouch method dynamically.
Refer this link,
http://www.kellbot.com/2009/06/android-hello-circle/

display transparent view over existing views

In Android 2.2, I want to display a few sprites and clickable animations over an existing view. I just discovered that SurfaceView cannot be transparent
I tried overriding dispatchDraw() of a LinearLayout but that doesn't seem to be callable via a Runnable Thread.
I also tried to extend Canvas directly, but that quickly turned into a mess when trying to place it in my XML layout.
Do I have to use GLSurfaceView to have a drawing view whose background is transparent?
Optimally, I'd like to just have a Canvas on which I can draw Bitmap at various coordinates, instead of dealing with the details of GL (unless GL is the only way I can do transparency).
Silly Rabbit. Just use Views and move them around wherever you want within a FrameLayout. SurfaceView is not designed for what you're trying to do.
I'm going to try Switching from Canvas to OpenGL and toss in parts of TranslucentGLSurfaceView.
I'm still open to selecting an answer that doesn't require OpenGL, just for the sake of it solving my original question.

Categories

Resources