android圖像繪制(二)——放縮問題

android中圖像在畫佈上放大縮小時,圖像的邊框大小沒有改變!

原圖如下:

 

放大後:原來圖片的邊框沒有改變,位置依舊!

 

 

所以如果要放置圖片的位置的話,就需要做相應的位置移動才可以!

 

采用如下代碼(全屏放置圖片):

[java]
Matrix matrix = new Matrix(); 
        matrix.postScale(canvas.getWidth()*1.01f/bmpBg.getWidth(), canvas.getHeight()*1.01f/bmpBg.getHeight(), bmpBg.getWidth() / 2, bmpBg.getHeight() / 2); 
        matrix.postTranslate( (canvas.getWidth()-bmpBg.getWidth()) / 2, (canvas.getHeight()-bmpBg.getHeight()) / 2); 
        canvas.drawBitmap(bmpBg, matrix, paint); 
Matrix matrix = new Matrix();
  matrix.postScale(canvas.getWidth()*1.01f/bmpBg.getWidth(), canvas.getHeight()*1.01f/bmpBg.getHeight(), bmpBg.getWidth() / 2, bmpBg.getHeight() / 2);
  matrix.postTranslate( (canvas.getWidth()-bmpBg.getWidth()) / 2, (canvas.getHeight()-bmpBg.getHeight()) / 2);
  canvas.drawBitmap(bmpBg, matrix, paint);

 摘自 阿凱的專欄

 

發佈留言