Listener for onDraw method - android

Following is my onDraw method.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(inte < listOfPoints.size()){
canvas.drawLine(listOfPoints.get(0).x, listOfPoints.get(0).y, listOfPoints.get(inte).x,listOfPoints.get(inte).y, paint);
inte++;
if(inte < listOfPoints.size()){
invalidate();
}
}
}
Is it possible to get a callback once if the canvas has finished drawing the above point?

OnDraw is automatically called for situations that need to be drawn inside the view, so a Canvas is provided, so it will be called as long as the list size.

Related

How to display what I drew on a custom View's canvas only at certain times?

So I have a custom View which has a BitmapDrawable as its background which I draw to:
public void onDraw(Canvas canvas){
super.onDraw(canvas);
draw_game_map();
draw_player();
draw_overlay_with_circle();
}
(each of these methods just draws bitmap(s))
Now I'm calling View.draw(Canvas) 60 times per second and it works fine, except I want to have something like this:
public void onDraw(Canvas canvas){
super.onDraw(canvas);
//From this point, don't show what I'm about to draw
draw_game_map();
draw_player();
draw_overlay_with_circle();
//Show what I just drew
}
Any code I can add to only display the changes made only after I'm done drawing, rather than during?

how to tilt a canvas in the ondraw method inside a view

In my OnDraw method, I draw path into my canvas (circle with segements). All work well, now what I am not sure how to do is how can I tilt it (sort of rotation around one axis).
Basically, what I am trying to achieve is something like the tilt functionality of google map.
Here is the pseduo code:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// I do all the drawing here
canvas.tilt(45degrees) <-----??
}
I don't think you have to create the tilt effect within the onDraw method modifying your canvas manually. Instead use setRotationX() method on the whole View. You can also define it in xml as android:rotationX="45".
Use Canvas.concat(Matrix) with a rotation matrix, i.e.:
class MyView {
private final Matrix rotate = new Matrix();
public MyView()
{
rotate.setRotate(45.0f);
}
protected void onDraw(Canvas c)
{
super.onDraw(canvas);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.concat(rotate);
canvas.restore();
}
}
Note also that you should not do any memory allocations inside onDraw() as per the android linter.

Rotate ImageButton onCreate without Animation

Is it possible to rotate an ImageButton using the onCreate() function? Or do you have to use an Animation which starts onCreate()? Because with an Animation i can see a little "flick" on Activity start...
you can use ViewCompat.setRotation(buttonInstance, rotationAngle);. From the documentation
Sets the degrees that the view is rotated around the pivot point.
You can override the onDraw() method using a custom class that extends ImageButton (which I'm sure you have).
#Override
protected void onDraw(#NonNull Canvas canvas) {
super.onDraw(canvas);
// Rotate a Bitmap
final Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
canvas.drawBitmap(bitmap, matrix, null);
/*
* OR
**/
// Rotate the canvas
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.rotate(-angle);
canvas.drawBitmap(bitmap, left, top, null);
canvas.restore();
}
Choose one or the other solution, not both together ;)
EDIT
After some quick reflection, that could also work (not tested):
#Override
protected void onDraw(#NonNull Canvas canvas) {
canvas.rotate(-angle);
super.onDraw(canvas);
}

How do i clean drawn path on canvas in android

i need some help on my android project.
i have a canvas and i fill it with some bitmap.
and i have a pointer that drawn a line on the canvas.
my problem is how to clean the line that i've drawn before?
what method should i call on canvas?
i've tried Canvas.drawColor(), invalidate() and that's not working.
and what is the function of Canvas.drawColor() and
Please help me solve my problem.
thanks in advance
UPDATE!
if i made the code like this:
#Override
protected void onDraw(Canvas canvas) {
// fills the canvas with black
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
where i place invalidate() in my code?
and what code should i use if i want to clear the canvas using a button?
UPDATE!
i wrote my onDraw like this:
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if(letsdraw){
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
}
and the method in reset button is like this:
public void rst(){
letsdraw = false;
invalidate();
Log.v("tag", "this method called");
}
but no change in the canvas when i called the method.
did i wrote something wrong on the code above?
Make your logic like this. Draw the line on the Canvas with some condition. Check if you want to draw the line, then draw the line.
Skeleton code -
#Override
protected void onDraw(Canvas canvas)
{
if(needToDrawLine)
{
//draw the line
}
// Other drawing stuff
}
Now just update your needToDrawLine variable and call invalidate(). You'll get your result. Let me know if it works.
Update:
onDraw() method will call everytime you call the invalidate(). So everyting inside the onDraw() will execute. The way is, you have to prevent it from drawing some of the part. You'll call invalidate() when you want to redraw the whole view, for example - button for clear the canvas.
canvas.drawColor(Color.BLACK); this line clear your whole view to BLACK color.
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); this line draw the bitmap at (0,0).
canvas.drawPath(mPath, p); this line draw the path mPath.
obaby.draw(canvas); some other object draw itself.
Now - for example you want to clear the screen, when button pressed. Just initialize a variable if it draw everything. And update the variable in button click.
public boolean drawEverything = true;
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if(drawEverything)
{
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
}
public void buttonClicked( ... )
{
drawEverything = false;
}
Iam useing this errage the paint in my activity
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
mView.invalidate();

onDraw calling frequently

i have a method which displays a map. i call this method in onDraw method. but on Action move i need to redraw the map and need to call that method again but i am not getting canvas reference to redraw the map tiles. i used invalidate method but it start refreshing the onDraw frequently which made my map movement too to slow..
here is my onDraw method.
protected void onDraw(Canvas canvas)
{
Log.i("On Draw Call", "On Draw call");
mapMaker.getMapForScreenArea(map.getiScrnArea(), mapType, input, canvas);
invalidate();
this.canvas = canvas;
}
any help will be appreciative.
thanks a lot.
onDraw() gets called again and again because you invalidate() every time.
And also this.canvas = canvas is unnecessary.

Categories

Resources