edit: Added a bounty. Hopefully this will draw some attention to the issue at hand.
I am currently trying to solve a problem in which an animation is continuously run on the main screen of an application animating a baby sleeping. this is done by having an imageView and replacing the current image in the animation with the next in line. Seing as there are more than 500 images this becomes very CPU intensive as each time an image is replaced the entire View Hierachy is re-calculated. Is there any way to NOT recalculate the entire view hierachy during this animation?
The run method looks like this :
@Override
public void run() {
if (bitmap != null) {
bitmap.recycle();
}
final String name = String.format(Locale.ENGLISH, pattern, current);
final int id = getResources().getIdentifier(name, "drawable", getPackageName());
if (id != 0) {
bitmap = BitmapFactory.decodeResource(getResources(), id);
view.setImageBitmap(bitmap);
}
current++;
if (runAgain()) {
frameCounter++;
long nextAt = firstFrameAt + (frameCounter * interval);
long delay = nextAt - SystemClock.elapsedRealtime() - 1;
if (delay > 0) {
handler.postDelayed(this, delay);
} else {
this.run();
}
}
}
edit: I’ve been thinking about creating my own custom ImageView and overriding whatever method causes the problem. Would this be a viable solution?
I solved the problem by implementing my own custom ImageView overriding the following two methods: