在android中缩放/最小化YoutubePlayerView时调整视频大小

时间:2014-07-27 10:30:01

标签: android youtube android-videoview viewgroup

我已按照以下链接中的教程创建类似YouTube的功能,可将YouTube播放器视图最小化到屏幕的右下角。

http://flavienlaurent.com/blog/2013/08/28/each-navigation-drawer-hides-a-viewdraghelper/

但是,视频当前不会缩放到缩小窗口的大小并保留原始大小。

如何更新YoutubePlayerView以更改要随缩放更改的视频大小?

view.xml用:

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.views.YoutubeVideoView
        android:id="@+id/youtubeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible" >

        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtube_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/viewDesc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF00FF"
            android:gravity="center"
            android:tag="desc"
            android:text="Loreum Loreum"
            android:textColor="@android:color/white"
            android:textSize="35sp" />
    </com.example.views.YoutubeVideoView>
</FrameLayout>

DragHelperCallback包含在我的YoutubeVideoView类中,它扩展了ViewGroup:

private class DragHelperCallback extends ViewDragHelper.Callback {

    @Override
    public boolean tryCaptureView(View child, int pointerId) {
        return child == mVideoView;
    }

    @Override
    public void onViewPositionChanged(View changedView, int left, int top,
            int dx, int dy) {
        mTop = top;

        mDragOffset = (float) top / mDragRange;

        mVideoView.setPivotX(mVideoView.getWidth());
        mVideoView.setPivotY(mVideoView.getHeight());
        mVideoView.setScaleX(1 - mDragOffset / 2);
        mVideoView.setScaleY(1 - mDragOffset / 2);
        mDescView.setAlpha(1 - mDragOffset);

        requestLayout();
    }

    @Override
    public void onViewReleased(View releasedChild, float xvel, float yvel) {
        int top = getPaddingTop();
        if (yvel > 0 || (yvel == 0 && mDragOffset > 0.5f)) {
            top += mDragRange;
        }
        mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top);
    }

    @Override
    public int getViewVerticalDragRange(View child) {
        return mDragRange;
    }

    @Override
    public int clampViewPositionVertical(View child, int top, int dy) {
        final int topBound = getPaddingTop();
        final int bottomBound = getHeight() - mVideoView.getHeight()
                - mVideoView.getPaddingBottom();

        final int newTop = Math.min(Math.max(top, topBound), bottomBound);
        return newTop;
    }

}

在我的YoutubeVideoView类中重写了onLayout方法,该类扩展了ViewGroup

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mDragRange = getHeight() - mVideoView.getHeight();

    mVideoView.layout(0, mTop, r, mTop + mVideoView.getMeasuredHeight());

    mDescView
            .layout(0, mTop + mVideoView.getMeasuredHeight(), r, mTop + b);
}

1 个答案:

答案 0 :(得分:0)

我知道这已经过时但我使用DraggablePanel时遇到了类似的问题。 DraggablePanel基于ViewDragHelper。如果您调整YouTubePlayerView的高度和宽度布局参数,播放视频也会调整。我不知道为什么缩放视图不会调整播放视频。我更多地谈论我的解决方案here。 DraggablePanel库中的ResizeTransformer也可以帮助您。