AndroidVideoView的scaleX,scaleY不起作用
创始人
2024-08-22 00:30:08
0

可以通过编写自定义View,覆盖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 defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mVideoWidth == 0 || mVideoHeight == 0) {
            setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
        } else {
            int width = getMeasuredWidth();
            int height = getMeasuredHeight();
            float scaleX = (float) mVideoWidth / (float) width;
            float scaleY = (float) mVideoHeight / (float) height;
            float scale = Math.max(scaleX, scaleY);
            width = (int) ((float) mVideoWidth / scale);
            height = (int) ((float) mVideoHeight / scale);
            setMeasuredDimension(width, height);
        }
    }

    public void setVideoSize(int videoWidth, int videoHeight) {
        mVideoWidth = videoWidth;
        mVideoHeight = videoHeight;
        requestLayout();
    }
}

您可以将此自定义视图放在XML中,并像VideoView一样使用它。要指定视频的大小,请使用setVideoSize()方法:

mScalableVideoView.setVideoSize(videoWidth, videoHeight);

相关内容

热门资讯

七分钟了解!多乐小程序辅助器免... 七分钟了解!多乐小程序辅助器免费!一贯一直总是有辅助神器(有挂教程)-哔哩哔哩1、该软件可以轻松地帮...
七分钟了解!新二号辅助软件价格... 七分钟了解!新二号辅助软件价格!竟然是真的有辅助技巧(有挂神器)-哔哩哔哩新二号辅助软件价格破解侠是...
2分钟了解!欢乐茶馆辅助!好像... 2分钟了解!欢乐茶馆辅助!好像有辅助攻略(有挂技巧)-哔哩哔哩1、欢乐茶馆辅助免费脚本咨询教程、欢乐...
7分钟了解!广东雀神智能插件辅... 7分钟了解!广东雀神智能插件辅助脚本!其实有辅助神器(果真有挂)-哔哩哔哩该软件可以轻松地帮助玩家将...
5分钟了解!微信老铁13水辅助... 5分钟了解!微信老铁13水辅助!原来是有辅助软件(有挂分析)-哔哩哔哩1、完成微信老铁13水辅助辅助...
第1分钟了解!熊猫辅助官网下载... 第1分钟了解!熊猫辅助官网下载安装!一直是真的有辅助工具(有挂技巧)-哔哩哔哩1、玩家可以在熊猫辅助...
第六分钟了解!星悦软件辅助!总... 第六分钟了解!星悦软件辅助!总是是有辅助神器(有挂助手)-哔哩哔哩1、星悦软件辅助免费辅助多个强度级...
三分钟了解!功夫川麻小程序有挂... 三分钟了解!功夫川麻小程序有挂吗!确实真的是有辅助教程(有挂技术)-哔哩哔哩1、功夫川麻小程序有挂吗...
第二分钟了解!浙江游戏大厅透视... 第二分钟了解!浙江游戏大厅透视辅助器!总是是真的有辅助方法(讲解有挂)-哔哩哔哩浙江游戏大厅透视辅助...
第五分钟了解!皮皮四川游戏挂!... 第五分钟了解!皮皮四川游戏挂!其实是有辅助脚本(有挂工具)-哔哩哔哩1、超多福利:超高返利,海量正版...