2011-09-15 71 views
4

我正在測量字符串文本以查看是否應爲包裝添加'\ n'。android將文本寬度(以像素爲單位)轉換爲屏幕寬度/高度的百分比

我使用Paint.measureText功能,它工作的大部分時間,但它發生,我認爲這是不準確的,因爲一個像素不等於一個DP - 我在網上看到如何轉換像素DP,而是我寧願做的是轉換像素屏幕大小的例如

如果「A」爲8個像素寬的百分比,我如何說

float LetterWidthPercent = _______ //get width of character in percent of screen width 
float LetterHeightPercent = _______ //get height of character in percent of screen height 

這樣我只能看到

if (LineOfTextWidth >= ScreenWidth * 0.9f) //90% 

這將是一個非常有用的功能,方便。

+0

@CrandellWS幫助我理解,你究竟想從這個問題到底究竟是什麼?是不是隻是通過getWindowManager()。getDefaultDisplay()。getMetrics(度量))來計算屏幕寬度並將測量的文本寬度除以它? (關於你提到的你自己的問題,只需根據'dp'的屏幕寬度設置fontSize) –

+0

我試圖吸引人們關注我自己的問題 - > http://stackoverflow.com/questions/35117278/canvas-drawtext -with-pixel-perfect-t在畫布上,我解決了這個問題,您可以使用該解決方案爲此問題創建答案。聲譽對我來說不是非常重要,所以請隨時收集簡單的50 ... – CrandellWS

+1

@GideonKain你可以使用佈局百分比支持庫,並可以給小部件提供百分比。你可以在這裏查看樣品。 http://www.androidauthority.com/using-the-android-percent-support-library-630715/ – androidnoobdev

回答

0

您可以使用以下方法將do轉換爲像素和像素爲dp。

轉換DP像素:

public int dpToPixel(int dp) { 
    DisplayMetrics dm= getContext().getResources().getDisplayMetrics(); 
    int pixel = Math.round(dp * (dm.xdpi/DisplayMetrics.DENSITY_DEFAULT));  
    return pixel 
} 

轉換像素DP:

public int pixelToDp(int pixel) { 
    DisplayMetrics dm = getContext().getResources().getDisplayMetrics(); 
    int dp = Math.round(pixel/(dm.xdpi/DisplayMetrics.DENSITY_DEFAULT)); 
    return dp; 
} 
+0

您將需要使用邊界來獲取像素的高度和寬度 – CrandellWS

+0

嘗試重新處理您的答案也許這將有助於http: //堆棧溢出。com/a/35193695/1815624 – CrandellWS

1

您需要通過DisplayMetrics還是怎麼過讓屏幕寬度...

爲了得到字符大小使用帶邊界的繪畫進行計算。

characterWidth/screenWidth = characterScreenPercentage

我做了一個雙,但你可以很容易地將其更改爲浮動。

對於人物:

public Double characterToScreenWidthPercentage(Character c) { 
    Paint paint = new Paint(); 
    Rect boundA = new Rect(); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)) 
      .getDefaultDisplay().getMetrics(metrics); 
    paint.getTextBounds(Character.toString(c), 0, 1, boundA); 
    Log.v("fn", Integer.toString(boundA.width())); 
    Log.v("fn", Integer.toString(metrics.widthPixels)); 
    Log.v("fn", Double.toString((float) boundA.width()/(float) metrics.widthPixels)); 
    return ((double) boundA.width()/(double) metrics.widthPixels); 
} 

對於字符串:

public Double stringToScreenWidthPercentage(String str) { 
    Paint paint = new Paint(); 
    Rect boundA = new Rect(); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)) 
      .getDefaultDisplay().getMetrics(metrics); 
    paint.getTextBounds(str, 0, 1, boundA); 
    Log.v("fn", Integer.toString(boundA.width())); 
    Log.v("fn", Integer.toString(metrics.widthPixels)); 
    Log.v("fn", Double.toString((float) boundA.width()/(float) metrics.widthPixels)); 
    return ((double) boundA.width()/(double) metrics.widthPixels); 
} 
1
public static double measureWeightPercent(Context context, String textToMeasure) { 
    DisplayMetrics metrics = new DisplayMetrics(); 
    ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)) 
      .getDefaultDisplay().getMetrics(metrics); 

    TextView view = new TextView(context); 
    view.setText(textToMeasure); 
    view.measure(metrics.widthPixels, metrics.heightPixels); 

    double textWidth = view.getMeasuredWidth(); 

    return textWidth/(metrics.widthPixels/100); 
} 

可以測量與繪製的文本是在屏幕上一個TextView裏面之前(或代替)。通過這種方式,您可以爲TextView設置任何字體或textSize,並始終知道文本在屏幕上有多少百分比。

+0

很好的答案,那會比我的建議更有用,更好的資源。 – CrandellWS

相關問題