android 获取屏幕的密度和像素 点

经过测试发现屏幕的像素点 如果

density =1 那么 1dp = 1px

density =2 那么 1dp = 2px

density =3 那么 1dp = 3px

在字体设置的时候用dp比用px好,因为这样可以防止在不同的分辨率上显示的字体大小不同



textView_bacMsg = (TextView) findViewById(R.id.textView_bacMsg);
        bacImg = (ImageView) findViewById(R.id.bacImg);
          drawable = new MyProgressDrawable(this);
        
        
          // 获取屏幕密度(方法2)  
        DisplayMetrics dm = new DisplayMetrics();  
        dm = getResources().getDisplayMetrics();  
          
        float density  = dm.density;        // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)  
        int densityDPI = dm.densityDpi;     // 屏幕密度(每寸像素:120/160/240/320)  
        float xdpi = dm.xdpi;             
        float ydpi = dm.ydpi;  
          
       int screenWidth  = dm.widthPixels;      // 屏幕宽(像素,如:480px)  
       int  screenHeight = dm.heightPixels;     // 屏幕高(像素,如:800px)  
    
       
        RelativeLayout.LayoutParams  layoutParams =new  RelativeLayout.LayoutParams(-2, -2);
        layoutParams.width = screenWidth-200;
        layoutParams.height = screenWidth-200;
        layoutParams.topMargin = (int) ((screenHeight - 240*density - ( screenWidth-200))/2);
        layoutParams.leftMargin = (int) ((screenWidth- ( screenWidth-200))/2);
        bacImg.setLayoutParams(layoutParams);
        bacImg.setBackgroundDrawable(drawable);
        drawable.start();
        
        
        RelativeLayout.LayoutParams  layoutParams_text =(LayoutParams) textView_bacMsg.getLayoutParams();
        layoutParams_text.topMargin =(int) ((screenHeight - 240*density - (layoutParams_text.height))/2);
        
        Log.e("中间的字体宽度", layoutParams_text.width+"");
        layoutParams_text.leftMargin = (int) ((screenWidth - (20*4*density))/2);
        textView_bacMsg.setLayoutParams(layoutParams_text);
   

转载于:https://my.oschina.net/tslin/blog/478647

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值