با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
میتوانید یک تصویر را بر روی یک شکل بریده شده قرار دهید و سایههایی را در اطراف محیط شکل بکشید تا حسی سهبعدی ایجاد کنید. این تکنیک برای ایجاد طرح هایی مانند آواتارها و تصاویر بندانگشتی محصول یا نمایش لوگو با اشکال سفارشی مفید است.
برای نمایش تصویر بریده شده به شکل، باید موارد زیر را انجام دهید:
شکل را ایجاد کنید.
تصویر را به شکل برش دهید.
سازگاری نسخه
این پیاده سازی مستلزم آن است که minSDK پروژه شما روی سطح API 21 یا بالاتر تنظیم شود.
وابستگی ها
یک شکل ایجاد کنید
کد زیر یک شکل سفارشی ایجاد می کند که می تواند به صورت پویا یک چند ضلعی گرد را ترسیم و ارائه دهد:
RoundedPolygon.getBounds() یک تابع پسوندی را در کلاس RoundedPolygon تعریف می کند تا مرزهای آن را محاسبه کند.
کلاس RoundedPolygonShape رابط Shape پیاده سازی می کند و به شما امکان می دهد یک شکل سفارشی (یک چند ضلعی گرد) را در Jetpack Compose تعریف کنید.
شکل از یک Matrix برای مدیریت عملیات مقیاس بندی و ترجمه برای رندر انعطاف پذیر استفاده می کند.
تابع createOutline() یک شی RoundedPolygon را می گیرد، آن را مقیاس بندی و ترجمه می کند تا در یک اندازه معین قرار گیرد، و یک شی Outline برمی گرداند که شکل نهایی را که باید ترسیم شود، توصیف می کند.
تصویر را به یک شکل برش دهید
کد زیر تصویر را به یک شش ضلعی برش می دهد و یک سایه ریز ظریف برای ایجاد حس عمق اضافه می کند:
اشیاء RoundedPolygon و RoundedPolygonShape برای تعریف و اعمال یک شکل شش ضلعی بر روی تصویر استفاده می شود.
کد از graphicsLayer برای اضافه کردن سایه مبتنی بر ارتفاع به تصویر استفاده می کند. این باعث ایجاد حس عمق و جدایی بصری از پس زمینه می شود.
استفاده از بلوک های remember عملکرد را با اطمینان از اینکه شکل و تعاریف برش فقط یک بار محاسبه می شود و برای ترکیب های بعدی رابط کاربری به خاطر سپرده می شود، بهینه می کند.
نتایج
شکل 1. شکل سفارشی به عنوان گیره اعمال می شود.
مجموعه هایی که حاوی این راهنما هستند
این راهنما بخشی از مجموعههای راهنمای Quick Guide است که اهداف توسعه Android گستردهتری را پوشش میدهد:
نمایش تصاویر
تکنیک هایی را برای استفاده از تصاویر روشن و جذاب کشف کنید تا به برنامه اندروید خود ظاهر و احساس زیبایی ببخشید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-02-06 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-02-06 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Display an image clipped to a shape\n\n\u003cbr /\u003e\n\nYou can fit an image to a clipped shape, and draw shadows around the perimeter\nof the shape to impart a three-dimensional feel. This technique is useful for\ncreating designs such as avatars and product thumbnails, or displaying\nlogos with custom shapes.\n\nTo display an image clipped to a shape, you must do the following:\n\n- Create the shape.\n- Clip the image to the shape.\n\nVersion compatibility\n---------------------\n\nThis implementation requires that your project minSDK be set to API level 21 or\nhigher.\n\n### Dependencies\n\n### Kotlin\n\n\u003cbr /\u003e\n\n```kotlin\n implementation(platform(\"androidx.compose:compose-bom:2025.08.00\"))\n implementation(\"androidx.graphics:graphics-shapes:1.0.0-alpha05\")\n \n```\n\n\u003cbr /\u003e\n\n### Groovy\n\n\u003cbr /\u003e\n\n```groovy\n implementation platform('androidx.compose:compose-bom:2025.08.00')\n implementation 'androidx.graphics:graphics-shapes:1.0.0-alpha05'\n \n```\n\n\u003cbr /\u003e\n\nCreate a shape\n--------------\n\nThe following code creates a custom shape that can dynamically draw and render\na rounded polygon:\n\n\n```kotlin\nfun RoundedPolygon.getBounds() = calculateBounds().let { Rect(it[0], it[1], it[2], it[3]) }\nclass RoundedPolygonShape(\n private val polygon: RoundedPolygon,\n private var matrix: Matrix = Matrix()\n) : Shape {\n private var path = Path()\n override fun createOutline(\n size: Size,\n layoutDirection: LayoutDirection,\n density: Density\n ): Outline {\n path.rewind()\n path = polygon.toPath().asComposePath()\n matrix.reset()\n val bounds = polygon.getBounds()\n val maxDimension = max(bounds.width, bounds.height)\n matrix.scale(size.width / maxDimension, size.height / maxDimension)\n matrix.translate(-bounds.left, -bounds.top)\n\n path.transform(matrix)\n return Outline.Generic(path)\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/graphics/ShapesSnippets.kt#L353-L375\n```\n\n\u003cbr /\u003e\n\n### Key points about the code\n\n- `RoundedPolygon.getBounds()` defines an extension function on the [`RoundedPolygon`](/reference/kotlin/androidx/graphics/shapes/RoundedPolygon) class to calculate its bounds.\n- The `RoundedPolygonShape` class implements the [`Shape`](/reference/kotlin/androidx/compose/ui/graphics/Shape) interface, allowing you to define a custom shape (a rounded polygon) in Jetpack Compose.\n- The shape uses a [`Matrix`](/reference/kotlin/androidx/compose/ui/graphics/Matrix) to manage scaling and translation operations for flexible rendering.\n- The `createOutline()` function takes a `RoundedPolygon` object, scales and translates it to fit within a given size, and returns an [`Outline`](/reference/kotlin/androidx/compose/ui/graphics/Outline) object that describes the final shape to be drawn.\n\nClip the image to a shape\n-------------------------\n\nThe following code crops the image to a hexagon, and adds a subtle drop\nshadow to provide a sense of depth:\n\n\n```kotlin\nval hexagon = remember {\n RoundedPolygon(\n 6,\n rounding = CornerRounding(0.2f)\n )\n}\nval clip = remember(hexagon) {\n RoundedPolygonShape(polygon = hexagon)\n}\nBox(\n modifier = Modifier\n .clip(clip)\n .background(MaterialTheme.colorScheme.secondary)\n .size(200.dp)\n) {\n Text(\n \"Hello Compose\",\n color = MaterialTheme.colorScheme.onSecondary,\n modifier = Modifier.align(Alignment.Center)\n )\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/graphics/ShapesSnippets.kt#L382-L402\n```\n\n\u003cbr /\u003e\n\n### Key points about the code\n\n- The `RoundedPolygon` and `RoundedPolygonShape` objects are used to define and apply a hexagonal shape to the image.\n- The code uses [`graphicsLayer`](/reference/kotlin/androidx/compose/ui/graphics/package-summary#(androidx.compose.ui.Modifier).graphicsLayer(kotlin.Function1)) to add an elevation-based shadow to the image. This creates a sense of depth and visual separation from the background.\n- The use of [`remember`](/reference/kotlin/androidx/compose/runtime/package-summary#remember(kotlin.Function0)) blocks optimizes performance by ensuring that the shape and clipping definitions are calculated only once and remembered for later recompositions of the UI.\n\nResults\n-------\n\n**Figure 1.** Custom shape applied as clip.\n\nCollections that contain this guide\n-----------------------------------\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\n### Display images\n\nDiscover techniques for using bright, engaging visuals to give your Android app a beautiful look and feel. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-images) \n\nHave questions or feedback\n--------------------------\n\nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]