获取我的主屏幕小部件的大小

时间:2014-08-06 06:29:06

标签: android android-widget homescreen

我只是想知道我当前的小部件有多大。我找到了很多问题来设置最小尺寸,但我不想设置它。相反,我想展示那些只适合小部件的信息。

如果小部件太小我需要隐藏一些东西,但我需要知道它的大小。我阅读了一些类的源代码,比如AppWidgetProvider,甚至是文档。总是只有关于最小或最大尺寸的参考,但从来没有当前尺寸。

请指出正确的资源。

6 个答案:

答案 0 :(得分:10)

不幸的是,仍然没有api从AppWidget-Host获取此信息。

您只能获得有关调整大小事件(Android 4.1+)的当前大小信息,并且仅获得支持此功能的启动器(例如Sony XPeria Launcher不支持,以及某些第三方启动器)。

我遵循了这个stackoverflow线程和Android开发者文档:

Determine the homescreen's appwidgets space grid size

https://developer.android.com/reference/android/appwidget/AppWidgetProvider.html#onAppWidgetOptionsChanged%28android.content.Context,%20android.appwidget.AppWidgetManager,%20int,%20android.os.Bundle%29

以下是我的代码,用于确定小部件大小,取自图片日历2014(Play商店):

        /* Get Device and Widget orientation. 
           This is done by adding a boolean value to 
           a port resource directory like values-port/bools.xml */

        mIsPortraitOrientation = getResources().getBoolean(R.bool.isPort);

        // Get min dimensions from provider info
        AppWidgetProviderInfo providerInfo = AppWidgetManager.getInstance(
            getApplicationContext()).getAppWidgetInfo(appWidgetId);

        // Since min and max is usually the same, just take min
        mWidgetLandWidth = providerInfo.minWidth;
        mWidgetPortHeight = providerInfo.minHeight;
        mWidgetPortWidth = providerInfo.minWidth;
        mWidgetLandHeight = providerInfo.minHeight;

        // Get current dimensions (in DIP, scaled by DisplayMetrics) of this
        // Widget, if API Level allows to
        mAppWidgetOptions = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            mAppWidgetOptions = getAppWidgetoptions(mAppWidgetManager,
                    appWidgetId);

        if (mAppWidgetOptions != null
                && mAppWidgetOptions
                        .getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) > 0) {
            if (D.DEBUG_SERVICE)
                Log.d(TAG,
                        "appWidgetOptions not null, getting widget sizes...");
            // Reduce width by a margin of 8dp (automatically added by
            // Android, can vary with third party launchers)

            /* Actually Min and Max is a bit irritating, 
               because it depends on the homescreen orientation
               whether Min or Max should be used: */

            mWidgetPortWidth = mAppWidgetOptions
                    .getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
            mWidgetLandWidth = mAppWidgetOptions
                    .getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
            mWidgetLandHeight = mAppWidgetOptions
                    .getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
            mWidgetPortHeight = mAppWidgetOptions
                    .getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

            // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
            int category = mAppWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);

            // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen
            // widget (dumped with Android-L preview :-( ).
            mIsKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;

        } else {
            if (D.DEBUG_SERVICE)
                Log.d(TAG,
                        "No AppWidgetOptions for this widget, using minimal dimensions from provider info!");
            // For some reason I had to set this again here, may be obsolete
            mWidgetLandWidth = providerInfo.minWidth;
            mWidgetPortHeight = providerInfo.minHeight;
            mWidgetPortWidth = providerInfo.minWidth;
            mWidgetLandHeight = providerInfo.minHeight;
        }

        if (D.DEBUG_SERVICE)
            Log.d(TAG, "Dimensions of the Widget in DIP: portWidth =  "
                    + mWidgetPortWidth + ", landWidth = " + mWidgetLandWidth
                    + "; landHeight = " + mWidgetLandHeight
                    + ", portHeight = " + mWidgetPortHeight);

        // If device is in port oriantation, use port sizes
        mWidgetWidthPerOrientation = mWidgetPortWidth;
        mWidgetHeightPerOrientation = mWidgetPortHeight;

        if (!mIsPortraitOrientation)
        {
            // Not Portrait, so use landscape sizes
            mWidgetWidthPerOrientation = mWidgetLandWidth;
            mWidgetHeightPerOrientation = mWidgetLandHeight;
        }

在Android 4.1+上,您现在拥有当前设备方向的DIP中Widget的当前大小。要知道您的窗口小部件的宽度和高度有多少主屏幕网格单元,您必须将其除以单元格大小。默认值为74dip,但这可能因使用不同的设备或启动器而异。允许更改网格的自定义启动器对于Widget开发人员来说是一件痛苦的事。

在Android上< 4.1无法获取当前的小部件大小。因此,最好将您的API级别设置为JellyBean以避免麻烦。现在应该没问题......两年前,当我开始使用我的Widget应用程序时,这不是一个选项。

获取有关方向更改的通知以重绘窗口小部件是另一个主题。

重要:如果您允许用户拥有多个您的小部件,则必须根据其ID对所有人进行此计算!

答案 1 :(得分:0)

您可以将一种方法添加到小部件提供程序中,并在从API 16开始为所有版本的Android调整大小时获取小部件大小。

    @Override
    public void onAppWidgetOptionsChanged (Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle widgetInfo) {

        int width = widgetInfo.getInt (AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
        int height = widgetInfo.getInt (AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

    }

对于新添加的小部件,您可以通过从其布局中获取其大小并将其乘以其cols num来获得其特色:

    AppWidgetProviderInfo widgetInfo = AppWidgetManager.getInstance (context).getAppWidgetInfo (widgetId);

    private int getColsNum (int size) {
        return (int) Math.floor ((size - 30) / 70);
    }

    public int getWidthColsNum () {
        return getColsNum (widgetInfo.minWidth);
    }

    public int getHeightColsNum () {
        return getColsNum (widgetInfo.minHeight);
    }

    int width = widgetInfo.minWidth * getWidthColsNum ();
    int height = widgetInfo.minHeight * getHeightColsNum ();

获取小部件cols num是从官方Android guidelines中获取的。

答案 2 :(得分:0)

获取列数/行数(科特琳):

val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
val minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
val maxHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)

val oneCellHeight = ViewExtensions.convertDpToPx(40f * 1, context).toInt() - (30 * 1)
val twoCellHeight = ViewExtensions.convertDpToPx(40f * 2, context).toInt() - (30 * 2)
val threeCellHeight = ViewExtensions.convertDpToPx(40f * 3, context).toInt() - (30 * 3)
val fourCellHeight = ViewExtensions.convertDpToPx(40f * 4, context).toInt() - (30 * 4)

when {
    oneCellHeight in (minHeight..maxHeight) -> {
        // 1 cell
    }
    twoCellHeight in (minHeight..maxHeight) -> {
        // 2 cells
    }
    threeCellHeight in (minHeight..maxHeight) -> {
        // 3 cells
    }
    fourCellHeight in (minHeight..maxHeight) -> {
        // 4 cells
    }
    else -> {
        // More
    }
}

class ViewExtensions {
    companion object {
        fun convertDpToPx(sp: Float, context: Context): Float {
            return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sp, context.resources.displayMetrics)
        }
    }
}

基于,这1个单元格= 40dp

https://developer.android.com/guide/practices/ui_guidelines/widget_design.html#anatomy_determining_size

答案 3 :(得分:0)

这是获取小部件大小(以像素为单位)的类助手

请注意,您应该使用应用程序上下文,否则在小部件旋转时方向将不正确

class WidgetSizeProvider(
    private val context: Context // Do not pass Application context
) {

    private val appWidgetManager = AppWidgetManager.getInstance(context)

    fun getWidgetsSize(widgetId: Int): Pair<Int, Int> {
        val isPortrait = context.resources.configuration.orientation == ORIENTATION_PORTRAIT
        val width = getWidgetWidth(isPortrait, widgetId)
        val height = getWidgetHeight(isPortrait, widgetId)
        val widthInPx = context.dip(width)
        val heightInPx = context.dip(height)
        return widthInPx to heightInPx
    }

    private fun getWidgetWidth(isPortrait: Boolean, widgetId: Int): Int =
        if (isPortrait) {
            getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
        } else {
            getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
        }

    private fun getWidgetHeight(isPortrait: Boolean, widgetId: Int): Int =
        if (isPortrait) {
            getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
        } else {
            getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
        }

    private fun getWidgetSizeInDp(widgetId: Int, key: String): Int =
        appWidgetManager.getAppWidgetOptions(widgetId).getInt(key, 0)

    private fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()

}

答案 4 :(得分:0)

对于Android 4.1(API 16)或更高版本:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance( context );
int ids[] = appWidgetManager.getAppWidgetIds( componentName );
for( int id : ids ){
    Bundle options = appWidgetManager.getAppWidgetOptions( id );
    int width = options.getInt (AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
    int height = options.getInt (AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
}

答案 5 :(得分:0)

我们无法从OPTION_APPWIDGET_MAX_WIDTH和OPTION_APPWIDGET_MAX_HEIGHT中获取小部件尺寸,它将返回错误的值。

此解决方案基于@Zanael答案。我添加了增量值,因为在某些设备上,cellSize不能在minSize和maxSize范围内。

private fun getWidgetSize(minSize: Int, maxSize: Int, context: Context): Int {
    var delta = Int.MAX_VALUE
    var lastDeltaRecord = -1
    for (i in 1..30) {
        val cellSize = convertDpToPx(40f * i, context).toInt() - (30 * i)
        if (cellSize in minSize..maxSize) {
            return i
        }
        val deltaMin = abs(minSize - cellSize)
        val deltaMax = abs(cellSize - maxSize)
        if (delta > deltaMin) {
            delta = deltaMin
            lastDeltaRecord = i
        }
        if (delta > deltaMax) {
            delta = deltaMax
            lastDeltaRecord = i
        }
    }
    return lastDeltaRecord
}

private fun convertDpToPx(sp: Float, context: Context): Float {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        sp,
        context.resources.displayMetrics
    )
}