我有一个使用自定义 LinearLayout 的小型应用程序,称为 LinearlayoutOutlined。我想在里面画出不同大小和颜色的盒子。它还有两个指示时间间隔的文本标签。在某些用户操作后必须重新绘制布局。因此我确实刷新了布局
slotPanel.setDayBoundariesInMinutes( db, dw );
TimeSlot[] tSlots = nextDaysSlots.getGaps( dayOfWeek );
slotPanel.setItems( tSlots );
slotPanel.invalidate();
其中slotpanel是LinearLayoutOutlined实例。
我检测到 onDraw 方法被不断调用。它不会在特定数量的周期后停止调用。
这是整个 LinearLayoutOutlined 类:
包 com.widgets;
public class LinearLayoutOutlined extends LinearLayout {
private int workingTimeBeginsInMinutes;
private int workingTimeFinishesInMinutes;
private TimeSlot[] items;
private Rect outline;
private Paint strokePaint = new Paint();
SimpleDateFormat formatter;
public LinearLayoutOutlined( Context context ) {
super( context );
setWillNotDraw( false );
}
public LinearLayoutOutlined( Context context, AttributeSet attrs ) {
super( context, attrs );
setWillNotDraw( false );
}
public void setWorkingTimeBeginsInMinutes( int m ) {
this.workingTimeBeginsInMinutes = m;
}
public void setWorkingTimeFinishesInMinutes( int m ) {
this.workingTimeFinishesInMinutes = m;
}
public void setDayWidthInMinutes( int dayWidthInMinutes ) {
this.workingTimeFinishesInMinutes = this.workingTimeBeginsInMinutes + dayWidthInMinutes;
}
public void setDayBoundariesInMinutes( int daybeginInMinutes, int dayWidthInMinutes ) {
setWorkingTimeBeginsInMinutes( daybeginInMinutes );
setDayWidthInMinutes( dayWidthInMinutes );
}
private int getWorkingTimeBeginsInMinutes() {
return workingTimeBeginsInMinutes;
}
public int getWorkingTimeFinishesInMinutes() {
return workingTimeFinishesInMinutes;
}
public void setItems( TimeSlot[] items ) {
this.items = items;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw( Canvas canvas ) {
if( items == null ) return;
// time labels
TextView tvFrom = ( TextView )findViewById( R.id.lblStartingTime ), tvTo = ( TextView )findViewById( R.id.lblFinishingTime );
formatter = new SimpleDateFormat( "HH:mm", Locale.getDefault() );
tvFrom.setText( formatter.format( new Date( getWorkingTimeBeginsInMinutes() * 60000L ) ) );
tvTo.setText( formatter.format( new Date( getWorkingTimeFinishesInMinutes() * 60000L ) ) );
strokePaint.setStyle( Paint.Style.STROKE );
strokePaint.setStrokeWidth( 1 );
strokePaint.setStyle( Style.FILL );
outline = canvas.getClipBounds();
Convert.setDayBoundaries( getWorkingTimeBeginsInMinutes(), getWorkingTimeFinishesInMinutes(), outline );
for( TimeSlot slotItem: items ) {
RectTouchable rect = Convert.ToRect( slotItem );
if( slotItem.isFree() )
strokePaint.setARGB( 255, 0, 255, 0 );
else
strokePaint.setARGB( 255, 255, 0, 0 );
canvas.drawRect( rect.getRect(), strokePaint );
}
}
}
请给我一些想法,我该怎么办?提前感谢
我不明白为什么我不能对自己的问题添加评论?!?
@Romain Guy: 谢谢!好的,我明白了。但我必须将 setText() 与绘图框一起设置。文本标签被覆盖,它们是最上面的。我该怎么办?