【问题标题】:Resizing wallpaper after reboot重启后调整壁纸大小
【发布时间】:2017-07-04 22:30:58
【问题描述】:

我为 Android 4.4.2 构建了我的自定义启动器,但壁纸存在一些问题。当我设置新壁纸时,一切都很好地适合屏幕,但在重新加载设备后,壁纸会被缩放并且我看到唯一的中心部分,即使默认情况下屏幕尺寸和图像尺寸相同。 我认为这是因为多屏模式,但我不确定。现在我每次都在 ACTION_BOOT 事件上使用广播接收器重置壁纸,但它提供了一些滞后并且看起来不像是一个优雅的解决方案。 我用这个类设置和重新加载壁纸:

public class WallpaperUtils {

public static final String PREF_CUSTOM_WALLPAPER = "PREF_CUSTOM_WALLPAPER";
public static final String PREF_WALLPAPER_RES = "PREF_WALLPAPER_RES";

public static void reloadWallpaper(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean isCustomWallpaper = prefs.getBoolean(PREF_CUSTOM_WALLPAPER, false);

    if (isCustomWallpaper) {
        int wallRes = prefs.getInt(PREF_WALLPAPER_RES, 0);
        setWallpaper(context, wallRes);
    }


}

public static void setWallpaper(Context context, int wallRes) {

    WallpaperManager manager = WallpaperManager.getInstance(context.getApplicationContext());

    WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    DisplayMetrics metrics = new DisplayMetrics();
    window.getDefaultDisplay().getMetrics(metrics);

    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    Bitmap tempBitmap = BitmapFactory.decodeResource(context.getResources(), wallRes);
    Bitmap bitmap = Bitmap.createScaledBitmap(tempBitmap, width, height, true);

    manager.setWallpaperOffsetSteps(1, 1);
    manager.suggestDesiredDimensions(width, height);

    try {
        manager.setBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

在 AndroidManifest 主屏幕中看起来像:

    <activity
        android:name=".ui.main.MainActivity"
        android:launchMode="singleTask"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

在默认启动器中,壁纸也会缩放,所以我认为样式没有问题,但我找不到问题出在哪里。

【问题讨论】:

    标签: android launcher wallpaper android-launcher android-wallpaper


    【解决方案1】:

    在研究了 android 主题和样式后,我会明白出了什么问题。

    @android:style/主题

    已弃用,不建议在 API 11 之后使用。

    所以我只是通过壁纸管理器在 onStart 回调中获取主屏幕活动中的壁纸,并将其设置为主布局的背景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      相关资源
      最近更新 更多