AppIconLoader
Android app icon loader from AOSP iconloaderlib, with optional Glide and Coil integration.
This is not an officially supported Google product.
Why AppIconLoader?
Because PackageManager.getApplicationIcon()
(or PackageItemInfo.loadIcon()
) just doesn't work well with adaptive icons. Non-adaptive icons usually have some shadow baked in (it's the recommended behavior), however adaptive icons never contain a shadow themselves, so we'll need to manually add the shadow or icons with a white background will just blend into our app's own background.
This library packaged the AOSP iconloaderlib for loading app icons, which has proper shadow and badging logic, and added easy integration with Glide and Coil.
Meanwhile, by passing true
for the shrinkNonAdaptiveIcons
parameter, this library can also synthesize adaptive icons for apps that don't have it.
Preview
Integration
Gradle:
// For using with Glide.
implementation 'me.zhanghai.android.appiconloader:appiconloader-glide:1.5.0'
// For using with Coil.
implementation 'me.zhanghai.android.appiconloader:appiconloader-coil:1.5.0'
// For using AppIconLoader directly.
implementation 'me.zhanghai.android.appiconloader:appiconloader:1.5.0'
// For using iconloaderlib directly.
implementation 'me.zhanghai.android.appiconloader:appiconloader-iconloaderlib:1.5.0'
Usage
Glide integration
See Glide's documentation on registering a ModuleLoader
.
Inside your implementation of AppGlideModule.registerComponents()
, you can have something like the following code fragment:
int iconSize = context.getResources().getDimensionPixelSize(R.dimen.app_icon_size);
registry.prepend(PackageInfo.class, Bitmap.class, new AppIconModelLoader.Factory(iconSize,
false, context));
See also the sample app's AppGlideModule
implementation.
After the setup above, Glide will support loading app icons with the app's PackageInfo
.
GlideApp.with(imageView)
.load(packageInfo)
.into(imageView);
Coil integration
val iconSize = context.resources.getDimensionPixelSize(R.dimen.app_icon_size)
Coil.setImageLoader(
ImageLoader.Builder(context)
.components {
add(AppIconKeyer())
add(AppIconFetcher.Factory(iconSize, false, context))
}
.build()
)
After the setup above, Coil will support loading app icons with the app's PackageInfo
.
imageView.loadAny(packageInfo)
AppIconLoader
AppIconLoader
is the API exposed by this library, and you can simply call AppIconLoader.loadIcon()
to load an app icon. You can also use AppIconLoader.getIconKey()
to generate a cache key for your loaded icon.
iconloaderlib
Please refer to its source code.
License
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.