2014-05-14 71 views
5

我正在使用DraggablePanel库(https://github.com/pedrovgs/DraggablePanel)来促进类似YouTube的视频播放器。如果您不熟悉此功能,它基本上允许用户将当前正在播放的视频缩小为停靠在屏幕角落的小缩略图。VideoView不会缩放内容以匹配其父项

不幸的是(我不太确定这是上述库的问题)我注意到,如果我在VideoView的父视图上应用X和Y比例,VideoView本身不会调整它的内容大小比赛。

请参阅下面的截图,其中VideoView处于自然规模,然后将其父视图缩小到约0.6:0.6。您应该注意到,在缩放的屏幕截图中,VideoView裁剪了其内容,而不是调整大小以适合其大小。

VideoView properly scaled 1:1

VideoView parent scaled to 0.6:0.6

所以,我想作为其母公司的规模变化,迫使宽度和高度上VideoView。有了解重写VideoView的尺寸在互联网上的几个例子 - 但这里是我的简单版本:

public class ScalableVideoView extends VideoView { 

    private int mVideoWidth; 
    private int mVideoHeight; 

    public ScalableVideoView(Context context) { 
     super(context); 
    } 

    public ScalableVideoView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ScalableVideoView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     mVideoWidth = 0; 
     mVideoHeight = 0; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     if (mVideoWidth > 0 && mVideoHeight > 0) { 
      // If a custom dimension is specified, force it as the measured dimension 
      setMeasuredDimension(mVideoWidth, mVideoHeight); 
     } else { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 

    public void changeVideoSize(int width, int height) { 
     mVideoWidth = width; 
     mVideoHeight = height; 

     getHolder().setFixedSize(width, height); 

     requestLayout(); 
     invalidate(); 
    } 

} 

包含VideoView负责调用changeVideoSize上它时,它接收来自一个通知的片段DraggablePanel库关于比例变化。在这一点上,我根据提供的比例值计算出一对新的宽度和高度。 (规模从0f到1f)

public void setVideoViewScale(float scaleX, float scaleY) { 
     if (mMaxVideoWidth == 0) { 
      mMaxVideoWidth = mVideoView.getWidth(); 
     } 

     if (mMaxVideoHeight == 0) { 
      mMaxVideoHeight = mVideoView.getHeight(); 
     } 

     if (mMaxVideoWidth > 0) { 

      mVideoView.changeVideoSize((int) (scaleX * mMaxVideoWidth), 
        (int) (scaleY * mMaxVideoHeight)); 
     } 
    } 

不幸的是,这会导致一些非常有趣的结果。看来VideoView的视频部分可以适当缩放 - 但VideoView的边界看起来缩放得太快(导致视频缩小和裁剪)。

ScalingVideoView at 0.5 scale

为了进一步证明这里有两个视频:

我还上传了该示例项目GitHub上,这样就可以看到完整的代码: https://github.com/npike/so_scalevideoview

回答

3

我与我的应用程序有同样的问题,所以我把你的例子简单得多,并试图修复它。
这是修复问题的代码。编辑您的changeVideoSize(INT宽度,高度INT)方法:

public void changeVideoSize(int width, int height){ 
    mVideoWidth = width; 
    mVideoHeight = height; 

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height); 
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
    lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 
    setLayoutParams(lp); 

    requestLayout(); 
    invalidate(); 
} 

随着你强制VideoView重绘整个视图的新布局参数视图的失效。

1

我相信VideoView不支持缩放。在DraggablePanel附带的示例应用程序中,有一个VideoSampleActivity,它演示了一个可拖动的视频。为了实现类似缩放的效果,在activity_video_sample中将top_view_resize属性设置为true。XML:

<com.github.pedrovgs.DraggableView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:draggable_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/draggable_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    draggable_view:top_view_id="@+id/video_view" 
    draggable_view:bottom_view_id="@+id/iv_thumbnail" 
    draggable_view:top_view_x_scale_factor="@dimen/x_scale_factor" 
    draggable_view:top_view_y_scale_factor="@dimen/y_scale_factor" 
    draggable_view:top_view_height="@dimen/top_fragment_height" 
    draggable_view:top_view_margin_right="@dimen/top_fragment_margin" 
    draggable_view:top_view_margin_bottom="@dimen/top_fragment_margin" 
    draggable_view:enable_minimized_horizontal_alpha_effect="false" 
    draggable_view:top_view_resize="true" 
    android:background="@color/black"> 

设置top_view_resize为true,可使TransformerFactory中返回一个ResizeTransformer而不是ScaleTransformer的。 ResizeTransformer使用setLayoutParams()来调整关联视图的大小,其中ScaleTransformer应用比例因子。

而不是尝试缩放VideoView,你应该能够将top_view_resize设置为true。

注意:如果您使用DraggablePanel(支持顶部和底部片段)而不是DraggableView,那么您当前无法访问嵌套在DraggablePanel内的DraggableView的top_view_resize属性。我认为这是一个错误,我希望我可以与Pedro合作在DraggablePanel上公开一个将其值传递给嵌套DraggableView的top_view_resize属性。由于这个bug,DraggablePanel总是使用默认的Transformer,它是ScaleTransformer--它不适用于VideoView。