What is the best way to draw arbitrary objects in Android? - 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

Related

Android - How to create circle view with Android?

I am creating a View as below design here (like apple music).
pic 1:
pic 2:
The pink circle with physical interaction and fly. Can you suggest ways to make them?
Indeed, you should take a look at the custom view documentation.
What you should do to obtain such a result is to first override the onDraw() method in order to make your custom drawing inside.
Using the canvas, you will be able to create circle by calling :
canvas.drawCircle(x, y, radius, paint);
In order to make the circles look as you want, just take a look at the Paint documentation.
You can create as much circle as you want (the app efficiency is, of course, affected by the number of circle you draw).
With your custom view, you will be able to handle interactions easily, through the onTouchEvent() and to animate the circle modifying their properties over time.
You need to write own View as documented here https://developer.android.com/training/custom-views/index.html and then in your onTouchEvent() check if tap is inside or outside of area you consider checkable (in this case inside the given radius).

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.

How to draw a shape in Android as one of other layout objects?

I've been working on this for a while but can't find anything that exactly addresses my question (at least not something easy to understand).
I have a main layout XML file where I define various layout objects like a Button or a TextView (and I know I can add SurfaceView, View, and view and other things too). I want to draw a shape (in my case it's an arc) in just one of these objects so it doesn't take up the whole screen and so I can position it relative to other things.
(In my case it will ultimately re-draw the arc kind of like a circle with a gap in a different position every time I call a method depending on a value I pass to the method, but that's separate from my basic question.)
I know the answer will have something to do with a canvas, an onDraw method, maybe Paint, probably a view. I have been able to draw a circle from a custom View object by setting the main java file's layout as that View (as opposed to R.layouts.main), but that takes up the whole screen, and I'm unsure how I might be able to have that dynamically draw with modifications.
A really clear explanation or better yet an actual example would just be awesome.
As i see it u need to draw a specific shape on widget and not on complete screen. Try using layer List.
you can refer this link for sample Link

Custom Overlay MKMapOverlay iOS port of Android method

I have an android app I have written, that basically has a custom overlay, in this overlay I have overwritten the DRAW method to draw something on a canvas and at the end of my DRAW override, I call the super DRAW passing in the canvas I have painted.. the result is, every time the map is modified in anyway my custom overlay is redrawn as expected.
Now, I am trying to accomplish the same thing on iPhone and am getting a little confused.
Basically I need something drawn in the top right corner of the map every time a redraw occurs of the map. The drawin will change change with every update as well so can't simply be put a view over the map and pass touches through etc.
So, I guess the question is, what is the euivalent of the DRAW method in iOS.. how would I roughly accomplish this same thing? There are MKOverlayView in the API, but there seem to be some significant differences.. So, how do I put something say at 10x10 over the map, whos size is variable and make sure every time the map is moved, scaled or otherwised interacted with this object is redrawn at location 10x10 on the screen.
The docs for MKMapView state that you should not subclass MKMapView, so you can't / shouldn't override the drawRect method. You could add a UIView, though, and override drawRect to do your custom drawing.

Draw an image floating over another in Android

I want to be able to dynamically place image(s) over another image in my app.
Consider the first image as background and the other images to be on top level, I will also need to move those top level images (change their x and y on the screen) by code too.
Imagine, for example, a sea in which the user places fish and sea animals, then those sea animals start to move here and there on the screen: it will be like that.
How can I do this? If you don't know but remember any simple program or demo that does that, it will be also very welcome!
Thank you!
There is, of course, more than one way to do this, but I would say that the best way to do it would be to create a custom View (class that derives from View) and have this handle your bitmap drawing and all of your touch events.
There's a lot of code to write for loading the Bitmaps and keeping track of all of their positions (and then drawing them to the canvas in onDraw), but if you start really small by just allowing one image to be drawn and dragged around the screen, you can build on that and keep your code organized.
You would need to override onDraw(Canvas) and onTouchEvent(MotionEvent) in your custom View. You'll load your bitmaps with BitmapFactory (decodeResource method if you're including your images as resources in your project) and you'll need to remember to call recycle on your bitmaps when you're no longer using them.
In onDraw, you draw your bitmaps to the canvas at a specific location using Canvas.drawBitmap. There are two overloads of this method you can choose from, one that takes the top and left coordinates of the bitmap as floats (and therefore performs no scaling or stretching) and one that takes a destination and source rectangle to perform scaling, stretching and placement.
I always use the latter as it gives me finer tuned control. If you choose this route, you'll want to keep two Rect instances and a Bitmap instance for each image being drawn, update them in the touch events and draw them to the canvas in the draw event.
When something changes inside your view (as in the case of a touch event), call invalidate() method and the framework will know to redraw everything which triggers your onDraw method.

Categories

Resources