title: Android - invalidate() in onDraw()
body: <p>I created two class - one is a class which extends <code>SurfaceView</code> and has a method <code>onDraw()</code>, inside which I'm drawing an image on my canvas:</p>
  
  <pre><code>public class Board extends SurfaceView{
  
      public BitmapFactory myBitmapFactory = new BitmapFactory();
  
      public  Bitmap myBitmap = new Bitmap;
      public Board(Context context) {
          super(context);
          setFocusable(true);
      }
      //here i implement all the abstract classes, which are not important now
  
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);
          Paint paint = new Paint();
          canvas.drawBitmap(myBitmap, 20, 20, paint);
      }
      public void setBitmap(Bitmap b){
          this.myBitmap = b;
      }
  }
  </code></pre>
  
  <p>And I have also my activity class, where after a button click which runs the method <code>changeImage</code>. I want two change this image which i put on my canvas</p>
  
  <pre><code>public class MyActivity extends Activity {
      public Context context = this;
      public Board board;
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_start_game);
      }
      public void changeImage(View view){
          Bitmap tmpBitmap = new Bitmap();
          BitmapFactory tmpBitmapFactory = new BitmapFactory();
          tmpBitmap = Bitmap.createScaledBitmap(tmpBitmapFactory.decodeResource(getResources(), R.drawable.image2), 130, 130, false);
          board.setBitmap(tmpBitmap);
          board.invalidate();
      }
  }
  </code></pre>
  
  <p>But after calling method <code>changeImage</code> nothing is changing - the image is the same as at the beginning. I tried to use <code>invalidate()</code> also inside <code>setBitmap()</code> method, but it also doesn't work. Is this because every time when I call <code>invalidate()</code> I'm creating a new Bitmap or I'm doing something else wrong? </p>
  
  <p>---EDIT---
  So ofc, wrong was that: </p>
  
  <pre><code>myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);
  </code></pre>
  
  <p>was setting all the time the same image. I changed it, but still invalidate() thosent work. I mean the image is not changing. I was looking for the result in other topics, but I did't find any solution, now my classes looks like this:</p>
  
  <pre><code>  public class Board extends SurfaceView{
  
  
         public BitmapFactory myBitmapFactory = new BitmapFactory();
         public  Bitmap myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), size, size, false);
      public int tmp = 0;
          public Paint paint = new Paint();
         protected void onDraw(Canvas canvas) {
             Paint paint = new Paint();
             canvas.drawBitmap(myBitmap, 50, 50, paint);
      }
      public void setBitmaps(Bitmap one){
              this.paint = new Paint();
              this.myBitmap = one;
              this.tmp=4;
              this.invalidate();
  
          }
  </code></pre>
  
  <p>And the method changeImage() inside my activity:</p>
  
  <pre><code>public void changeImage(View view){
          Bitmap tmpBitmap1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.blue), 230, 230, false);
  myGameBoard.setBitmaps(tmpBitmap1);
  
  }
  </code></pre>
  
  <p>I tried to use: invalidate(), postInvalidate(), but it doesn't work and i hae now idea what am I doing wrong</p>