Android scrollable textview on canvas - android

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.

Related

How to create a custom view in android

I want to learn, how to create custom view. I have gone through multiple tutorial and didn't get what i want(I mean concept).
What i have learned
I am sharing my concept,if i am wrong please correct me.
If you want to make a minor change to the standard view then inherit itself(e.g extends seekbar) and customize it.
But if you want to make difficult or multifunction view, We should inherit View Class
When we inherit a View.
Two methods are neccessary
onDraw(Canvas canvas) // it call each time it and also can be call using invalidate.
onMeasure // it measure the actual width and size and set our viewit accordingly
Whatever you want to draw on View can be done using canvas;(Am i right).
To define additional attributes create an attrs.xml file in your res/values folder. and add <declare-styleable name="YourCustomView"> and add attributes to it.
get these attribute in the constructor of the View using TypedArray call recyle method.
My Confusions
can we draw something without using Canvas
can we draw ImageView without converting it to bitmap.
I simply want to create a custom imageView which can be rotated like
a dialView but it should rotate half(semi-circle) and also want to
draw its progress outside it. using canvas.drawArc()
What i want to achieve
I want to create my own custom knob, I have searched our google but didn't find what i want see my questions here
First Question
Second Question
Thanks in Advance....
1)You can use openGL. Otherwise you need a canvas. The canvas defines where you're drawing to, without it (or the openGL equivalent) we don't know where to draw. You might not want to draw directly to the screen, its common to draw to bitmaps in many cases.
2)Your question doesn't make a lot of sense. Why would you want to draw an ImageView, other than by letting it draw itself?
3)Easiest way to do that is to use a normal image view and call setRotation to rotate it as needed.

Android canvas on layout (as opposed to view)

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

Change canvas of a view?

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.

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.

creating a CustomView

I am trying to implement this game, and I am confused about how to start.
Should I use ImageButtons and implement this game, or should Icreate some custom view?
I am not asking you the code, but what custom view I should create. Basically, I need a plan as to how to implement this game.
It looks like line drawing.
So extend View, implement onDraw method, and there draw lines to Canvas.
If there're other parts to draw, you may possibly use Bitmap to draw these onto the Canvas.

Categories

Resources