creating a CustomView - android

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.

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.

What is the best way to draw arbitrary objects in Android?

I've recently started building my second android app and I've hit a wall. What I want to know in general is this. In almost all other programming languages that I've used there is a simple way of drawing arbitrarily on screen. Usually there is some sort of Scene or Canvas type class and another GraphicObject type class. So If, say, I want to draw a car, I create a class that descends from GraphicObject and define they it draws itself and then add an instantiation of the Car class (the object) to the Scene. If I want a second car, you just instantiate another and put it wherever you like, by setting some kind of coordinate function. If this is a program where what is drawn depends on user input, everything must be done programatically and hence it does not depend on anything other than the code written.
How do you achieve this in Android? As far as I can muster the only object which you can define the way it is drawn is a View. However I have not been able to simply take my custom view (that draws a rectangle in my case) and instantiate two of them a put them in the screen where I want them, and do this in a programtic simple way....
Thank you for any advice.
Well if I want to draw lets say 10 rectangles then I wont instantiate my custom View 10 times. Instead I will add logic in my custom View to be able to draw 10 or more rectangles. Anyway if you want to draw again what was drawn in a View then you can call setDrawingCacheEnabled(true); on your View and then call getDrawingCache() to get Bitmap to draw it somewhere else.
Its almost same in android also,
All the widgets are inherited from class View, by extending this class you can actually create whatever you want limiting by only yours imagination, you have access to canvas here, use it along with touch controls exposed from class itself to override all the behaviours you want as per your need,.
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/graphics/Canvas.html

Draw bitmap to canvas, being the main activity?

I want to draw a bitmap to the canvas.
However, I am an inexperienced programmer and don't know how to make the canvas the main screen.
Basically, I want to draw a bitmap to the main activity using a canvas. I know how to draw bitmaps to canvases but it doesn't show up anywhere that I can see (with only one activity). I want the bitmap to show up on the main activity. Any tips?
Sorry if the wording is confusing, thanks for any help.
see these links. might think they may be helpful.
https://www.linux.com/learn/tutorials/707993-how-to-draw-2d-object-in-android-with-a-canvas
http://www.linux.com/learn/tutorials/703911-2d-drawing-with-android-motion-sensors
you can try searching online..
some example
http://www.edu4java.com/en/androidgame/androidgame2.html
or some game engine which will help you to develop game with proper memory management.
https://stackoverflow.com/questions/17163446/what-is-the-best-2d-game-engine-for-android
What you are trying to do is create your own CustomView. When you are putting a button or anything in your layout file that is actually a view. Android does allow you to create your own view to manually handle things on the screen. Games are usually implemented that way. They use customview by extending SurfaceView/GLSurfaceView.
As for the solution of your problem:
create a custom view by extending the View.
add all the constructor from superclass.
override onDraw method
onDraw method will pass you a canvas for you to draw
draw whatever you like(Bitmap, line ,....) using canvas
In your layout file include the customview you just created.
<my.packagename.MyCustomView android:width .... > </my.packagename.MyCustomView>
Hope this helps.
Also you will find lots good resources on how you can create your own customview.

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.

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