0
votes

In my custom view i have 1 animation that i need to run at demand (on tile click). Currently i am using this method:

 public boolean onTouchEvent(MotionEvent event) {
            switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:
    //check what tile for clicked
                    getHandler().removeCallbacks(explosionThread); 
                    getHandler().post(explosionThread);
                    }

                }
            break;
        }
        return super.onTouchEvent(event);

    }

So i am calling (or sending to view thread, to be specific) a Runnable that calls it self until it comes to an end of an image...

private Runnable explosionThread=new Runnable(){
        @Override
        public void run() {
            invalidate();
            if(expCount<15){
            getHandler().postDelayed(this, 10);
            }
        }   
    };

In my onDraw() method i implemented logic to go threw bitmap and draw it on screen ( using cnavas.drawBitmap(bitmap,srcRect,destRect,paint)....

Now, I want to avoid using SurfaceView (i have only 1 animation and View uses less resources). I think the animation is slow because onDraw needs to draw whole screen each time invalidate() is called witch can be slow ( drawing 64 tiles with png images). I know that there is an overload of invalidate method, invalidate(Rect dirty) but i don't really know how to use it. If u think that that is the answer please write how to avoid drawing whole onDraw method ( or what method can I overwrite that is used by invalidate(Rect) method, if there is any).

If you have any other better way to speed up animation post it plz. Thanks in advance....

2

2 Answers

0
votes

That's right. One of the way to speed up rendering through canvas is to use invalidate(Rect). Rect passed to invalidate method defines area which will be redrawn. Your onDraw will be called after invalidate with clipping region being set up on canvas. So all your "drawBitmap" will be clipped by the rect.

0
votes

for running the animation are using a .gif file or you are using a sequence of images run on a thread to show as an animation ?