我已扩展 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);
像素独立的东西。
谢谢