Child pages
  • onDraw() folyamatosan triggerelődik
Skip to end of metadata
Go to start of metadata

Üdv,

van 1 kis programom, amiben saját LinearLayout-ot használnék. Ebben kis dobozokat rajzolok zöld, vagy piros színnel. A probléma az, hogy az onDraw() metódus folyamatosan hívódik. A kérdés az, hogy miért és mi lehet a megoldás?

a hívó kód:

slotPanel = ( LinearLayoutOutlined )findViewById( R.id.llySlotPanel );
slotPanel.setDayBoundariesInMinutes( db, dw );
TimeSlot[] tSlots = nextDaysSlots.getGaps( dayOfWeek );
slotPanel.setItems( tSlots );
slotPanel.invalidate();

a LinearLayoutOutlined osztály:

package 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 );
}
}
}

Segítségeteket előre is köszönöm.

 

      
      
Page viewed times
#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))
  • No labels

4 Comments

  1. Mit jelent az "állandóan meghívódik"? Rendszeresen, vagy valóban folyamatosan? Hogy kellene működnie? Nem lehet, hogy ez az üzemszerű? (smile)

    1. Anonymous

      a tervem szerint a 

      slotPanel.invalidate();

      híváskor lefut az onDraw(). egyszer. Ehhez képest folyamatosan hívódik, nincs annyi lépés, ami után leállna. Valami page lifecycle segítene, vagy az, hogy mi miatt triggerelődik meg az onDraw().

      1. Nincs sajnos ötletem... esetleg írass ki egy stacktrace-t, hogy honnan hívódik...

        1. a TextView.setText() triggereli végtelenbe az onDraw()-ot, ezt kerülni kell, canvas.drawtext()-tel oldottam meg