在高端设备上绘制画布问题 [英] Drawing on Canvas issue in high end devices

查看:92
本文介绍了在高端设备上绘制画布问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式在画布上绘制 SeekBar 。我写了代码根据设备密度设置 SeekBar 的LayoutParams。我使用的开关案例与设备密度像

I want to draw a SeekBar on canvas programmatically. I wrote code to set the LayoutParams of the SeekBar based on device density. I am using switch case with device density like

final DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
if(metrics.densityDpi <= DisplayMetrics.DENSITY_LOW){
        zoomBarParams = new LinearLayout.LayoutParams(18,
                LayoutParams.FILL_PARENT);
        } else if (metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM){
            zoomBarParams = new LinearLayout.LayoutParams(24,
                    LayoutParams.FILL_PARENT);

        }else if (metrics.densityDpi <= DisplayMetrics.DENSITY_HIGH){
            zoomBarParams = new LinearLayout.LayoutParams(24,
                    LayoutParams.FILL_PARENT);

        }else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XHIGH){
            zoomBarParams = new LinearLayout.LayoutParams(31,
                    LayoutParams.FILL_PARENT);


        }else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XXHIGH){
            zoomBarParams = new LinearLayout.LayoutParams(60,
                    LayoutParams.FILL_PARENT);

        }else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XXXHIGH){
            zoomBarParams = new LinearLayout.LayoutParams(60,
                    LayoutParams.FILL_PARENT);

        } else {
            zoomBarParams = new LinearLayout.LayoutParams(60,
                    LayoutParams.FILL_PARENT);

        }

但这不适用于高端设备注5,Galaxy S6 Edge等。我相信这些设备的密度范围是XXXHIGH,那么为什么这个不工作?在绘制画布时,设备密度和屏幕尺寸之间是否有任何关系?任何帮助将非常感激。

But this isn't working in high end devices like Samsung Note 5, Galaxy S6 Edge etc..I believe that these devices comes in the density range XXXHIGH, then why is this not working?? Is there any relation between device density and screen size while drawing on canvas? Any help will be greatly appreciated.

推荐答案

为什么你不试图摆脱所有 if else 案例并使某些泛型?由于您需要根据屏幕大小放置不同的像素值,因此您可以改用 dp

Why don't you try to get rid of all the if else cases and make something generic? Since you need to put different pixel values depending on the screen size, you can use dp instead.

取决于设备的屏幕密度,并使用 LayoutParams c> dp / code>。

You can get px from dp depending on the screen density of the device and use that in LayoutParams.

public static float convertDpToPixel(float dp, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); // You can cache "((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)" to avoid re-calculation.
    return px;
}

因此,您将创建 param 这样:

zoomBarParams = new LinearLayout.LayoutParams(convertDpToPixel(DP_VALUE, context),
                LayoutParams.FILL_PARENT);

其中 DP_VALUE

我希望这可能解决您的问题。

I hope this might solve your problem.

这篇关于在高端设备上绘制画布问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆