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 x in Data needs to call invalidate() to tell the View that it needs to redraw based on new data. The View can'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 your onDraw() you will have a great recipe for wasting CPU cycles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With