Advertisement
Guest User

Untitled

a guest
Aug 5th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package az.dgtl.egg.ui.widget;
  2.  
  3. import android.content.Context;
  4. import android.support.v7.widget.CardView;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7.  
  8. /**
  9. * Based on CommonsWare's AspectLockedFrameLayout
  10. */
  11. public final class AspectLockedCardView extends CardView {
  12. private double aspectRatio = 0.0;
  13. private AspectRatioSource aspectRatioSource = null;
  14.  
  15. public AspectLockedCardView(Context context) {
  16. super(context);
  17. }
  18.  
  19. public AspectLockedCardView(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. }
  22.  
  23. @Override
  24. protected void onMeasure(int widthSpec, int heightSpec) {
  25. double localRatio = aspectRatio;
  26.  
  27. if (localRatio == 0.0 && aspectRatioSource != null
  28. && aspectRatioSource.getHeight() > 0) {
  29. localRatio =
  30. (double) aspectRatioSource.getWidth()
  31. / (double) aspectRatioSource.getHeight();
  32. }
  33.  
  34. if (localRatio == 0.0) {
  35. super.onMeasure(widthSpec, heightSpec);
  36. } else {
  37. int lockedWidth = MeasureSpec.getSize(widthSpec);
  38. int lockedHeight = MeasureSpec.getSize(heightSpec);
  39.  
  40. if (lockedWidth == 0 && lockedHeight == 0) {
  41. throw new IllegalArgumentException(
  42. "Both width and height cannot be zero -- watch out for scrollable containers");
  43. }
  44.  
  45. // Get the padding of the border background.
  46. int hPadding = getPaddingLeft() + getPaddingRight();
  47. int vPadding = getPaddingTop() + getPaddingBottom();
  48.  
  49. // Resize the preview frame with correct aspect ratio.
  50. lockedWidth -= hPadding;
  51. lockedHeight -= vPadding;
  52.  
  53. if (lockedHeight > 0 && (lockedWidth > lockedHeight * localRatio)) {
  54. lockedWidth = (int) (lockedHeight * localRatio + .5);
  55. } else {
  56. lockedHeight = (int) (lockedWidth / localRatio + .5);
  57. }
  58.  
  59. // Add the padding of the border.
  60. lockedWidth += hPadding;
  61. lockedHeight += vPadding;
  62.  
  63. // Ask children to follow the new preview dimension.
  64. super.onMeasure(MeasureSpec.makeMeasureSpec(lockedWidth, MeasureSpec.EXACTLY),
  65. MeasureSpec.makeMeasureSpec(lockedHeight, MeasureSpec.EXACTLY));
  66. }
  67. }
  68.  
  69. public void setAspectRatioSource(View v) {
  70. this.aspectRatioSource = new ViewAspectRatioSource(v);
  71. }
  72.  
  73. public void setAspectRatioSource(AspectRatioSource aspectRatioSource) {
  74. this.aspectRatioSource = aspectRatioSource;
  75. }
  76.  
  77. // from com.android.camera.PreviewFrameLayout, with slight
  78. // modifications
  79.  
  80. public void setAspectRatio(double aspectRatio) {
  81. if (aspectRatio <= 0.0) {
  82. throw new IllegalArgumentException("aspect ratio must be positive");
  83. }
  84.  
  85. if (this.aspectRatio != aspectRatio) {
  86. this.aspectRatio = aspectRatio;
  87. requestLayout();
  88. }
  89. }
  90.  
  91. public interface AspectRatioSource {
  92. int getWidth();
  93.  
  94. int getHeight();
  95. }
  96.  
  97. private static class ViewAspectRatioSource implements AspectRatioSource {
  98. private View v = null;
  99.  
  100. ViewAspectRatioSource(View v) {
  101. this.v = v;
  102. }
  103.  
  104. @Override public int getWidth() {
  105. return (v.getWidth());
  106. }
  107.  
  108. @Override public int getHeight() {
  109. return (v.getHeight());
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement