下面是自定义标签字段,它将位图绘制为背景。我重写getprefferedwidth和getpreferredheight,这不会设置我的字段的高度和宽度吗?目前宽度和高度未正确设置
由于
public class TimerLabelField extends LabelField {
private int colour;
private Bitmap backgroundBitmap;
public TimerLabelField(Object text, long style, Font font, int colour,
Bitmap backgroundBitmap) {
super(text, style);
this.colour = colour;
this.backgroundBitmap = backgroundBitmap;
}
protected void paint(Graphics graphics) {
if(backgroundBitmap != null){
graphics.drawBitmap(0, 0, this.backgroundBitmap.getWidth(), this.backgroundBitmap.getHeight(), this.backgroundBitmap, 0, 0);
}
graphics.setColor(this.colour);
super.paint(graphics);
}
public int getPreferredWidth()
{
return this.backgroundBitmap.getWidth();
}
public int getPreferredHeight()
{
return this.backgroundBitmap.getHeight();
}
答案 0 :(得分:2)
您也应该覆盖layout(int width, int height)
:
protected void layout(int width, int height) {
super.layout(width, height);
setExtent(Math.min(width, this.BackgroundBitmap.getWidth()), Math.min(height, this.Backgroundbitmap.getHeight());
}