Guest User

Untitled

a guest
May 2nd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. int totalLineCount = textView.getLineCount();
  2. int lineHeight = textView.getLineHeight();
  3. int totlaHeight = totalLineCount * lineHeight;
  4.  
  5. float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
  6. int px = scaledDensity*sp;
  7.  
  8. ViewTreeObserver viewTreeObserver = textView.getViewTreeObserver();
  9. viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  10. @Override
  11. public void onGlobalLayout() {
  12. int width = textView.getWidth();
  13. int height= textView.getHeight()
  14. textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  15. }
  16. });
  17.  
  18. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
  19.  
  20. paint.setStyle(Paint.Style.FILL);
  21. paint.setColor(color);
  22. paint.setTextAlign(Paint.Align.CENTER);
  23. paint.setTextSize(textSize); //размер шрифта в **пикселях**
  24.  
  25. Rect bounds = new Rect();
  26. String text = "your text here";
  27. paint.getTextBounds(text , 0, text.length(), bounds);
  28.  
  29. int height = bounds.height();//высота
  30. int width = bounds.width();//ширина
  31.  
  32. textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
  33. int height = textView.getMeasuredHeight();
  34. int width = textView.getMeasuredWidth();
Add Comment
Please, Sign In to add comment