Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AutoFitTextureView : TextureView
- {
- private int mRatioWidth = 0;
- private int mRatioHeight = 0;
- public AutoFitTextureView(Android.Content.Context context) : this(context, null)
- {
- }
- public AutoFitTextureView(Android.Content.Context context, IAttributeSet attrs) : this(context, attrs, 0)
- {
- }
- public AutoFitTextureView(Android.Content.Context context, IAttributeSet attrs, int defStyle) : base(context,attrs,
- defStyle)
- {
- }
- public void SetAspectRatio(int width, int height)
- {
- if (width == 0 || height == 0)
- throw new ArgumentException("Size cannot be negative.");
- mRatioWidth = width;
- mRatioHeight = height;
- RequestLayout();
- }
- protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
- {
- base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
- int width = MeasureSpec.GetSize(widthMeasureSpec);
- int height = MeasureSpec.GetSize(heightMeasureSpec);
- if (0 == mRatioWidth || 0 == mRatioHeight)
- {
- SetMeasuredDimension(width, height);
- }
- else
- {
- if (width < (float)height * mRatioWidth / (float)mRatioHeight)
- {
- SetMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
- }
- else
- {
- SetMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement