با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
اگر مراقب نباشید، کار با تصاویر می تواند به سرعت مشکلات عملکرد را ایجاد کند. هنگام کار با بیت مپ های بزرگ به راحتی می توانید با یک OutOfMemoryError مواجه شوید. این بهترین شیوهها را دنبال کنید تا مطمئن شوید برنامهتان بهترین عملکرد را دارد.
فقط اندازه بیت مپ مورد نیاز خود را بارگیری کنید
اکثر گوشی های هوشمند دارای دوربین هایی با وضوح بالا هستند که فایل های تصویری بزرگ تولید می کنند. اگر تصویری را روی صفحه نمایش میدهید، باید وضوح تصویر را کاهش دهید یا فقط تصویر را تا اندازه ظرف تصویر خود بارگیری کنید. بارگذاری مداوم تصاویر بزرگتر از حد مورد نیاز می تواند حافظه پنهان GPU را خسته کند و منجر به رندر UI عملکرد کمتری شود.
برای مدیریت اندازه تصویر:
حجم فایل های تصویری خود را تا حد امکان کوچک کنید (بدون تاثیر بر روی تصویر خروجی).
ارائه تصاویر کوچکتر برای وضوح های مختلف صفحه (به نکته شماره 3 مراجعه کنید)،
از کتابخانه بارگیری تصویر استفاده کنید، که تصویر شما را به اندازه نمای شما بر روی صفحه نمایش کوچک می کند. این می تواند به بهبود عملکرد بارگیری صفحه نمایش شما کمک کند.
در صورت امکان از بردارها روی نقشه های بیتی استفاده کنید
هنگامی که چیزی را به صورت بصری بر روی صفحه نمایش می دهید، باید تصمیم بگیرید که آیا می توان آن را به صورت برداری نمایش داد یا خیر. تصاویر برداری را به بیت مپ ترجیح دهید، زیرا وقتی آنها را در اندازه های مختلف مقیاس می کنید، پیکسلی نمی شوند. با این حال، همه چیز را نمی توان به صورت وکتور نشان داد - تصاویر گرفته شده با دوربین را نمی توان به وکتور تبدیل کرد.
منابع جایگزین را برای اندازه های مختلف صفحه نمایش فراهم کنید
اگر در حال ارسال تصاویر با برنامه خود هستید، در نظر بگیرید دارایی هایی با اندازه های مختلف برای وضوح های مختلف دستگاه ارائه دهید. این میتواند به کاهش حجم دانلود برنامه شما در دستگاهها و بهبود عملکرد کمک کند، زیرا تصویری با وضوح پایینتر در دستگاهی با وضوح پایینتر بارگیری میکند. برای اطلاعات بیشتر در مورد تهیه بیت مپ های جایگزین برای اندازه های مختلف دستگاه، اسناد بیت مپ جایگزین را بررسی کنید .
هنگام استفاده از ImageBitmap ، قبل از ترسیم، prepareToDraw فراخوانی کنید
هنگام استفاده از ImageBitmap ، برای شروع فرآیند آپلود بافت در GPU، ImageBitmap#prepareToDraw() را قبل از ترسیم آن فراخوانی کنید. این به GPU کمک می کند تا بافت را آماده کند و عملکرد نمایش تصویر روی صفحه را بهبود بخشد. اکثر کتابخانه های بارگذاری تصویر قبلاً این بهینه سازی را انجام می دهند، اما اگر خودتان با کلاس ImageBitmap کار می کنید، باید به خاطر داشته باشید.
ترجیح می دهید یک IntDrawableRes یا URL را به عنوان پارامتر به composable خود به جای Painter ارسال کنید.
به دلیل پیچیدگی های برخورد با تصاویر (به عنوان مثال، نوشتن یک تابع برابر برای Bitmaps از نظر محاسباتی گران است)، API Painter به صراحت به عنوان یک کلاس Stable علامت گذاری نشده است. کلاس های ناپایدار می توانند منجر به ترکیب مجدد غیرضروری شوند زیرا کامپایلر نمی تواند به راحتی استنباط کند که داده ها تغییر کرده اند.
بنابراین، ترجیحاً به جای ارسال Painter به عنوان پارامتر، یک URL یا شناسه منبع قابل ترسیم را به عنوان پارامتر به composable خود ارسال کنید.
// Prefer this:@ComposablefunMyImage(url:String){}// Over this:@ComposablefunMyImage(painter:Painter){}
یک بیت مپ را بیشتر از زمانی که نیاز دارید در حافظه ذخیره نکنید
هر چه بیت مپ های بیشتری را در حافظه بارگذاری کنید، احتمال اینکه حافظه دستگاه تمام شود بیشتر است. به عنوان مثال، اگر لیست بزرگی از تصاویر را روی صفحه بارگیری می کنید، LazyColumn یا LazyRow استفاده کنید تا اطمینان حاصل کنید که هنگام پیمایش یک لیست بزرگ، حافظه آزاد می شود.
تصاویر بزرگ را با فایل AAB/APK خود بسته بندی نکنید
یکی از دلایل اصلی برای دانلود برنامه بزرگ به دلیل گرافیک هایی است که در داخل فایل AAB یا APK بسته بندی شده اند. از ابزار تحلیلگر APK استفاده کنید تا مطمئن شوید که فایل های تصویری بزرگتر از مقدار مورد نیاز را بسته بندی نمی کنید. اندازه ها را کاهش دهید یا در نظر بگیرید که تصاویر را روی سرور قرار دهید و فقط در صورت نیاز آنها را دانلود کنید.
{% کلمه به کلمه %} {% آخر کلمه %}
برای شما توصیه می شود
توجه: وقتی جاوا اسکریپت خاموش است، متن پیوند نمایش داده می شود
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی."],[],[],null,["Working with images can quickly introduce performance issues if you aren't\ncareful. You can quite easily run into an `OutOfMemoryError` when working\nwith large bitmaps. Follow these best practices to ensure your app performs at\nits best.\n\nOnly load the size of the bitmap you need\n\nMost smartphones have high resolution cameras that produce large image files. If\nyou're showing an image on screen, you must either reduce the resolution of the\nimage or only load the image up to the size of your image container. The\nconstant loading of larger than needed images can exhaust GPU caches, leading to\nless performant UI rendering.\n\nTo manage image sizes:\n\n- Scale down your image files to be as small as possible (without affecting output image).\n- Consider [converting your images to WEBP](/studio/write/convert-webp) format instead of JPEG or PNGs.\n- Supply smaller images for different screen resolutions (see [Tip #3](/develop/ui/compose/graphics/images/optimization#screen-sizes)),\n- Use an [image loading library](/develop/ui/compose/graphics/images/loading#load_an_image_from_the_internet), which scales down your image to fit the size of your view on screen. This can help improve the loading performance of your screen.\n\n| **Caution:** Using `painterResource` will **not** scale your image to the size of the Composable that is visible on screen. If you have a large image in a small Composable, be sure to use an image loading library which scales the image down for you to fit the bounds.\n\nUse vectors over bitmaps where possible\n\nWhen representing something visually on screen, you need to decide if it can be\nrepresented as a vector or not. Prefer vector images over bitmaps, as they\ndon't pixelate when you scale them to different sizes. However, not everything\ncan be represented as a vector - images taken with a camera can't be converted\ninto a vector.\n\nSupply alternative resources for different screen sizes\n\nIf you are shipping images with your app, consider supplying different sized\nassets for different device resolutions. This can help reduce the download size\nof your app on devices, and improve performance as it'll load up a lower\nresolution image on a lower resolution device. For more information on providing\nalternative bitmaps for different device sizes, [check out the alternative\nbitmap documentation](/training/multiscreen/screendensities#TaskProvideAltBmp).\n\nWhen using `ImageBitmap`, call `prepareToDraw` before drawing\n\nWhen using `ImageBitmap`, to start the process of uploading the texture to the\nGPU, call [`ImageBitmap#prepareToDraw()`](/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#prepareToDraw()) before actually drawing it. This\nhelps the GPU prepare the texture and improve the performance of showing a\nvisual on screen. Most image loading libraries already do this optimization, but\nif you are working with the `ImageBitmap` class yourself, it is something to\nkeep in mind.\n\nPrefer passing a `Int` `DrawableRes` or URL as parameters into your composable instead of `Painter`\n\nDue to the complexities of dealing with images (for example, writing an equals\nfunction for `Bitmaps` would be computationally expensive), the `Painter` API is\nexplicitly not marked as a [Stable](https://medium.com/androiddevelopers/jetpack-compose-stability-explained-79c10db270c8) class. Unstable classes can\nlead to unnecessary recompositions because the compiler cannot easily infer if\nthe data has changed.\n\nTherefore, it is preferable to pass a URL or drawable resource ID as parameters\nto your composable, instead of passing a `Painter` as a parameter. \n\n // Prefer this:\n @Composable\n fun MyImage(url: String) {\n\n }\n // Over this:\n @Composable\n fun MyImage(painter: Painter) {\n\n }\n\nDon't store a bitmap in memory longer than you need it\n\nThe more bitmaps you load into memory, the more likely it is that you could run\nout of memory on the device. For instance, if loading a large list of Image\ncomposables on screen, use `LazyColumn` or `LazyRow` to ensure that memory is\nfreed up when scrolling a large list.\n\nDon't package large images with your AAB/APK file\n\nOne of the top causes for large app download size is due to graphics that are\npackaged inside the AAB or APK file. Use the [APK analyzer](/studio/debug/apk-analyzer) tool to ensure\nthat you aren't packaging larger than required image files. Reduce the sizes or\nconsider placing the images on a server and only downloading them when required.\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [ImageBitmap vs ImageVector {:#bitmap-vs-vector}](/develop/ui/compose/graphics/images/compare)\n- [Save UI state in Compose](/develop/ui/compose/state-saving)\n- [Jetpack Compose Phases](/develop/ui/compose/phases)"]]