android不同版本 解析度不一樣,同一圖片測出的長和寬也不一樣!擷取螢幕的寬和高

來源:互聯網
上載者:User

    今天利用canvas.drawBitmap方法畫圖片的時候發現一個問題,用2.1版本做的時候是置中顯示的,

後來用2.2的時候卻發現同樣的圖片,卻不能置中顯示了。我看了,用的是同一圖片,後來想了想,估

計是與版本有關係,於是就測了下,發現同樣的測試代碼,在2.1版本測出螢幕長和寬為533*320圖片

的長和寬為480*320,2.2版本測出螢幕長和寬為800*480圖片的長和寬為720*480.

    看到這個結果我恍然大悟了,因為My Code裡是把螢幕的寬度和高度統一寫死為480*320,圖片的

座標是由螢幕和圖片的寬和高共同決定的,所以導致了這個結果。代碼大概如下:

Paint paint = new Paint();  //畫筆

int screenWidth=480;   //螢幕寬度
int screenHeight=320;  //螢幕高度 

Bitmap bm = BitmapFactory.decodeResource(activity.getResources(), R.drawable.bnkjs);//圖片

currentX=screenWidth/2-bm.getWidth()/2;  //圖片的X座標

currentY=screenHeight/2-bm.getHeight()/2; //圖片的Y座標

canvas.drawBitmap(bm, currentX, currentY, paint);

後來把螢幕的寬度和高度修改為擷取當前螢幕的寬度和高度後就可以正常置中顯示了,代碼如下:


Paint paint = new Paint(); //畫筆

int screenWidth=wm.getDefaultDisplay().getWidth();   //螢幕寬度
int screenHeight=wm.getDefaultDisplay().getHeight();  //螢幕高度

Bitmap bm = BitmapFactory.decodeResource(activity.getResources(), R.drawable.bnkjs);//圖片

currentX=screenWidth/2-bm.getWidth()/2; //圖片的X座標

currentY=screenHeight/2-bm.getHeight()/2; //圖片的Y座標

canvas.drawBitmap(bm, currentX, currentY, paint);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.