Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public class AutoFitTextureView : TextureView
  2. {
  3. private int mRatioWidth = 0;
  4. private int mRatioHeight = 0;
  5.  
  6. public AutoFitTextureView(Android.Content.Context context) : this(context, null)
  7. {
  8. }
  9. public AutoFitTextureView(Android.Content.Context context, IAttributeSet attrs) : this(context, attrs, 0)
  10. {
  11. }
  12. public AutoFitTextureView(Android.Content.Context context, IAttributeSet attrs, int defStyle) : base(context,attrs,
  13. defStyle)
  14. {
  15. }
  16. public void SetAspectRatio(int width, int height)
  17. {
  18. if (width == 0 || height == 0)
  19. throw new ArgumentException("Size cannot be negative.");
  20. mRatioWidth = width;
  21. mRatioHeight = height;
  22. RequestLayout();
  23. }
  24.  
  25. protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
  26. {
  27. base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
  28. int width = MeasureSpec.GetSize(widthMeasureSpec);
  29. int height = MeasureSpec.GetSize(heightMeasureSpec);
  30. if (0 == mRatioWidth || 0 == mRatioHeight)
  31. {
  32. SetMeasuredDimension(width, height);
  33. }
  34. else
  35. {
  36. if (width < (float)height * mRatioWidth / (float)mRatioHeight)
  37. {
  38. SetMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
  39. }
  40. else
  41. {
  42. SetMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
  43. }
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement