关于重写ViewGroup中和Activity中获取屏幕高度和宽度、手机屏幕中心坐标获取

    xiaoxiao2021-12-15  30

    转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/53519279

    ViewGroup中获取屏幕高度和宽度

    //获取屏幕高度宽度 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measureWidth = MeasureSpec.getSize(widthMeasureSpec); int measureHeigt = MeasureSpec.getSize(heightMeasureSpec); }

    Activity中获取屏幕高度和宽度

    Display display=getWindowManager().getDefaultDisplay(); int width=display.getWidth(); int height=display.getHeight();

    当然,现在上面这种获取方式已过时

    我们可以使用以下方式

    DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); width = metrics.widthPixels; height = metrics.heightPixels;

    手机屏幕中心坐标获取

    //获取屏幕高度宽度 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measureWidth = MeasureSpec.getSize(widthMeasureSpec); int measureHeigt = MeasureSpec.getSize(heightMeasureSpec); //获取屏幕中心点 centerX = measureWidth/2; centerY = measureHeigt/2; }

    转载请注明原文地址: https://ju.6miu.com/read-1000011.html

    最新回复(0)