Having a problem with a custom button control which I have made. The button is defined in layout like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:background="#fff"
android:contentDescription="@string/my_button_info"
android:src="@drawable/button_appearance" />
</merge>
and button_appearance is defined like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pic_down"
android:state_pressed="true" />
<item android:drawable="@drawable/button_pic"
android:state_focused="true" />
<item android:drawable="@drawable/button_pic" />
</selector>
Now, when I push my button (click on the LinearLayout) I show it as a different bitmap and animate it as rotating, here is the animation:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1666"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="359" />
and I call it like this:
public void animateButton() {
myButton.setEnabled(false);
myButton.setImageDrawable(getContext().getResources().getDrawable(R.drawable.button_pic_spin));
myButton.startAnimation(AnimationUtils.loadAnimation(context, R.anim.spinner_animation));
}
then, when the action it kicks off is done I stop it:
public void stopAndEnableButton() {
myButton.setEnabled(true);
myButton.setImageDrawable(getContext().getResources().getDrawable(R.drawable.button_appearance));
ttcButton.clearAnimation();
}
I've received a few crashes from Crashlytics on the line just above where I call setImageDrawable. Crashlytics says:
Failed to allocate a 2869648 byte allocation with 1275632 free bytes and 1245KB until OOM
The weird thing is that the pngs here are all about 25K in size - and there are only 3 of them (2 referenced in button_appearance.xml and the plain one button_pic_spin) how are we getting to 2 Megabytes requirement when I try to swap in something which ought to be just 50K at most?
Is the problem that I ought to call ClearAnimation before I do setImageDrawable? Is the system actually trying to make a bunch of allocations for partially rotated bitmaps?