Advertisement
Guest User

Untitled

a guest
Apr 11th, 2016
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. /**
  2. * A {@link TextureView} that can be adjusted to a specified aspect ratio.
  3. */
  4. public class AutoFitTextureView extends TextureView {
  5.  
  6. private int mRatioWidth = 0;
  7. private int mRatioHeight = 0;
  8.  
  9. public AutoFitTextureView(Context context) {
  10. this(context, null);
  11. }
  12.  
  13. public AutoFitTextureView(Context context, AttributeSet attrs) {
  14. this(context, attrs, 0);
  15. }
  16.  
  17. public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
  18. super(context, attrs, defStyle);
  19. }
  20.  
  21. /**
  22. * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
  23. * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
  24. * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
  25. *
  26. * @param width Relative horizontal size
  27. * @param height Relative vertical size
  28. */
  29. public void setAspectRatio(int width, int height) {
  30. if (width < 0 || height < 0) {
  31. throw new IllegalArgumentException("Size cannot be negative.");
  32. }
  33. mRatioWidth = width;
  34. mRatioHeight = height;
  35. requestLayout();
  36. }
  37.  
  38. @Override
  39. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  40. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  41. int width = MeasureSpec.getSize(widthMeasureSpec);
  42. int height = MeasureSpec.getSize(heightMeasureSpec);
  43. if (0 == mRatioWidth || 0 == mRatioHeight) {
  44. setMeasuredDimension(width, height);
  45. } else {
  46. if (width < height * mRatioWidth / mRatioHeight) {
  47. setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
  48. } else {
  49. setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
  50. }
  51. }
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement