Friday, September 7, 2012

Draw bitmap on canvas

Modify onDraw(Canvas canvas) method of MyView.java in the former exercise to draw bitmap on convas of our custom view.

 @Override
 protected void onDraw(Canvas canvas) {

  float w, h, cx, cy;
  w = getWidth();
  h = getHeight();
  cx = w/2;
  cy = h/2;

  canvas.drawRect(0, 0, w, h, BackPaint);
  
  Bitmap myBitmap = BitmapFactory.decodeResource(
    getResources(), 
    R.drawable.ic_launcher);
        canvas.drawBitmap(myBitmap, cx, cy, null);
  
 };


Draw bitmap on canvas


download filesDownload the files.


No comments: