AndikaK
AndikaK

Reputation: 137

Android - I want... after click button, drawing image in canvas

I click dice button in arenalayout.xml (this xml is displayed by 'ArenaLayout' class). But canvas doesn't draw char 2.I want to... if I click dice button then I draw char 2.

Note : After I click dice button, value of 'haveFirstDiced' variable in Arena class change to TRUE. There's condition inside of onDraw in 'Arena' class. . . if 'haveFirstDiced' variable have TRUE value then drawing char 2.

     public class ArenaLayout extends Activity {

        private Arena arena;  
        ImageButton diceButton;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.arena_layout);
            diceButton = (ImageButton) findViewById(R.id.dice);
            dice();
        }

        private void dice() {
            diceButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    arena.dicing();
                }
            });
        }
}

This is 'Arena' class :

 public class Arena extends View{

    private Paint p;
    private Handler threadHandler = new Handler();
    private Bitmap char1;
    private float x = 20, y = 300;
    Canvas c;
    boolean haveFirstDiced = false; 

    public Arena(Context context, AttributeSet aSet) {
        super(context, aSet);
        p = new Paint();
     }

    public void dicing() {
        new Thread(new XXX()).start();
    }

      @Override
    synchronized public void onDraw(Canvas canvas) {
      char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);    
      if(haveFirstDiced == true) {
              canvas.drawBitmap(char2,x,y,null);
          }
    }

   class XXX implements Runnable { 
       @Override
       public void run() {
               threadHandler.post(new Runnable() {
                   @Override
                   public void run() {
                       haveFirstDiced = true;      
                   }
               });
           }
       }


  }

Upvotes: 0

Views: 1335

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93708

This is the most convoluted solution I've ever seen. There's no need for a thread, a runnable, or for onDraw to be synchronized. Here's what it should be:

private void dice() {
    diceButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            arena.dicing();
        }
    });
}


 public class Arena extends View{

    private Paint p;
    private Handler threadHandler = new Handler();
    private Bitmap char2;
    private float x = 20, y = 300;
    Canvas c;
    boolean haveFirstDiced = false; 

    public Arena(Context context, AttributeSet aSet) {
        super(context, aSet);
        p = new Paint();
        char2 = BitmapFactory.decodeResource(getResources(), R.drawable.char_2);    
     }

    public void dicing() {
        haveFirstDiced = true;
        invalidate();
    }

      @Override
    public void onDraw(Canvas canvas) {
      if(haveFirstDiced == true) {
              canvas.drawBitmap(char2,x,y,null);
          }
    }



  }

Upvotes: 1

Related Questions