Android手机分辨率测试程序

该程序可以测试出个人手机设备的分辨率,属于哪种dpi级别,以便开发参考。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical" >
	
	<TextView 
		android:id="@+id/screenSize"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="@string/screenSize"
		/>
	
	<EditText 
		android:id="@+id/size"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:inputType="numberDecimal"
		/>
	<Button
		android:id="@+id/submit"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="@string/submit"
		/>
	<EditText 
		android:id="@+id/showWidth"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:inputType="text"
		android:editable="false"
		/>
	
	<EditText
		android:id="@+id/showDPI"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:inputType="text"
		android:editable="false"
		/>
	
	
	
</LinearLayout>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="hello">Hello World, AndroidDPI!</string>
	<string name="app_name">AndroidDPI</string>
	<string name="screenSize">输入该设备的屏幕大小:</string>
	<string name="submit">确认</string>
</resources>

Java代码:

package com.shine.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AndroidDPI extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText size = (EditText) findViewById(R.id.size);
        final EditText showWidth = (EditText) findViewById(R.id.showWidth);
        final EditText showDPI = (EditText) findViewById(R.id.showDPI);
        Button submit = (Button) findViewById(R.id.submit);
        
        submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(size.length() == 0){
					size.setText("请输入该屏幕的尺寸");
				}else {
					DisplayMetrics dm = new DisplayMetrics();
					getWindowManager().getDefaultDisplay().getMetrics(dm);
					int width = dm.widthPixels;
					int height =dm.heightPixels;
					float sizeInt = Float.parseFloat(size.getText().toString());
					showWidth.setText("手机屏幕分辨率为:"+width+"*"+height);
					double dpi = Math.sqrt(height*height +width*width)/sizeInt;
					String dpiType = "";
					if(dpi >320){
						dpiType = "属于xhdpi";
					}else if(dpi>240){
						dpiType = "属于hdpi";
					}else if(dpi>160){
						dpiType = "属于mdpi";
					}else if(dpi>120){
						dpiType = "属于ldpi";
					}
					showDPI.setText("手机DPI是:"+ dpi+ ","+ dpiType);
				}
			}
		});
    }
}
显示结果:



发布了41 篇原创文章 · 获赞 44 · 访问量 18万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览