调整布局大小的问题

时间:2017-06-01 10:04:33

标签: android resize

好吧,我尝试使用自定义方法调整Layouts大小,但我在不同设备上遇到问题。适用于我的手机和平板电脑,但当我尝试在我的朋友的手机上打开应用时,会发生这种情况:

Image Problem

我的设备: 我只能添加一张照片。 Image mobile

重新缩放的方法:

public final static int DEV_WIDTH = 480;
public final static int DEV_HEIGHT = 800;

public static int toDensityPx(Activity context, int targetPx){

    DisplayMetrics displayMetrics = new DisplayMetrics();
    Display display = context.getWindowManager().getDefaultDisplay();
    display.getMetrics(displayMetrics);
    Point resolution = new Point();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        display.getRealSize(resolution);
    } else display.getSize(resolution);

    int deviceWidth = resolution.x;

    Log.e("DENSITY", String.valueOf(displayMetrics.densityDpi));
    Log.e("DENSITY XXHIGH", String.valueOf(DisplayMetrics.DENSITY_XXHIGH));
    Log.e("WIDTH DEVICE", String.valueOf(deviceWidth));

    int scale = scale(deviceWidth, targetPx);

    Log.e("RESULT", String.valueOf(scale));
    Log.e("TOTAL", String.valueOf((scale*targetPx)));

    int result = 0;

    switch(displayMetrics.densityDpi){

        case DisplayMetrics.DENSITY_LOW: result = (int)(scale/0.75f);
            break;
        case DisplayMetrics.DENSITY_MEDIUM: result = targetPx;
            break;
        case DisplayMetrics.DENSITY_HIGH: result = (int)(scale/1.5f);
            break;
        case DisplayMetrics.DENSITY_XHIGH: result = (int)(scale/2f);
            break;
        case DisplayMetrics.DENSITY_XXHIGH: result = (int)(scale/3f);
            break;
        case DisplayMetrics.DENSITY_XXXHIGH: result = (int)(scale/4f);
            break;
    }
    return result;
}


public static int scale(int deviceWidth, int targetPx){

    double scale = 0;

    if(deviceWidth < DEV_WIDTH) scale = (double)DEV_WIDTH/deviceWidth;

    else if(deviceWidth == DEV_WIDTH) scale = 1;

    else scale = (double)deviceWidth/DEV_WIDTH;


    return (int)((scale*targetPx));
}

我尝试添加比例的代码:

PercentRelativeLayout Percent = (PercentRelativeLayout) rootView.findViewById(R.id.percent1);
    ViewGroup.LayoutParams params = Percent.getLayoutParams();
    params.height = Scaler.toDensityPx(getActivity(), 380);
    params.width = Scaler.toDensityPx(getActivity(), 380);
    Percent.setLayoutParams(params);

    FrameLayout Frame = (FrameLayout) rootView.findViewById(R.id.imageprofile);
    ViewGroup.LayoutParams paramsfr = Frame.getLayoutParams();
    paramsfr.height = Scaler.toDensityPx(getActivity(), 190);
    paramsfr.width = Scaler.toDensityPx(getActivity(), 190);
    Frame.setLayoutParams(paramsfr);

该方法看起来不错,因为正在调整应用程序上的所有文本并且一切正常,但在我尝试调整布局时不一样。

0 个答案:

没有答案