I am developing a Watch Face for android wear. I want to make the Canvas hardware accelerated.
In manifest I declared it like bellow
<application
android:hardwareAccelerated="true"
..>
<service
android:name=".WatchFaceMain"
android:hardwareAccelerated="true"
..>
</application>
But when I checked it from inside of My onDraw(Canvas canvas, Rect bounds) method canvas.isHardwareAccelerated() returns false. which clearly means my canvas is not hardware accelerated.
#Override
public void onDraw(Canvas canvas, Rect bounds) {
...
boolean isAccelarated = canvas.isHardwareAccelerated();
.......
}
How do I make this canvas Hardware Accelerated?
Finally found a solution for this issue. Though it wasn't a straight forward or easy solution.
First I got to copy the whole android.support.wearable.watchface.CanvasWatchFaceService class from librar, which my WatchFaceMain class extended.
Then I had to edit the draw() Method inside Engine Class like bellow
public abstract class CanvasWatchFaceService extends WatchFaceService {
.....
public class Engine extends android.support.wearable.watchface.WatchFaceService.Engine {
...
private void draw(SurfaceHolder holder) {
this.mDrawRequested = false;
Canvas canvas = holder.getSurface().lockHardwareCanvas();
if(canvas != null) {
try {
this.onDraw(canvas, holder.getSurfaceFrame());
} finally {
holder.getSurface().unlockCanvasAndPost(canvas);
}
}
}
}
}
I edited two lines inside onDraw like bellow.
from
Canvas canvas = holder.lockCanvas();
To
Canvas canvas = holder.getSurface().lockHardwareCanvas();
And From
holder.unlockCanvasAndPost(canvas);
To
holder.getSurface().unlockCanvasAndPost(canvas);
I still wonder why did CanvasWatchFaceService didn't offer any easier way to request for a hardware accelerated canvas!
More detail solution is available here in this Article.
Hopefully this answer helps someone else too.
With newer versions of the SDK, it's much easier. The CanvasWatchFaceService.Engine class has a boolean useHardwareCanvas constructor parameter. In your subclass, call the constructor as needed:
public Engine() {
super(true);
}
With the new APIs https://developer.android.com/reference/kotlin/androidx/wear/watchface/WatchFaceService, use CanvasType.HARDWARE
override suspend fun createWatchFace(
surfaceHolder: SurfaceHolder,
watchState: WatchState,
complicationSlotsManager: ComplicationSlotsManager,
currentUserStyleRepository: CurrentUserStyleRepository
) = WatchFace(
WatchFaceType.ANALOG,
object : Renderer.CanvasRenderer2<MySharedAssets>(
surfaceHolder,
currentUserStyleRepository,
watchState,
CanvasType.HARDWARE,
interactiveDrawModeUpdateDelayMillis = 16,
clearWithBackgroundTintBeforeRenderingHighlightLayer = true
) {
Related
How can i animate a line on a canvas in WearOS every 5 seconds?
I know that we have to use an AnimatorTask with a call to postInvalidate(). However since I am directly drawing the line on the canvas, I do not have a View object.
public class AnimatorTask extends TimerTask {
private WatchEventInfo eventInfo;
public AnimatorTask(WatchEventInfo eventInfo) {
this.eventInfo = eventInfo;
}
#Override
public void run() {
drawAndAnimate();
}
public WatchEventInfo getEventInfo() {
return eventInfo;
}
private void drawAndAnimate() {
Canvas canvas = eventInfo.getCanvas();
// For testing
canvas.drawLine(100, 100, 300, 300 markerPaint);
}
}
How do get access to the canvas.drawLine() method object and notify the canvas to redraw itself from the TimerTask assuming that my TimerTask exists in CanvasWatchFaceService subclass?
You get access to the canvas by overriding the onDraw(Canvas canvas, Rect bounds) method declared in the CanvasWatchFaceService.Engine class.
If you create your watch face using the New Project wizard in Android Studio, you'll notice a method at the end of the MyWatchFace class called handleUpdateTimeMessage(). This logic will ensure that your onDraw method gets called at the interval specified by INTERACTIVE_UPDATE_RATE_MS (default is once per second) in interactive mode.
Add a check in your onDraw method to only trigger your line animation if five seconds have past since the last update.
I have the following situation I want to solve. I wrote my own small drawing engine based on a Canvas. I created for every renderable a base class, for example, line, text, point, etc. From these base classes, I created a few derived entity classes. I also implemented a layer management system which is based on an observer-pattern to toggle the visibility of the derived entity classes. So far so good.
Every derived entity class has its own onDraw function where something is drawn on the canvas e.g
#Override
public void onDraw(Canvas canvas) {
if(isVisible) {
if (p1X != 0 && p1Y != 0 && p2X != 0 && p2Y != 0) {
canvas.drawLine(
p1X,
p1Y,
p2X,
p2Y,
paint
);
}
}
}
The crucial part I want to improve is
if(isVisible){
...
}
I really want to do something like this
#isVisible
#Override
public void onDraw(Canvas canvas) {
...
}
If I would do this in Python, I would write a Decorator to wrap the onDraw function.
But well, I want to write in Java and I'm a little bit clueless about how to do it. Maybe with an annotation (as I have shown it above as an idea), or are there other ways to do it?
I have 2 separate Android app (apk).
App 1 creates SurfaceView inside it and should provide AIDL methods for other apps to obtain an instance of SurfaceHolder for such SurfaceView. So the other apps will be able to draw on that view, displayed inside app number 1.
I was able to transfer Surface itself via aidl easily, since it implements Parcelable interface.
// IMainService.aidl
package com.commonsware.cwac.preso.demo.service;
import android.view.Surface;
interface IMainService {
Surface getSurf();
}
But 3-rd party sdk need SurfaceHolder to draw on. So the question is - how can I create SurfaceHolder for a given Surface instance, or how can I transfer SurfaceHolder via AIDL. Is there any examples of how I can implement Parcelable for SurfaceHolder?
My use-case (if its matter): App 1 starts as a Service and draw UI on Presentation Screen. And I need a way to get another apps display Navigation Data via Nokia Here Mobile SDK inside app 1. I need SurfaceHolder in order to use this api.
Any help will be appreciated.
After a while, finally create a solution that works well with Here OffscreenRender API.
At one app I create SurfaceHolder and this app provide AIDL (see at the post above). With this AIDL I was able to correctly send Surface object and then, on app 2 side, make SurfaceHolder from it.
public class MySurfaceHolder implements SurfaceHolder {
private Surface surface;
private Rect rect;
MySurfaceHolder(Surface surface, Rect rect) {
this.surface = surface;
this.rect = rect;
}
///....
#Override
public Canvas lockCanvas() {
return surface.lockCanvas(this.rect);
}
#Override
public Canvas lockCanvas(Rect rect) {
return surface.lockCanvas(rect);
}
#Override
public void unlockCanvasAndPost(Canvas canvas) {
surface.unlockCanvasAndPost(canvas);
}
#Override
public Rect getSurfaceFrame() {
return rect;
}
#Override
public Surface getSurface() {
return surface;
}
}
Basically, its my own SurfaceHolder, which get initialized by my own, instead of system. This approach need extend AIDL to be able to request Rect sizes, as well.
Hope that will be helpful for someone.
I use a surfaceview to draw a pie chart in Android. In order to know how big the pie slice should be I need to pass a parameter to the onDraw() method. How can I do this? Inside the onDraw() I make a query to a datahelper-class that fetches the right data.
I tried to call a static function in one Activity from the onDraw, and which function returned an integer. But I want something more dynamic, so I can send the integers I need to from the Activity to the onDraw and just get the result in form of a pie chart.
Any suggestions?
One of solutions can be like this:
public class MySurfaceView extends SurfaceView
{
private MyParameter parameter;
public void setParameter(MyParameter parameter)
{
this.parameter=parameter;
}
#Override
public void onDraw(Canvas canvas)
{
if(this.parameter==null)
return; //nothing to draw...
//draw here...
}
}
//somewhere in code
MySurfaceView sView=(SurfaceView )findViewById(R.id.pie_chart);
//
sView.setParameter(new MyParameter(100,2000, false));
}
#Override
protected void onDraw(Canvas canvas)
{
//Note:I do not want to use the canvas object from this function param
//If i do so its working , But i would like to understand why the following is not working
Canvas c =new Canvas();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
c.drawText("HELLO CANVAS",200,300,paint);
}
MORE CODE
public class graphicProj extends Activity {
private Canvas canvas;
#Override
public void onCreate(Bundle savedInstanceState) {
{
....
SimpleView simpleview_obj = new SimpleView(this);
setContentView(simpleview_obj);
simpleview_obj.onDraw(canvas);
.....
new GetData().execute();
}
private static class SimpleView extends View {
private ShapeDrawable mDrawable = new ShapeDrawable();
....
protected void onDraw(Canvas canvas) {
//draw graphic objects
....
}
}
public class GetData extends AsyncTask<Void, String, Void> {
#Override
protected void onPreExecute() {
Log.d("PROJ","STARTIN");
}
#Override
protected Void doInBackground(Void... unused) {
////My calculation and reading frm DataStream
}
#Override
protected void onProgressUpdate(String... data) {
//I Keep updating the result...
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
canvas.drawText(result, 200, 300, paint);
}
#Override
protected void onPostExecute(Void unused) {
Log.d("PROJ","END");
}
}
}
Not here or in your other question have you provided enough information on why you can't do that. There is no reason to draw on a new canvas instead of the already existing one.
The code is not working because your new Canvas c isn't assigned to anything. Its like creating a String myString for a log but never using Log.d(tag, myString)
edit (after reading all the comments)
If you calculate a value in your onCreate() and want to display that value in your onDraw(), that simply do that. Store the result in a member variable and you can access it in the onDraw().
Otherwise: Please provide your complete code. I guess you just do it way more complex than it should be...
edit2
Your code is a bit messy and does a lot of stuff in areas where you shouldn't do it. So drawing inside the onProgressUpdate() is seriously wrong. You should encapsulate your calculation and drawing.
What you should do (I recommend using SurfaceView instead of View, anyway...):
You should start your AsynchTask which updates the string you want to draw. The string should be a variable inside your View, where you use it for drawing.
The drawing itself should be called by a drawing thread (I remember: use the SurfaceView instead of the View as a parent class). Inside that onDraw() you should just use your paint object, the given canvas and the string you want to draw (don't forget to make the paint variable also a member variable to prevent recreating the same object over and over again for performance/memory reasons).
If you do not know how to work with a SurfaceView or if you want to learn how you could work with a drawing thread please read my tutorial about 2d drawing: www.droidnova.com/2d-tutorial-series
A short last sentence: You did a lot of things in the right way, you just mixed up with the places where you do it. You should try to rethink what you really want to achieve and how it could be done the easiest way. Maybe my tutorial helps to clear your mind a bit.