Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีดึงขนาด Launcher Icon ขนาดใหญ่ใน Android

ภาพรวม

บทความนี้สาธิตวิธีดึงขนาดของ Launcher Large Icon (ไอคอนแอปขนาดใหญ่ใน Launcher) ผ่าน ActivityManager.getLauncherLargeIconSize()

ขั้นตอนที่ 1: สร้างโปรเจกต์

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="24sp"
        android:gravity="center"
        android:padding="16dp" />

</LinearLayout>

ขั้นตอนที่ 3: โค้ดหลัก (java/MainActivity.java)

package com.example.myapplication;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        int launcherLargeIconSize = activityManager.getLauncherLargeIconSize();

        // ขนาดอื่นๆ ที่เกี่ยวข้อง
        int appTaskThumbnailSize = activityManager.getAppTaskThumbnailSize();
        int memoryClass = activityManager.getMemoryClass();
        int largeMemoryClass = activityManager.getLargeMemoryClass();

        String info = String.format(
            "Launcher Large Icon Size: %d x %d px\n" +
            "App Task Thumbnail Size: %d x %d px\n" +
            "Memory Class: %d MB\n" +
            "Large Memory Class: %d MB",
            launcherLargeIconSize, launcherLargeIconSize,
            appTaskThumbnailSize, appTaskThumbnailSize,
            memoryClass, largeMemoryClass
        );

        textView.setText(info);
    }
}

คำอธิบาย

  • getLauncherLargeIconSize() - ขนาดไอคอนขนาดใหญ่สำหรับ Launcher (Pixel) - ใช้สร้าง Adaptive Icon, Shortcut Icon
  • getAppTaskThumbnailSize() - ขนาด Thumbnail ใน Recent Apps
  • getMemoryClass() - Heap limit ปกติ (MB)
  • getLargeMemoryClass() - Heap limit สำหรับ largeHeap=true (MB)

Adaptive Icon (Android 8.0+)

ขนาดมาตรฐาน:

  • Overall: 108x108 dp (Safe zone: 72x72 dp)
  • Launcher Large Icon: มักเป็น 192x192 px หรือ 168x168 px ขึ้นอยู่กับ Launcher

การใช้งาน

// สร้าง Bitmap ขนาดพอดีสำหรับ Shortcut/Notification
Bitmap largeIcon = Bitmap.createBitmap(launcherLargeIconSize, launcherLargeIconSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(largeIcon);
// วาด icon ที่นี่...

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    .setLargeIcon(largeIcon)
    .setSmallIcon(R.drawable.ic_notification);

ทดสอบ

รันแอป จะแสดงขนาดต่างๆ เช่น: Launcher Large Icon: 192x192 px Task Thumbnail: 172x172 px Memory Class: 256 MB