欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android从源码层100%屏幕适配

程序员文章站 2022-05-25 11:54:20
...

1.首先写一个获取比例的工具类


public class AutoUtils {
    private  Context context;
    private  static  AutoUtils instance;
    //标准值
    public static final float STANDARD_WIDTH=1080f;
    public static final float STANDARD_HEIGHT=1920f;
    //实际设备信息
    public static float displayMetriceWidth;
    public static float displayMetriceHight;

    public static AutoUtils getInstance(Context context){
        if (instance==null){
            instance=new AutoUtils(context);
        }
        return instance;
    }
    private AutoUtils(Context context){
        this.context=context;
        WindowManager windowManager= (WindowManager) context.getSystemService(
                Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics=new DisplayMetrics();
        if (displayMetriceWidth==0.0f||displayMetriceHight==0.0f){
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            int systemBarHeight = getStatusBarHeight(context);
            if(displayMetrics.widthPixels>displayMetrics.heightPixels){
                displayMetriceWidth=(float) (displayMetrics.heightPixels);
                displayMetriceHight=(float) (displayMetrics.widthPixels-systemBarHeight);
            }else {
                displayMetriceWidth=(float) (displayMetrics.widthPixels);
                displayMetriceHight=(float) (displayMetrics.heightPixels-systemBarHeight);
            }
        }
    }
    public float getHorizontalScaleValue(){
        return ((float)displayMetriceWidth)/STANDARD_WIDTH;
    }
    public float getVertivaScaleValue(){
        return ((float)(displayMetriceHight)/STANDARD_HEIGHT);
    }
    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = resources.getDimensionPixelSize(resourceId);
        return statusBarHeight;
    }
}


2.自定义布局,按照比例来计算组件的宽高


public class AutoRelativeLayout extends RelativeLayout {
    private boolean flag = true;
    public AutoRelativeLayout(Context context) {
        super(context);
    }
    public AutoRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public AutoRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed,l,t,r,b);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //保证值测量一次,查看源码会发现父类也会调用child.onMeasure()方法
       if(flag){
           float scaleX = AutoUtils.getInstance(getContext()).getHorizontalScaleValue();
           float scaleY = AutoUtils.getInstance(getContext()).getVertivaScaleValue();

           int childCount = this.getChildCount();

           for (int i = 0; i < childCount ; i++) {
               View child = getChildAt(i);
               LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
               layoutParams.width = (int)(layoutParams.width * scaleX);
               layoutParams.height= (int)(layoutParams.height * scaleY);
               layoutParams. leftMargin = (int) (layoutParams. leftMargin * scaleX);
               layoutParams. rightMargin = (int) (layoutParams. rightMargin * scaleX);
               layoutParams. topMargin = (int) (layoutParams. topMargin * scaleY) ;
               layoutParams. bottomMargin = (int) (layoutParams. bottomMargin * scaleY);
           }
       }
       flag=false;
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

相关标签: 应用