Android: Text not visible on writing using drawText() - android

#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.

Related

Wear OS animation Android

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.

How to get hardware accelerated Canvas in onDraw() of CanvasWatchFaceService?

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
) {

passing instructions from UI thread to Rendering thread (GLSurfaceView)

The Setup : A RelativeLayout with a GLSurfaceView and a Button as shown in the image..
The Problem: Lets say I have other triangle models (The one in the picture being the initial model)... I wish to change the models cyclically on click of the button. Since button is on the UI thread and glSurfaceView runs on a separate thread, I don't exactly know how to pass the info/instruction to it. I know there is this thing called Handler in Android which might be useful in this case... But I need some help here..
Edit: If Handler is the right way, I need to know how to add Looper to that Handler... The documentation says add looper.prepare() at the start of run() method.. But the glSurfaceView creates thread implicitly, resulting in no run() method directly available..
I don't think it is necessary to use handlers to solve this issue but you may need to adjust the way you organise your classes.
Here is an example of an organisational structure that might solve your issue:
Activity Class
public class MainActivity extends Activity {
private int modelNumber = 0;
private ArrayList<Model> models = new ArrayList<Model>();
private YourRendererClass renderer;
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Setup GLSurfaceView
GLSurfaceView surface = new GLSurfaceView(this);
setContentView(surface);
renderer = new YourRendererClass();
surface.setRenderer(renderer);
// Set up models
models.add(new Model(x, y, size etc..));
models.add(new Model(x, y, size etc..));
models.add(new Model(x, y, size etc..));
etc.
// Display first model
renderer.setCurrentModel(models.get(modelNumber));
...
}
// Called by the button press:
// Use android:onClick="onClick"
// in your layout xml file within button
public void onClick(View view){
// Make it loop round
modelNumber++;
if(modelNumber>=models.size()){
modelNumber=0;
}
// Display current model
renderer.setCurrentModel(models.get(modelNumber));
}
}
Renderer Class
public class YourRendererClass implements Renderer {
private Model currentModel;
#Override
public void onDrawFrame(GL10 gl) {
// ** Your existing set-up code **//
// Draw model
if (currentModel!=null){
currentModel.draw(gl);
}
}
public void setCurrentModel(Model model){
currentModel = model;
}
}
Model class
public class Model {
// Holds model information
private int size;
private int x;
private int y;
// etc...
public model(int x, int y, int size etc...){
this.x=x;
this.y=y;
this.size=size;
// etc....
}
public void draw(GL10 gl) {
// ** Draw model based on model information fields above **
}
}
The above code is untested as I don't have access to your drawing code but the structure should work if implemented correctly. I've tried to make it clear where you'll have to insert your own code to make it work. In particular I wasn't sure what defines each of your different models so you'll need to include sufficient local variables within the Model class to define them.
I hope my answer helps, let me know if you have any questions.
Tim
You should look at queueEvent! It's a very convenient way to pass informations from UI Thread to renderer Thread:
queueEvent(new Runnable(){
#Override
public void run() {
mRenderer.method();
}});

How can I pass an int to my onDraw(Canvas canvas) in Android?

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));
}

How can I be informed that the UI has been redrawn in Android?

Hey guys... My application requires that I be informed of any changes to the UI of my application. Is there anyway that I can be informed of this? Perhaps through means of an event or something?
You can try overriding the onDraw() method of View. Something like
public class MyView extends View{
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("MyView", "onDraw() called");
}
...

Categories

Resources