eneim / toro

Video list auto playback made simple, specially built for RecyclerView
Apache License 2.0
1.42k stars 253 forks source link

zoom/crop video to fit the view #136

Open jpage4500 opened 7 years ago

jpage4500 commented 7 years ago

Hi,

In our list (recyclerview) we want to make all video's the same width and height (basically full screen width and a fixed height).

We were using this library to help with center-cropping the video: https://github.com/yqritc/Android-ScalableVideoView

I worked on switching over to using Toro and so far am loving everything except for the fact that it seems like Toro will always 'fit' the entire video to be displayed 'inside' the view (keeping the aspect ratio). So, certain videos will end up not displayed as tall as the view.

What we really want is to fill the entire view regardless of the aspect ratio of the video.. if it can't fit entirely I'd like to use 'center crop'. In our case a user can always click on the video to view it full-screen (without the cropping).

I'm wondering if there's a way to support cropping like this in Toro? Maybe it's been done before and I'm just not looking in the right place?

Here's a better visual of what I'm looking for (using ImageView as the example): https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide

I think this is how Toro is sizing it's contents and would need another flag to handle center cropping (or any cropping for that matter).

Please let me know! If it makes sense I can try to help get that added as well too.

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (videoAspectRatio == 0) {
      // Aspect ratio not set.
      return;
    }

    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    float viewAspectRatio = (float) width / height;
    float aspectDeformation = videoAspectRatio / viewAspectRatio - 1;
    if (Math.abs(aspectDeformation) <= MAX_ASPECT_RATIO_DEFORMATION_FRACTION) {
      // We're within the allowed tolerance.
      return;
    }

    switch (this.resizeMode) {
      case RESIZE_MODE_FIXED_WIDTH:
        height = (int) (width / videoAspectRatio);
        break;
      case RESIZE_MODE_FIXED_HEIGHT:
        width = (int) (height * videoAspectRatio);
        break;
      case RESIZE_MODE_DEFAULT:
      default:
        if (aspectDeformation > 0) {
          height = (int) (width / videoAspectRatio);
        } else {
          width = (int) (height * videoAspectRatio);
        }
        break;
    }

    super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
  }
eneim commented 7 years ago

Hi @jpage4500 , scaling or animating over VideoView requires TextureView instead of SurfaceView, if you are Ok with this, I can give you some advice. Looking for your reply.

eneim commented 7 years ago

@jpage4500 2.2.0-SNAPSHOT adds "ExoPlayerView" which internally use "SimpleExoPlayerView" that has the ability to fit Video surface. Can you take a look?

shashankjamdade commented 6 years ago

@eneim @jpage4500 @thiagoricieri @fossabot How can I get zoomed preview (Bitmap) of the video in textureview?? I used getBitmap() but it gives unprocessed bitmap i.e. before zoomed preview. I apply the transformed matrix on this bitmap it gives me zoomed preview starting from the (0,0) i.e. top left corner of bitmap only, not currently focused area of the bitmap (I used Bitmap.createBitmap(MyTextureView.this.getBitmap() , 0, 0, 1024, 768, matrix, false); ). plz help me to resolve the problem.