Drawing from a Service in Android - android

I'm trying to draw (IE GLSurfaceView) from a Service and so that it runs in the background.
I know this is possible because the app Transparant Screen gives a perfect example of this.
However, i have no idea how to accomplish this.
So far, i created a activity and a service and some Opengl draw methods.
I also made the GlSurfaceview and activity transparant, but ofcourse touches won't be redirected to the layer under (child).

I don't think it has been designed to be used that way,
however you can reach activity from service quiet easily (using a singleton) and then you should be able to draw into a canvas or drawable and then invalidate the view to draw your offline one ...
Note I am answering on a slightly different context, by using a 2D widget view.
I need to check my own to make sure it is the best of the worse cases... I'll may update this post later

Related

Display Watch Face in android activity

I'd like to display a watch face I've developed in my app and have it appear live as though it was on a watch. The class and engine already exist so i feel like it shouldn't be too hard to get it to appear within an activity. Does anyone have experience with this or have a suggestions as to which path to take in attempting this?
It should be fairly easy to achieve. What you need to do is this:
extract all the drawing logic; which is whatever code is interacting with Canvas.
create a custom View and in View.onDraw(Canvas) use the extracted code to draw the watch face.
In the end everything draws on a Canvas, so you can (more or less) transfer functionality from View objects to WallpapersService. View system is an abstraction on top of Canvas.

What is a good way to test the onMeasure/onLayout/onDraw methods of a custom View?

I just wrote a custom View class that, among other things, allows a developer to easily set borders (e.g. setBorderWidth, setBorderColor, setBorderWidthLeft, etc.). I did this by overriding onMeasure/onDraw and I want to test that the View properly draws these borders.
Ideally, I want something at a higher level than a unit test: basically I want to enforce that if I set borders, they are drawn as expected; if I don't set them they aren't drawn; if I change them, the new borders are drawn and the old ones are no longer visible. This allows me to know that my view is working at a high level.
Things I've considered:
Taking the view in isolation with Robolectric and calling onDraw manually with a mock Canvas (doesn't test invalidation though)
Making an Activity test case and somehow saving a screenshot of the Activity and analyzing it programmatically.
Neither of these seem great to me, but I'm leaning towards 2). Any other ideas?
For testing onDraw, onLayout and onMeasurescreenshots-tests-for-android is considerable option.

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

The proper way to create a 2d android game

After getting help with starting android pong, I realized that I painted myself into a corner.
For reference on the code(Its long) and advice, here is a link to the last thread Updating player score in an android game
Basically the frame rate is halved when i touch the screen. I think it is due to updating everything or drawing on the canvas. This also lends itself to bad communication between the pieces of the application(the view, frame, and oncreate). Is there a recommended way to create an android 2d game? This is based off the lunar landing game and it seemed fine but when i tried to recreate it , it seemed much worse. I could not find a lot of resource on a best practice for drawing and so far this seems a bit discouraging.
So in short
How would you modify the code posted in the link to improve performance? Or did I paint myself into a poorly made corner, and it is time to start a new? If so , how?
Thanks
You don't need invalidate() in onTouch because you are already running a frame animation by the additional thread.
Try to define all the fixed values/variables in the constructor of the SurfaceView or in onSizeChanged() where you know the size of the canvas and bitmaps loaded, so not to make any unneccesary variable creation, requests or calcualtions in onDraw, onTouch or other methods to handle game logic, otherwise it will slow down.

Multiple animations in Android

I'm just starting to get the hang of animations, tweened animations that is.
I have made several imageviews with pictures rotating and moving, and its all very fun, but I don't know what the best approach for doing multiple animations is.
For example I created a LinearLayout and stuck some ImageViews in it and wrote this:
RotateAnimation imgrotate = new RotateAnimation(0,360,
RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
rotate.setDuration(2000);
someview.setAnimation(imgrotate);
someview.startAnimation(imgrotate);
for each imageview. So they all spin. But now I want to step it up a gear. But reaidng other sources on the net it seems some people use canvas to do animations. What is the best way?
There is no best way. You should use the simplest and most straightforward approach that gets the job done.
Canvas' are often used because developers want more specific control over how their objects are displayed on the canvas. For example, how an object gets displayed when it interacts with other objects or the user.

Categories

Resources