Сохранение соотношения сторон ImageButton в AppWidget при изменении размера - PullRequest
0 голосов
/ 01 октября 2019

В моем виджете рабочего стола есть кнопка ImageButton. Предполагается, что он становится больше / меньше, когда пользователь изменяет размер виджета, но остается совершенно круглым.

Следующий код работает на эмуляторе, но на некоторых телефонах круг все еще имеет овальную форму (возможно, на 10% больше, чемширокий). Я использую заполнение, потому что я не нашел способ установить размер удаленного представления в коде. Спасибо.

AppWidgetProvider:

@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
...
onUpdate(context, appWidgetManager, new int[]{appWidgetId});
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

Bundle options = appWidgetManager.getAppWidgetOptions(myIds[id]);
                    int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
                    int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

                    int width = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minWidth, context.getResources().getDisplayMetrics()));
                    int height = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minHeight, context.getResources().getDisplayMetrics()));

                    views.setViewPadding(R.id.image_button,0, (height-width)/2,0,(height-width)/2);
...

widget.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llWidget"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/image_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/widget_button"
        android:visibility="visible"
        android:background="@drawable/widget_button"
        android:elevation="2dp"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text=""
        android:elevation="2dp"
        android:textAllCaps="true"/>

</FrameLayout>

widget_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">

    <item android:id="@android:id/background">
        <shape android:shape="oval">
        </shape>
    </item>

</ripple>
...