2011-04-14 156 views
1

我正在使用画布在Android中显示图像。无论屏幕的大小如何,我都希望此图像占据屏幕的中心。那么我怎么能实现它。如何使用画布在屏幕中央放置图像

这是我试过的片段,请让我知道你的想法。

@Override 
     protected void onDraw(Canvas canvas) { 
     .......... 
      .......... 
      sampleImage.draw(canvas,getWidth(),getHeight()); // This moves the image to right end of the view. 
      .......... 
      .......... 

任何形式的帮助,高度赞赏。

谢谢。

回答

1
sampleImage.draw(
     canvas, 
     getWidth()/2 - imageWidth/2, 
     getHeight()/2 - imageHeight/2 
); 

;)

+0

我也认为你应该过载你的观点的OnSizeChanged,然后保存视图的宽度和高度。 – Lukas 2011-04-14 10:05:34

+0

嗨卢卡斯,感谢您的回应,我尝试了以下方式,但仍然没有得到正确的解决方案。 (getResources(),R.drawable.loader).getWidth()/ 2,getHeight()/ 2 - BitmapFactory.decodeResource(getResources(),Rdrawable .loader).getHeight()/ 2); – 2011-04-14 10:22:48

+0

它仍然在一些右侧。不完全在我看来的中心。如果您有任何想法请帮助。谢谢... – 2011-04-14 10:24:35