i made a custom view which should animate a dot on a background according to some external data. (works like a charm with textviews)
view is then added in xml and android:background is set.
view gets rendered properly but does not update. some debugging revealed that onDraw only gets called once. what is missing?
code for my custom view:
public class Gmeter extends ImageView {
private Bitmap dot;
private float dotHeight, dotWidth;
public Gmeter(Context context, AttributeSet attrs) {
super(context, attrs);
dot = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.g_dot);
dotWidth = dot.getWidth();
dotHeight = dot.getHeight();
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
float dataX =(float) Data.getX();
float x = (getWidth()-dotWidth)/2f;
float y = (getHeight()-dotHeight)/2f;
x+= dataX * getWidth() /2f;
canvas.drawBitmap(dot, x, y, null);
}
}
Whatever is setting the
xinDataneeds to callinvalidate()to tell theViewthat it needs to redraw based on new data. TheViewcan’t read your mind to know when new data is available :).And definitely tie it to the data updating… if you call
invalidate()from within youronDraw()you will have a great recipe for wasting CPU cycles.