如何擷取Android手機螢幕高寬值

來源:互聯網
上載者:User

擷取Android手機螢幕高寬值的方法,大概有兩種,一種是通過View提供的方法擷取,還有一種是通過Display對象擷取螢幕高寬。

下面這篇文章介紹的很詳細,有興趣的朋友可以過去看下,原文地址如下:http://www.iteye.com/topic/828830 網上還有其他的文章,有興趣的朋友可以自己試試。

我這裡主要跟大家分享的是如何通過DisplayMetrics的參數擷取螢幕的寬高.下面我貼出源碼,跟大家分享下.因為注視寫得都很清楚,我就不做過多說明了.

import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.DisplayMetrics;import android.widget.TextView;public class DisplayActivity extends Activity {    /* 首先聲明一個文本控制項,用於顯示擷取的寬和高 */    private TextView TV = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 載入布局檔案        setContentView(R.layout.main);        // 根據控制項的ID找到相應的控制項        TV = (TextView) findViewById(R.id.tv);        // 要擷取螢幕的寬和高等參數,首先需要聲明一個DisplayMetrics對象,螢幕的寬高等屬性存放在這個對象中        DisplayMetrics DM = new DisplayMetrics();        // 擷取視窗管理器,擷取當前的視窗,調用getDefaultDisplay()後,其將關於螢幕的一些資訊寫進DM對象中,最後通過getMetrics(DM)擷取        getWindowManager().getDefaultDisplay().getMetrics(DM);        int wdip = px2dip(getApplicationContext(), getApplicationContext()                .getResources().getDisplayMetrics().widthPixels);        int hdip = px2dip(getApplicationContext(), getApplicationContext()                .getResources().getDisplayMetrics().heightPixels);        // 列印擷取的寬和高        TV.setText("densityDpi: "                + DM.densityDpi                + "\n"                + "density: "                + DM.density                + "\n"                + "scaledDensity: "                + DM.scaledDensity                + "\n"                + "heightPixels(The absolute height of the display in pixels.): \n "                + DM.heightPixels                + "\n"                + "widthPixels(The absolute width of the display in pixels.): \n "                + DM.widthPixels                + "\n"                + "xdpi(The exact physical pixels per inch of the screen in the X dimension): \n "                + DM.xdpi                + "\n"                + "ydpi(The exact physical pixels per inch of the screen in the Y dimension): \n "                + DM.ydpi + "\n"                + "wdip: " + wdip + "\n"                + "hdip: " + hdip + "\n");    }    /**     * Change value from pix to dip     *      * @param context     * @param pxValue     * @return     */    public final static int px2dip(Context context, float pxValue) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (pxValue / scale + 0.5f);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.