我写了一个自定义视图,我自己画画。我需要这个,因为我必须绘制带有边框的圆圈,其中包含边界和内部的一些东西。
目前我使用5px作为圆圈边框的宽度。这在ldpi屏幕上看起来不错,在mdpi屏幕上看起来不错,我觉得它在hdpi屏幕上看起来很糟糕。
如何编写视图以缩放不同屏幕的边框?目前,自定义视图获取一个以像素为单位设置宽度的属性。
如何在绘图过程中使用其他尺寸?
答案 0 :(得分:2)
在dp中思考而不是像素,并使用以下代码获取与dp值对应的像素:
/**
* Convert a dimension in dip to px
* @param dip
* @param context
* @return px
*/
public static int dipToPx(int dip, Context context) {
return (int) (dip * context.getResources().getDisplayMetrics().density);
}
或者,如果您在xml资源文件中定义了一个维度,如下所示:
<resources>
<dimen name="common_padding_large">20dp</dimen>
</resources>
您可以使用
获取此维度的像素值getContext().getResources().getDimension(R.dimen.common_padding_large);
答案 1 :(得分:1)
您应该考虑使用dp或与密度无关的像素。 This page详细介绍了如何支持不同的屏幕。