如何在 onDraw() 方法中定义与像素无关的高度
我扩展了 View 来构建自定义小部件。我想用独立的像素单位定义小部件的高度。 我认为可以通过将像素密度乘以所需的高度来完成,但我不知道该怎么做。
到目前为止我所拥有的(最小化):
public class Timeline extends View
{
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
int canvasWidth = canvas.getWidth();
this.widgetWith = canvasWidth;
this.setBackgroundGradientNoVideo();
RectF rect = new RectF();
rect.set(0, 0, canvasWidth, 50);
//Dessin d'un rectangle de fond
canvas.drawRoundRect(rect, 10, 10, this.paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
//super.onMeasure(widthMeasureSpec, 50);
this.setMeasuredDimension(widthMeasureSpec, 50);
this.widgetWith = MeasureSpec.getSize(widthMeasureSpec);
}
}
所以我想改变线条
this.setMeasuredDimension(widthMeasureSpec, 50);
和
rect.set(0, 0, canvasWidth, 50);
像素独立的东西。
谢谢
I have extended View to build a custom widget. I would like to define the height of the widget with an independ pixel unit.
I think it could be done by multiplying the pixel density by the desired height , but i don't know how to do that.
What i have so far (minimized) :
public class Timeline extends View
{
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
int canvasWidth = canvas.getWidth();
this.widgetWith = canvasWidth;
this.setBackgroundGradientNoVideo();
RectF rect = new RectF();
rect.set(0, 0, canvasWidth, 50);
//Dessin d'un rectangle de fond
canvas.drawRoundRect(rect, 10, 10, this.paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
//super.onMeasure(widthMeasureSpec, 50);
this.setMeasuredDimension(widthMeasureSpec, 50);
this.widgetWith = MeasureSpec.getSize(widthMeasureSpec);
}
}
So i would like to change the lines
this.setMeasuredDimension(widthMeasureSpec, 50);
and
rect.set(0, 0, canvasWidth, 50);
To something pixel independent.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将像素值乘以
getResources().getDisplayMetrics().密度
以使像素与密度无关。如果您绘制文本,您可能需要使用scaledDensity。我相信 mdpi 上的密度 = 1,hdpi 上的密度 = 1.5,ldpi 上的密度 = 0.75。
要完成答案,需要一些源代码:
在处理视图或图像时使用以下内容
,或者在处理文本时使用以下内容:
multiply your pixel values by
getResources().getDisplayMetrics().density
to make pixels density-independent. If you draw text, you might want to use scaledDensity instead.density = 1 on mdpi, 1.5 on hdpi, and 0.75 on ldpi i believe.
To complete the answer, some source code:
Either use the following when dealing with views or images
Or, when dealing with text:
要从 dp 值获取 px 值,请计算
我建议在某处放置一个静态方法,该方法将 dp 值作为参数,以便用更少的代码来完成此操作。例如,我创建了一个类“ResolutionHelper”,它在应用程序启动时使用正确的密度值进行初始化,并且具有一个静态方法“dipToPx(int dial)”,然后可以从任何地方使用它。
To get px values from dp values, calculate
I recommend to place a static method somewhere which takes the dp value as parameter in order to do this with less code. E.g. I created a class "ResolutionHelper" which is initialized with the correct density value on app start and which has a static method "dipToPx(int dip) that can then used from anywhere.