Giới thiệu
Bài viết hướng dẫn lấy kích thước chuẩn của icon launcher lớn (large icon) trên thiết bị qua ActivityManager.getLauncherLargeIconSize(). Dùng cho adaptive icon, notification large icon, custom launcher.
Các bước thực hiện
Bước 1: Tạo project mới
Android Studio → File → New Project → Empty Activity.
Bước 2: Giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/tvIconSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launcher Large Icon Size"
android:textSize="18sp" />
<ImageView
android:id="@+id/ivLargeIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:contentDescription="Large Icon" />
</LinearLayout>
Bước 3: Code (MainActivity.kt)
package com.example.launchericonsize
import android.app.ActivityManager
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val packageManager = packageManager
// 1. Lấy kích thước large icon chuẩn của hệ thống
val size = activityManager.getLauncherLargeIconSize()
tvIconSize.text = "Launcher Large Icon Size: ${size.x} x ${size.y} px"
// 2. Load icon app với kích thước đó
loadAppIconAtSize(packageName, size.x, size.y)
}
private fun loadAppIconAtSize(packageName: String, width: Int, height: Int) {
try {
val appInfo = packageManager.getApplicationInfo(packageName, 0)
val drawable = packageManager.getApplicationIcon(appInfo)
// Scale bitmap to exact size
val bitmap: Bitmap = if (drawable is BitmapDrawable) {
Bitmap.createScaledBitmap(drawable.bitmap, width, height, true)
} else {
// VectorDrawable or other -> draw to canvas
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = android.graphics.Canvas(bitmap)
drawable.setBounds(0, 0, width, height)
drawable.draw(canvas)
bitmap
}
ivLargeIcon.setImageBitmap(bitmap)
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
}
}
Giải thích
getLauncherLargeIconSize(): Trả vềPoint(x, y)pixel cho large icon (thường 96x96, 128x128, 192x192 tùy density).- Khác với
getLauncherActivityIconSize()(icon nhỏ trong app drawer) vàgetDefaultThumbnailSize(). - Dùng cho: Custom launcher, notification largeIcon, widget preview, shortcut icon.
Kích thước icon Android chuẩn (Reference)
| Loại | Method | Kích thước điển hình (mdpi baseline) |
|---|---|---|
| Launcher Icon (App icon) | getLauncherActivityIconSize() |
48×48 dp |
| Large Icon (Notification, Recents) | getLauncherLargeIconSize() |
96×96 dp ~ 128×128 dp |
| Action Bar Icon | - | 24×24 dp |
| Notification Small Icon | - | 24×24 dp |
| Adaptive Icon (Foreground) | - | 108×108 dp (safe zone 72×72) |
Adaptive Icon (Android 8.0+)
App hiện đại nên dùng Adaptive Icon (2 layer: foreground + background):
<!-- res/mipmap-anydpi-v26/ic_launcher.xml -->
<adaptive-icon xmlns:android="https://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_monochrome" /> <!-- Android 13+ -->
</adaptive-icon>
Hệ thống tự mask thành hình tròn, vuông bo góc, squircle tùy launcher.
Lưu ý
- Kích thước trả về là **pixel**, không phải dp. Chuyển đổi:
px = dp * density. - Density buckets: mdpi(1x), hdpi(1.5x), xhdpi(2x), xxhdpi(3x), xxxhdpi(4x).
- Luôn cung cấp icon cho mọi density trong
mipmap-*folders. - Dùng
ImageView.setImageBitmap()với bitmap đã scale đúng kích thước để tránh OOM.