【发布时间】:2014-07-27 10:30:00
【问题描述】:
我按照下面链接中的教程创建了类似 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);
}
【问题讨论】:
标签: android youtube android-videoview viewgroup