三种方式获得手机屏幕的宽和高

本文介绍了在Android开发中获取手机屏幕宽度和高度的三种常见方法,包括使用DisplayMetrics,WindowManager以及View的测量方法。详细展示了相关代码实现,帮助开发者了解如何在实际项目中获取屏幕尺寸信息。
摘要由CSDN通过智能技术生成


主要代码:


package com.km.screeninfo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        private TextView tvModel;
        private TextView tvScreenHeight1;
        private TextView tvScreenHeight2;
        private TextView tvScreenHeight3;
        private TextView tvScreenWidth1;
        private TextView tvScreenWidth2;
        private TextView tvScreenWidth3;
        private TextView tvScreenDensity2;
        private TextView tvScreenDensityDPI2;
        private TextView tvScreenDensity3;
        private TextView tvScreenDensityDPI3;
        private TextView tvScreenWidth4;
        private TextView tvScreenHeight4;

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            tvModel = (TextView) rootView.findViewById(R.id.tv_model);
            tvScreenHeight1 = (TextView) rootView.findViewById(R.id.tv_screen_height1);
            tvScreenHeight2 = (TextView) rootView.findViewById(R.id.tv_screen_height2);
            tvScreenHeight3 = (TextView) rootView.findViewById(R.id.tv_screen_height3);
            tvScreenHeight4 = (TextView) rootView.findViewById(R.id.tv_screen_height4);

            tvScreenWidth1 = (TextView) rootView.findViewById(R.id.tv_screen_width1);
            tvScreenWidth2 = (TextView) rootView.findViewById(R.id.tv_screen_width2);
            tvScreenWidth3 = (TextView) rootView.findViewById(R.id.tv_screen_width3);
            tvScreenWidth4 = (TextView) rootView.findViewById(R.id.tv_screen_width4);

            tvScreenDensity2 = (TextView) rootView.findViewById(R.id.tv_screen_density2);
            tvScreenDensityDPI2 = (TextView) rootView.findViewById(R.id.tv_screen_densityDPI2);

            tvScreenDensity3 = (TextView) rootView.findViewById(R.id.tv_screen_density3);
            tvScreenDensityDPI3 = (TextView) rootView.findViewById(R.id.tv_screen_densityDPI3);

            tvModel.setText(android.os.Build.MODEL);

            // 获取屏幕密度(方法1)
            int screenWidth1 = getActivity().getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px)
            int screenHeight1 = getActivity().getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p)

            tvScreenHeight1.setText(screenHeight1 + " px");
            tvScreenWidth1.setText(screenWidth1 + " px");

            // 获取屏幕密度(方法2)
            DisplayMetrics dm2 = getResources().getDisplayMetrics();

            float density = dm2.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
            int densityDPI = dm2.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
            float xdpi = dm2.xdpi;
            float ydpi = dm2.ydpi;

            int screenWidth2 = dm2.widthPixels; // 屏幕宽(像素,如:480px)
            int screenHeight2 = dm2.heightPixels; // 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值