安卓学习笔记---获取屏幕的宽高以及屏幕密度,最小宽度等同时可以可以获取屏幕的不同dimens进行适配

public class MainActivity extends Activity {


private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.text);


//也要看对应的屏幕最小宽度,这个最小宽度是和dp对应的,用下列代码既可:
Configuration config = getResources().getConfiguration();
int smallestScreenWidth = config.smallestScreenWidthDp;


//在代码中获取屏幕像素、屏幕密度 
DisplayMetrics metric = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metric); 
int width = metric.widthPixels; // 屏幕宽度(像素) 
int height = metric.heightPixels; // 屏幕高度(像素) 
float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5 / 2 / 3 / 4) 
//float xDpi=metric.xdpi;         //x方向上的密度
//float yDpi=metric.ydpi;         //y方向上的密度
int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240 / 320 / 480) 

//+xDpi+"\ny方向上的密度"+yDpi+"\n屏幕密度dpi-->"+densityDpi
String str="屏幕宽度-->"+width+"\n屏幕高度-->"+height+"\n屏幕密度(比率)-->"+density+"\n屏幕密度dpi-->"+densityDpi+"\n屏幕最小宽度dp-->"+smallestScreenWidth;
text.setText(str);
Log.i("Log.i", "屏幕宽度-->"+width+"\n屏幕高度-->"+height+"\n屏幕密度(比率)-->"+density+"\n屏幕密度dpi-->"+densityDpi+"\n屏幕密度dpi-->"+densityDpi+"\n屏幕最小宽度dp-->"+smallestScreenWidth);

}

}


//--------------------------------------------------------下面来说一下屏幕的适配

转自网址:

http://www.cnblogs.com/soaringEveryday/p/4835839.html

个人觉得很有道理,看自己觉得吧


附上可以在Eclipse和Studio都可以进行适配的工具

点击打开链接


AndroidStudio 获取dimen的代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class DimenToolAndroidStadio2 {

    public static void gen() {

        // AndroidStudio的dimens.xml的路径
        File file = new File("./app/src/main/res/values/dimens.xml");
        BufferedReader reader = null;
        // 设置屏幕的最小宽度
        StringBuilder sw240 = new StringBuilder();
        StringBuilder sw320 = new StringBuilder();
        StringBuilder sw360 = new StringBuilder();
        StringBuilder sw384 = new StringBuilder();
        StringBuilder sw392 = new StringBuilder();
        StringBuilder sw400 = new StringBuilder();
        StringBuilder sw432 = new StringBuilder();
        StringBuilder sw480 = new StringBuilder();
        StringBuilder sw600 = new StringBuilder();
        StringBuilder sw720 = new StringBuilder();
        StringBuilder sw768 = new StringBuilder();
        StringBuilder sw800 = new StringBuilder();
        StringBuilder w820 = new StringBuilder();

        try {
            System.out.println("生成不同分辨率:");
            reader = new BufferedReader(new FileReader(file));
            String tempString;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束

            while ((tempString = reader.readLine()) != null) {

                if (tempString.contains("</dimen>")) {
                    // tempString = tempString.replaceAll(" ", "");
                    String start = tempString.substring(0,
                            tempString.indexOf(">") + 1);
                    String end = tempString.substring(tempString
                            .lastIndexOf("<") - 2);
                    int num = Integer.valueOf(tempString.substring(
                            tempString.indexOf(">") + 1,
                            tempString.indexOf("</dimen>") - 2));
                    sw240.append(start).append((int) Math.round(num * 0.67))
                            .append(end).append("\n");
                    sw320.append(start).append((int) Math.round(num * 0.89))
                            .append(end).append("\n");
                    sw360.append(tempString).append("\n");
                    sw384.append(start).append((int) Math.round(num * 1.07))
                            .append(end).append("\n");
                    sw392.append(start).append((int) Math.round(num * 1.09))
                            .append(end).append("\n");
                    sw400.append(start).append((int) Math.round(num * 1.11))
                            .append(end).append("\n");
                    sw432.append(start).append((int) Math.round(num * 1.2))
                            .append(end).append("\n");
                    sw480.append(start).append((int) Math.round(num * 1.33))
                            .append(end).append("\n");
                    sw600.append(start).append((int) Math.round(num * 1.67))
                            .append(end).append("\n");
                    sw720.append(start).append((int) Math.round(num * 2))
                            .append(end).append("\n");
                    sw768.append(start).append((int) Math.round(num * 2.13))
                            .append(end).append("\n");
                    sw800.append(start).append((int) Math.round(num * 2.22))
                            .append(end).append("\n");
                    w820.append(start).append((int) Math.round(num * 2.27))
                            .append(end).append("\n");

                } else {
                    sw240.append(tempString).append("\n");
                    sw320.append(tempString).append("\n");
                    sw360.append(tempString).append("\n");
                    sw384.append(tempString).append("\n");
                    sw392.append(tempString).append("\n");
                    sw400.append(tempString).append("\n");
                    sw432.append(tempString).append("\n");
                    sw480.append(tempString).append("\n");
                    sw600.append(tempString).append("\n");
                    sw720.append(tempString).append("\n");
                    sw768.append(tempString).append("\n");
                    sw800.append(tempString).append("\n");
                    w820.append(tempString).append("\n");
                }
                line++;
            }
            reader.close();
            System.out.println("<!--  sw240 -->");
            System.out.println(sw240);
            System.out.println("<!--  sw320 -->");
            System.out.println(sw320);
            System.out.println("<!--  sw360 -->");
            System.out.println(sw360);
            System.out.println("<!--  sw384 -->");
            System.out.println(sw384);
            System.out.println("<!--  sw392 -->");
            System.out.println(sw392);
            System.out.println("<!--  sw400 -->");
            System.out.println(sw400);
            System.out.println("<!--  sw432 -->");
            System.out.println(sw432);
            System.out.println("<!--  sw480 -->");
            System.out.println(sw480);
            System.out.println("<!--  sw600 -->");
            System.out.println(sw600);

            System.out.println("<!--  sw720 -->");
            System.out.println(sw720);
            System.out.println("<!--  sw768 -->");
            System.out.println(sw768);
            System.out.println("<!--  sw800 -->");
            System.out.println(sw800);

            System.out.println("<!--  w820 -->");
            System.out.println(w820);

            String sw240file = "./app/src/main/res/values-sw240dp/dimens.xml";
            String sw320file = "./app/src/main/res/values-sw320dp/dimens.xml";
            String sw360file = "./app/src/main/res/values-sw360dp/dimens.xml";
            String sw384file = "./app/src/main/res/values-sw384dp/dimens.xml";
            String sw392file = "./app/src/main/res/values-sw392dp/dimens.xml";
            String sw400file = "./app/src/main/res/values-sw400dp/dimens.xml";
            String sw432file = "./app/src/main/res/values-sw432dp/dimens.xml";
            String sw480file = "./app/src/main/res/values-sw480dp/dimens.xml";
            String sw600file = "./app/src/main/res/values-sw600dp/dimens.xml";
            String sw720file = "./app/src/main/res/values-sw720dp/dimens.xml";
            String sw768file = "./app/src/main/res/values-sw768dp/dimens.xml";
            String sw800file = "./app/src/main/res/values-sw800dp/dimens.xml";
            String w820file = "./app/src/main/res/values-w820dp/dimens.xml";
            writeFile(sw240file, sw240.toString());
            writeFile(sw320file, sw320.toString());
            writeFile(sw360file, sw360.toString());
            writeFile(sw384file, sw384.toString());
            writeFile(sw392file, sw392.toString());
            writeFile(sw400file, sw400.toString());
            writeFile(sw432file, sw432.toString());
            writeFile(sw480file, sw480.toString());
            writeFile(sw600file, sw600.toString());
            writeFile(sw720file, sw720.toString());
            writeFile(sw768file, sw768.toString());
            writeFile(sw800file, sw800.toString());
            writeFile(w820file, w820.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void writeFile(String file, String text) {
        PrintWriter out = null;
        try {
            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.close();
    }

    public static void main(String[] args) {
        gen();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值