2011-02-27 68 views
0

如何在widget中使用imageswitcher?Android小部件imageswitcher?

我的代碼編譯罰款,但是當我嘗試創建我的小部件也給出了一個錯誤:

Error inflating AppWidget AppWidgetProviderInfo(provider=ComponentInfo{test.basic/test.basic.HelloWorldWidget}): android.view.InflateException: Binary XML file line #5: Error inflating class android.widget.ImageSwitcher

我爲我的imageswitcher XML代碼:

<ImageSwitcher android:id="@+id/image_switcher" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

和我創建的代碼一個是的ViewFactory:

@Override 
public void onEnabled(final Context context) { 
    readImages(context); 

    ImageSwitcher imageSwitcher = getImageSwitcher(context); 
    imageSwitcher.setFactory(new ViewFactory() { 

     public View makeView() { 
      ImageView imageView = new ImageView(context); 
      imageView.setBackgroundColor(0xFF000000); 
      imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
      return imageView; 
     } 
    }); 
} 

任何幫助,將大幅升值。

謝謝

回答

1

我不認爲這是可能在AppWidget使用的ImageSwitcher。 AppWidget文檔(http://developer.android.com/guide/topics/appwidgets/index.html)列出了構建AppWidget時可能使用的七種控件類型。 ImageSwicher不在列表中。

您最好的辦法是使用FrameLayout或RelativeLayout與多個ImageView堆疊在一起,然後逐個設置其可見性,直到只顯示當前所需的圖像。

+0

但隨後問題變成如何使用圖像之間的動畫 – 2011-02-27 22:21:41

+0

是的,我試過一次在我的小部件中使用LayoutAnimations,結果不太好。他們在有趣的時候被觸發,例如當用戶在升​​級後第一次使用我的一個小部件導航到主屏幕時。如果你真的想要動畫,我認爲你必須使用AlarmManager(或其他類型的調度機制)來安排5次更新,間隔爲100毫秒,然後操縱圖像的Alpha來實現淡入/淡入例如,輸出效果。 – 2011-02-28 13:51:04