Efficient Invalidate() with Dirty Rect

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Efficient Invalidate() with Dirty Rect

Postby snailfur » Tue Apr 01, 2008 7:24 pm

Hey peepes,

Just thought I'd share a little tip when drawing onto a view using the .invalidate() function with you lovely people. All tutorials on here which I've seen so far all use MyView.invalidate();, which if your view is fullscreen is one cpu whore.

Its always better when drawing, especially on mobile devices with lower cpu's, to only ever draw exactly what you need. To do this we pass a Rect as the parameter to invalidate().

Example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyActivity extends Activity
  2.  
  3. {
  4.  
  5.         Handler loopTheLoop = new Handler()
  6.  
  7.         {
  8.  
  9.                 public void handleMessage()
  10.  
  11.                 {
  12.  
  13.                         int x = 0;
  14.  
  15.                         int y = 100;
  16.  
  17.                         int width = 0;
  18.  
  19.                         int height = 100;
  20.  
  21.                         Rect invalidRect = new Rect(x, y, width, height);
  22.  
  23.                         view.invalidate(invalidRect);
  24.  
  25.                 }
  26.  
  27.         }
  28.  
  29. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


This will only redraw the specified area defined by Rect instead of the whole screen meaning less pressure on the cpu/gpu. Which is especially useful for any fullscreen or smooth animation.

Hope it helps someone ;)
snailfur
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 08, 2008 5:22 pm

Top

Postby m.aghasyan » Wed Nov 26, 2008 8:26 pm

Hi,

As I know invalidating only some area of the view causes to be re-drawn all the views (children) which bounds are overlapped with the invalid area.
I have the following situation: I have one custom view without children and I want to be able to draw the specific area of my view. The reason is that I have many small bitmaps/animations in this view and I don't want to draw all this small bitmaps on ALL animations' ALL steps. Each animation's each step should re-draw only it's area.
Can anyone help me?

Thanks in advance.
Best Regards,
Mikayel Aghasyan
m.aghasyan
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Nov 26, 2008 8:12 pm
Location: Yerevan, Armenia

Postby dinoguan » Fri Nov 28, 2008 9:49 am

This is an awesome tip thanks. I will have to implement this and report any increase in fps.
dinoguan
Freshman
Freshman
 
Posts: 4
Joined: Tue Nov 11, 2008 6:01 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests