【问题标题】:How to set different widget icon upon resize如何在调整大小时设置不同的小部件图标
【发布时间】:2017-02-13 11:25:34
【问题描述】:

我在文件夹 drawable-mdpi/drawable-xhdpi 等中有一个用于不同屏幕尺寸的小部件图标。

假设设备具有 mdpi 屏幕,因此 android 将使用 drawable-mdpi/widget_pic.png [48x48px] 显示我的 1x1 小部件

当我将小部件的大小调整为2x2 时,它将使用相同的图标,但会放在中心而不缩放。
我希望 android 看到小部件大小变大了 2 倍,并相应地从资源中选择一个图标(即drawable-xhdpi/widget_pic.png[96x96px])。

所以我的问题是如何做到这一点?
我可以想到一个解决方案,但似乎有缺陷:
捕捉onAppWidgetOptionsChanged() 事件并在那里替换layout/image_src。
但是我不知道在哪里可以得到2x 图标。


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example">
    <application>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/myWidgetXml"/>
        </receiver>
    </application>
</manifest>

myWidgetXml.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/myLayout"
    android:initialLayout="@layout/myLayout"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:resizeMode="horizontal"
    android:updatePeriodMillis="86400000"
    android:widgetCategory="home_screen">
</appwidget-provider>

myLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin"
    android:layout_margin="0dp"
    android:gravity="center"
    android:background="@android:drawable/alert_dark_frame"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/WidgetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/widget_pic" />
    <TextView
        android:id="@+id/WidgetTextView"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        android:layout_marginTop="6dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />
</LinearLayout>

MyWidget.java

public class MyWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds)
            updateAppWidget(context, appWidgetManager, appWidgetId);
    }

    private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout);
        Intent intent = new Intent(context, MainActivity.class);
        Uri.Builder builder = new Uri.Builder();
        intent.setData(builder.build());
        intent.setAction(Intent.ACTION_VIEW);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);
        views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic);
        views.setTextViewText(R.id.WidgetTextView, "text");
        _appWidgetManager.updateAppWidget(WidgetId, views);
    }
}

【问题讨论】:

  • 在所有文件夹中放同一个分辨率图标(最高分辨率)..没关系..
  • @GokulNC,我喜欢这个想法,但我的应用中图标的最大尺寸是 800x800 像素,我猜它在 ldpi 上看起来不太清晰 :)

标签: java android android-widget android-icons


【解决方案1】:

实际上,在运行时告诉 android 更改资源是不可能的
因为它会在应用安装期间加载它们
所以有两种选择:

  • 使用android:scaleType="fitCenter"android:adjustViewBounds="true" 缩放图像
  • 在每个 drawable-* 文件夹中有 2 组资源
    (在我的例子中是 widget_pic1x1.pngwidget_pic2x2.png

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2017-01-09
    • 2016-06-01
    • 2011-05-18
    相关资源
    最近更新 更多