我已经扩展了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);
转化为像素独立的单位。
谢谢。