先上效果图
视频模块的布局是这样的:
<RelativeLayout
android:id="@+id/first_view_rl"
android:layout_width="510dp"
android:layout_height="291dp"
android:layout_marginLeft="@dimen/smart_hospital_home_marginLeft"
android:layout_marginTop="@dimen/smart_hospital_home_margin"
android:focusable="true"
android:padding="5dp"
android:visibility="invisible">
<FrameLayout
android:id="@+id/first_view_fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@color/black"
android:focusable="false">
<VideoView
android:id="@+id/first_view_vv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:focusable="false" />
</FrameLayout>
</RelativeLayout>
实现方案:
外层的RelativeLayout获取焦点时,执行放大逻辑:这里的xyScale自定义个值就可以了,比如宽高都放大1.25倍,xyScale就是1.25
view.bringToFront();
ViewPropertyAnimator animator = view.animate().scaleX(xyScale).scaleY(xyScale).setDuration(0);
animator.start();
关键点:此时VideoView不会跟着放大,因此VideoView用代码动态改变它的宽高
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) first_view_vv.getLayoutParams();
layoutParams.height = 321;
layoutParams.width = 530;
first_view_vv.setLayoutParams(layoutParams);
总结:VideoView的外层包裹通过Android提供的放大动画来做,VideoView本身需要真正的去改变它的大小。
附:为了方便放大效果的实现,封装工具类如下,可以指定绝对值尺寸放大,比如宽放大20,高按宽的比例放大
public class ViewAnimatorUtil { public static void zoomIn(View view, float xyScale) { ViewPropertyAnimator animator = view.animate().scaleX(xyScale).scaleY(xyScale); animator.start(); } public static void zoomIn(View view, float xScale, float yScale) { ViewPropertyAnimator animator = view.animate().scaleX(xScale).scaleY(yScale).setDuration(0); animator.start(); } public static void zoomIn(View view) { zoomIn(view, 1.0f); } public static void zoomOut(View view, float xyScale) { view.bringToFront(); ViewPropertyAnimator animator = view.animate().scaleX(xyScale).scaleY(xyScale).setDuration(0); animator.start(); } public static void zoomOut(View view, float xScale, float yScale) { ViewPropertyAnimator animator = view.animate().scaleX(xScale).scaleY(yScale).setDuration(0); animator.start(); } public static void zoomOut(View view) { zoomOut(view, 1.25f); } public static void zoomOut(View view, int absoluteValue) { //1.获取view宽高 int height = view.getHeight(); int width = view.getWidth(); //2.计算缩放比 float xScale = (width + DensityUtil.dip2px(StarCoreVariable.mContext, absoluteValue)) * 1.0f / width * 1.0f; float yScale = (height + DensityUtil.dip2px(StarCoreVariable.mContext, absoluteValue)) * 1.0f / height * 1.0f; if (yScale < xScale) { zoomOut(view, yScale); } else { zoomOut(view, xScale); } } }