pjv
pjv

Reputation: 10708

android UI: ImageButton with animation

I'm looking to make an ImageButton that contains an animation drawable, more precisely the repetitive tweened animation of a progressbar spinner (like the view/widget that exists for this).

In xml I specified this for the ImageButton in the activity's layout:

<ImageButton android:id="@+id/i_back_cover" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:scaleType="centerInside" 
    android:gravity="center" android:layout_weight="1" android:adjustViewBounds="true" 
    android:src="@anim/progress_large"> 
</ImageButton>

The "progress_large" animation is in res/anim/:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <rotate
        android:drawable="@drawable/spinner_black_76"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="350"
        android:duration="1200" />
</set>

I set toDegrees to 350 for now, to make sure it would rotate. spinner_black_76 is just an image.

When I open the activity having this layout, the app crashes before displaying.

Weird, there's no actual stacktrace in the log, just a killed VM if I'm right (I hardly can believe this myself):

10-07 21:16:18.467: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.EDIT dat=content://net.lp.collectionista.products/items/book/1 cmp=net.lp.collectionista/.ui.activities.items.book.BookItemEditWindow }
10-07 21:16:18.687: DEBUG/AndroidRuntime(636): Shutting down VM
10-07 21:16:18.687: WARN/dalvikvm(636): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-07 21:16:18.687: DEBUG/FlurryAgent(636): Ending session
10-07 21:16:18.977: DEBUG/dalvikvm(636): GC_FOR_MALLOC freed 3876 objects / 597200 bytes in 98ms
10-07 21:16:19.007: WARN/ActivityManager(59):   Force finishing activity net.lp.collectionista/.ui.activities.items.book.BookItemEditWindow
10-07 21:16:19.016: WARN/ActivityManager(59):   Force finishing activity net.lp.collectionista/.ui.activities.collections.book.BookCollectionViewWindow
10-07 21:16:19.507: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{43fec528 net.lp.collectionista/.ui.activities.items.book.BookItemEditWindow}
10-07 21:16:21.437: INFO/Process(636): Sending signal. PID: 636 SIG: 9
10-07 21:16:21.487: INFO/ActivityManager(59): Process net.lp.collectionista (pid 636) has died.
10-07 21:16:21.517: INFO/WindowManager(59): WIN DEATH: Window{4400f330 net.lp.collectionista/net.lp.collectionista.ui.activities.CollectionsListWindow paused=false}
10-07 21:16:21.517: INFO/WindowManager(59): WIN DEATH: Window{43fc89d0 net.lp.collectionista/net.lp.collectionista.ui.activities.collections.book.BookCollectionViewWindow paused=true}
10-07 21:16:21.667: INFO/UsageStats(59): Unexpected resume of com.android.launcher while already resumed in net.lp.collectionista
10-07 21:16:21.727: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 636 uid 10034
10-07 21:16:27.237: DEBUG/dalvikvm(278): GC_EXPLICIT freed 32 objects / 1616 bytes in 81ms
10-07 21:18:14.047: DEBUG/AndroidRuntime(654): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<

What am I doing wrong in this case? Or what would be a better way to get a progressbar round spinner on top of a button?

Upvotes: 1

Views: 8239

Answers (2)

gkee
gkee

Reputation: 771

OK, so this question has an accepted answer and was 18 months ago, but just in case anyone else finds this, this seems to work for me:

In the xml layout:

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:gravity="center">

            <ImageButton
                android:id="@+id/property_button_gps"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:paddingTop="@dimen/standardPadding"
                android:paddingBottom="@dimen/standardPadding"
                android:background="@drawable/gps_blank"
                android:src="@drawable/gps_icon">
            </ImageButton>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center">

                <ProgressBar
                    android:id="@+id/property_gps_spinner"
                    style="@android:style/Widget.ProgressBar.Small"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="@dimen/standardPadding"
                    android:paddingBottom="@dimen/standardPadding"
                    android:background="@null"
                    android:visibility="gone" />
            </LinearLayout>
        </FrameLayout>

and this in the java code:

/**
 * This method will hide the GPS button image and replace it with a loading spinner
 */
private void showGpsLoading() {
    gpsSpinner.setVisibility(View.VISIBLE);
    gpsButton.setImageDrawable(null);
}

/**
 * This method will hide the GPS loading spinner and replace it with the GPS button image.
 */
public void hideGpsLoading() {
    gpsSpinner.setVisibility(View.GONE);
    gpsButton.setImageResource(R.drawable.gps_icon);
}

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1007296

:: smacks forehead ::

I should have realized this before starting in on the comments.

I'm looking to make an ImageButton that contains an animation drawable, more precisely the repetitive tweened animation of a progressbar spinner (like the view/widget that exists for this).

That isn't an AnimationDrawable. An AnimationDrawable is a frame animation, not a tweened one. As the documentation for your animation XML indicates:

compiled resource datatype: Resource pointer to an Animation.

whereas for a frame animation:

compiled resource datatype: Resource pointer to an AnimationDrawable.

Hence, I don't think you can do what you're trying to do the way you're trying to do it. You could use a frame animation and use a handful of hand-rotated versions of your graphic.

Upvotes: 3

Related Questions