【问题标题】:android UI: ImageButton with animationandroid UI:带动画的ImageButton
【发布时间】:2010-10-07 19:37:22
【问题描述】:

我希望制作一个包含动画可绘制的 ImageButton,更准确地说是进度条微调器的重复补间动画(例如为此而存在的视图/小部件)。

在 xml 中,我为活动布局中的 ImageButton 指定了这个:

<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>

“progress_large”动画在 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>

我现在将toDegrees 设置为 350,以确保它会旋转。 spinner_black_76 只是一张图片。

当我打开具有此布局的 Activity 时,应用在显示前崩溃。

奇怪,日志中没有实际的堆栈跟踪,如果我是对的,只是一个被杀死的虚拟机(我自己几乎不敢相信):

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 <<<<<<<<<<<<<<

在这种情况下我做错了什么?或者有什么更好的方法可以让按钮顶部的进度条圆形微调器?

【问题讨论】:

  • 崩溃后的logcat输出是什么?
  • @pvj:尝试将ImageButton 设置为@drawable/spinner_black_76,以确保这与动画有某种联系。
  • @CommonsWare:马克,这就是我现在使用的。它只是显示静态环并且工作正常。

标签: android user-interface animation spinner imagebutton


【解决方案1】:

好的,所以这个问题有一个可接受的答案,并且是 18 个月前的问题,但以防万一其他人发现这个问题,这似乎对我有用:

在xml布局中:

        <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>

这在java代码中:

/**
 * 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);
}

【讨论】:

  • 有点贵,有两种布局。 LinearLayout 也不是多余的吗?
【解决方案2】:

:: 敲打额头 ::

我应该在开始参加 cmets 之前就意识到这一点。

我希望制作一个包含动画可绘制的 ImageButton,更准确地说是进度条微调器的重复补间动画(例如为此而存在的视图/小部件)。

这不是AnimationDrawableAnimationDrawable 是帧动画,而不是补间动画。正如您的动画 XML 的 documentation 所示:

编译的资源数据类型:指向动画的资源指针。

而对于frame animation

已编译的资源数据类型:指向 AnimationDrawable 的资源指针。

因此,我不认为你可以按照你想做的方式去做你想做的事。您可以使用帧动画并使用少量手动旋转的图形版本。

【讨论】:

  • 你是对的。补间动画不是 Drawable,因此我不应该将它放在 android:src 标签中。它在 res/anim 中的事实也应该放弃它。我的错。有趣的是,如果您将它与轻松的原生 ProgressBar xml 代码进行比较,那么自己制作一个旋转的进度条并不是一件容易的事。自己做的最简单的方法可能是用 12 个项目制作帧动画,这些项目是静态在 xml 中旋转的可绘制对象。谢谢
  • 实际上,ImageButton 似乎希望保存一个 BitmapDrawable(当它没有从 xml 中获取它时,它不会抱怨)。所以我上面描述的帧动画AnimationDrawable的想法也行不通。我得试试.gif。
  • @pjv:保证 GIF 在 2.1 及更低版本上无法使用。可能在 2.2 上工作——我知道添加了一些动画 GIF 支持,但我不确定具体在哪里。您还可以尝试将ImageView 浮动在无标题的Button 上,其中ImageView 将点击事件传递给Button
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多