Android TV开发 视频窗口选中放大效果的实现

先上效果图

视频模块的布局是这样的:

<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);
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值