0

這不關於AntiAlias,請在回答謝謝之前閱讀問題。canvas.drawText與畫布上的像素完美文字

ADDING賞金 - >android convert text width (in pixels) to percent of screen width/height

質量信息 - >https://stackoverflow.com/a/1016941/1815624

試圖繪製在畫布上的文字時,我得到不一致的結果。請幫忙,謝謝。

我希望文字消耗比例相同的空間量上

我不關心ANTI_ALIAS並增加了paint.setFlags(Paint.ANTI_ALIAS_FLAG);但問題是相同的所有設備......

在1個屏幕文本消耗寬度的一半,在另一個只佔1/3,甚至使用全寬。

我希望他們都使用等量的屏幕房地產。

對於實施例

1600x900的7英寸平板奇巧物理: inconsistent results

1920.1080 5.2奇巧物理 inconsistent results

1600x900的20英寸棒棒糖仿真 inconsistent results

1280×720 4。7寸 inconsistent results

這些已經使用指南:http://www.gkproggy.com/2013/01/draw-text-on-canvas-with-font-size.html

其中作爲教程顯示一致的結果 consistent results

要徹底在這裏創造的源泉:

public class MainActivity extends Activity 
{ 
    Paint paint; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     paint = new Paint(); 
     View test = new TestView(this); 

     setContentView(test); 
    } 

    public class TestView extends View 
    { 

     public TestView(Context context) 
     { 
      super(context); 
      setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     } 

     public float pixelsToSp(Context context, float px) { 
      float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; 
      return px/scaledDensity; 
     } 

     public float spToPixels(float sp) { 
      Context context = getContext(); 
      float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; 
      return scaledDensity*sp; 
     } 

     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      int size = getResources().getDimensionPixelSize(R.dimen.sp20); 
      int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()); 
      Log.v("intSize", Integer.toString(size)); 
      Log.v("intSize", Integer.toString(px)); 
      Log.v("intSize", Float.toString(spToPixels(20f))); 
      if(true) { 
       Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Ultra.ttf"); 
       paint.setTypeface(myTypeface); 
      } 
      paint.setColor(Color.BLACK); 
      paint.setTextSize(size); 
      paint.setFlags(Paint.ANTI_ALIAS_FLAG); 
      canvas.drawText("HELLOOOOOOOOOOOOOO!", 0, size, paint); 
      super.onDraw(canvas); 
     } 
    } 
} 

我已嘗試暗示 https://stackoverflow.com/a/5369766/1815624

展望本

https://stackoverflow.com/a/21895626/1815624

https://stackoverflow.com/a/14753968/1815624

https://stackoverflow.com/a/21895626/1815624 < - 用現在正試圖

+0

我真的不知道你在問什麼,但要獲得「平滑」邊緣可以在繪畫對象上設置消除鋸齒爲真 –

+0

我希望文本在所有設備上消耗相同數量的縮放空間 – CrandellWS

+1

它確實看起來像t嘿。你是否希望它們跨越屏幕寬度/高度的相同*百分比*,或者如果用尺子測量它們,它們的尺寸是相同的? –

回答

0

資源:

https://stackoverflow.com/a/4847027/1815624

https://stackoverflow.com/a/21895626/1815624

得到屏幕寬度,文本大小與屏幕比率的一些參考,並計算。這樣的結果是:

better results

better results

通知第二HELLOOOOOOOOOOOOOOOOO!

public class MainActivity extends Activity 
{ 
    Paint paint; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     paint = new Paint(); 
     View test = new TestView(this); 

     setContentView(test); 
    } 

    public class TestView extends View 
    { 

     public TestView(Context context) 
     { 
      super(context); 
      setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     } 

     public float pixelsToSp(float px) { 
      Context context = getContext(); 
      float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; 
      return px/scaledDensity; 
     } 

     public float spToPixels(float sp) { 
      Context context = getContext(); 
      float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; 
      return scaledDensity*sp; 
     } 

     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      int size = getResources().getDimensionPixelSize(R.dimen.sp20); 
      int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()); 
      Log.v("intSize", Integer.toString(size)); 
      Log.v("intSize", Integer.toString(px)); 
      Log.v("intSize", Float.toString(spToPixels(20f))); 

      DisplayMetrics metrics = new DisplayMetrics(); 
      getWindowManager().getDefaultDisplay().getMetrics(metrics); 

      Log.v("intSize hm", Integer.toString(metrics.heightPixels)); 
      Log.v("intSize wm", Integer.toString(metrics.widthPixels)); 

      if(true) { 
       Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Ultra.ttf"); 
       paint.setTypeface(myTypeface); 
      } 
      paint.setColor(Color.BLACK); 
      paint.setTextSize(size); 
      paint.setFlags(Paint.ANTI_ALIAS_FLAG); 
      canvas.drawText("HELLOOOOOOOOOOOOOO!", 0, size, paint); 
      setTextSizeForWidth(paint, metrics.widthPixels * 0.5f, "HELLOOOOOOOOOOOOOO!"); 
      canvas.drawText("HELLOOOOOOOOOOOOOO!", 0, size*3, paint); 
      super.onDraw(canvas); 
     } 
     /** 
     * Sets the text size for a Paint object so a given string of text will be a 
     * given width. 
     * 
     * @param paint 
     *   the Paint to set the text size for 
     * @param desiredWidth 
     *   the desired width 
     * @param text 
     *   the text that should be that width 
     */ 
     private void setTextSizeForWidth(Paint paint, float desiredWidth, 
               String text) { 

      // Pick a reasonably large value for the test. Larger values produce 
      // more accurate results, but may cause problems with hardware 
      // acceleration. But there are workarounds for that, too; refer to 
      // https://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache 
      final float testTextSize = 48f; 

      // Get the bounds of the text, using our testTextSize. 
      paint.setTextSize(testTextSize); 
      Rect bounds = new Rect(); 
      paint.getTextBounds(text, 0, text.length(), bounds); 

      // Calculate the desired size as a proportion of our testTextSize. 
      float desiredTextSize = testTextSize * desiredWidth/bounds.width(); 

      // Set the paint for that size. 
      paint.setTextSize(desiredTextSize); 
     } 

    } 
} 

請注意,如果你不延長活動使用

DisplayMetrics metrics = new DisplayMetrics(); 
    WindowManager windowManager = (WindowManager) context 
      .getSystemService(Context.WINDOW_SERVICE); 
    windowManager.getDefaultDisplay().getMetrics(metrics); 

,而不是

DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics);