2013-08-06 106 views
1

我的應用程序中有一個Widget,它具有用於顯示畫布的ImageView。Android Widget Canvas - java.lang.IllegalArgumentException

如果我看着我的用戶Crashs我看到這個錯誤非常頻繁(但始終只是少數設備):

java.lang.RuntimeException: Unable to start receiver my.package.name.WidgetProvider: java.lang.IllegalArgumentException: RemoteViews for widget update exceeds maximum bitmap memory usage (used: 5715000, max: 5529600 /*these arent always the same :)*/) The total memory cannot exceed that required to fill the device's screen once. 

我的畫布具有動態的大小,當用戶調整其大小自動調整大小,和已指定的高寬比,則創建這樣的:

Resources r = context.getResources(); 
    SharedPreferences widgets = context.getSharedPreferences("widgets", 0); 

      //these are stored in onAppWidgetOptionsChanged when user resizes his widget 
    int w = widgets.getInt(widgetId + "_width", 130); 
    int h = widgets.getInt(widgetId + "_height", 160); 

    w = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, w, 
      r.getDisplayMetrics()); 
    h = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, h, 
      r.getDisplayMetrics()); 

    float scaleX = (float) w/(float) 13; 
    float scaleY = (float) h/(float) 16; 

    float scale = 1.5f * (scaleX < scaleY ? scaleX : scaleY);//i multiplied it with 1.5 because the canvas was very unsharp 

    w = (int) (13 * scale); 
    h = (int) (16 * scale); 

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
      R.layout.widget); 


    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444); 
    Canvas canvas = new Canvas(bmp); 


[...] 

    remoteViews.setImageViewBitmap(R.id.widget, bmp); 

    appWidgetManager.updateAppWidget(widgetId, remoteViews); 

我估計是因爲畫布是1.5倍比插件較大,但否則它是非常不清晰的。

我該怎麼辦?

在此先感謝。

倚天芥蘭

回答

1

documentation規定:

使用的RemoteViews對象不能超過 總位圖存儲器,需要以填充屏幕的1.5倍,即。 (屏幕寬度x屏幕 高度x 4 x 1.5)字節。

因此,使用它來控制您的位圖大小,然後在您的佈局中使用ImageView scaleType fitCenter來縮放以適合小部件中的位圖。