Different resolution support android

前端 未结 8 1314
北海茫月
北海茫月 2020-11-22 04:21

Edited Question:

Mobile Resolution:
I would like to design different screen dpi like following resolutions.
320x480,
48

相关标签:
8条回答
  • 2020-11-22 04:55

    To avoid the problems of different screen resolutions and different densities, I size and position everything based on the percentage width and height of the screen. If text is resized based on the percentage width or height of the screen, the font will be the correct size on all devices and all resolutions. To get the correct font size based on a width and height of its space, simply use this function:

    private float SetTextSize(String text, int width, int height)
    {
        Paint paint = new Paint();
        float textWidth = paint.measureText(text);
        float textSize = (int) ((width / textWidth) * paint.getTextSize());
        paint.setTextSize(textSize);
    
        textWidth = paint.measureText(text);
        textSize = (int) ((width / textWidth) * paint.getTextSize());
    
        // Re-measure with font size near our desired result
        paint.setTextSize(textSize);
    
        // Check height constraints
        FontMetricsInt metrics = paint.getFontMetricsInt();
        float textHeight = metrics.descent - metrics.ascent;
        if (textHeight > height)
        {
            textSize = (int) (textSize * (height / textHeight));
            paint.setTextSize(textSize);
        }
        return textSize;
    }
    

    Here's the code to get the width and height of the screen:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
    {
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        screenWidth = size.x;
        screenHeight = size.y; 
    }
    else
    {
        Display display = getWindowManager().getDefaultDisplay(); 
        screenWidth = display.getWidth(); 
        screenHeight = display.getHeight(); 
    }
    

    To get the font size you want, simply make up an area based on the screen width and screen height and tweak it until the font size looks right. Once you get it looking right, it should look right on all devices.

    float textSize = SetTextSize("text", (int) (screenWidth * 0.1), (int) (screenHeight * 0.15));
    

    I hope this helps you

    0 讨论(0)
  • 2020-11-22 04:55

    First your application design for one resolution .

    example : suppose your mobile resolution 380*480

           mobile screen width:380
    
           textView size is 80dp   
           Assume : if width is 380dp then 100 % then
    
                   textview width 80dp then how many %(per).
    
                ower answer is: 25 %
    

    find screen size programmatically using belove formula

        DisplayMetric  displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        ScreenHeight = displaymetrics.heightPixels;
        ScreenWidth = displaymetrics.widthPixels;
            txt_height = (int)((ScreenHeight*14.58)/100);
        txt_width = (int)((ScreenWidth*37.5)/100);
    
    
        LinearLayout.LayoutParams normal_param = new LinearLayout.LayoutParams(txt_height ,txt_width );
    
        txt_Name.setLayoutParams(normal_param);
    
    0 讨论(0)
提交回复
热议问题