ווידג'ט ההגדלה זמין בגרסת Android 9 (רמת API 28) ואילך,
זכוכית מגדלת וירטואלית שמציגה עותק מוגדל של View באמצעות
חלונית שכבת-על שמייצגת את העדשה. התכונה משפרת את הוספת הטקסט
ובחירת חוויית המשתמש. בעת החלת זכוכית המגדלת על טקסט, משתמש יכול
למקם את הסמן או את נקודות האחיזה המדויקות באמצעות הצגת
הטקסט בחלונית שעוקבת אחרי האצבע שלהם.
איור 1 מראה איך הגדלת התצוגה מאפשרת בחירת טקסט. ממשקי ה-API של זכוכית המגדלת
אינן מקושרות לטקסט, ואפשר להשתמש בווידג'ט הזה במגוון תרחישים לדוגמה,
כמו קריאת טקסט קטן או הגדלה של שמות מקומות שקשה לראות במפות.
איור 1. הגדלת הטקסט. כשהמשתמש גורר את הבחירה הנכונה
התכונה 'זכוכית מגדלת' תקפוץ כדי לעזור במיקום מדויק.
זכוכית המגדלת כבר משולבת בווידג'טים של פלטפורמה כמו TextView,
EditText ו-WebView. השיטה הזו מאפשרת מניפולציה עקבית של הטקסט באפליקציות שונות.
הווידג'ט כולל API פשוט ואפשר להשתמש בו כדי להגדיל כל View
בהתאם להקשר של האפליקציה.
שימוש ב-API
ניתן להשתמש בזכוכית המגדלת באופן פרוגרמטי בתצוגה שרירותית באופן הבא:
בהנחה שהיררכיית התצוגות היא הפריסה הראשונה,
ומכיל אזור שמתרכז בקואורדינטות הנתונות בתצוגה.
החלונית תופיע מעל נקודת המרכז של התוכן שמועתק.
זכוכית המגדלת ממשיכה ללא הגבלת זמן עד שהמשתמש סוגר אותה.
בקטע הקוד הבא מוסבר איך לשנות את הרקע של תמונה מוגדלת
תצוגה:
Kotlin
view.setBackgroundColor(...)
Java
view.setBackgroundColor(...);
בהנחה שצבע הרקע גלוי בתוך חלון ההגדלה,
ישן, כי אזור בתצוגה עם הרקע הישן עדיין
מסכים. כדי לרענן את התוכן, לוחצים על
update(), באופן הבא:
Kotlin
view.post{magnifier.update()}
Java
view.post(magnifier::update);
בסיום, סוגרים את זכוכית המגדלת באמצעות הפעלה של
שיטת dismiss():
Kotlin
magnifier.dismiss()
Java
magnifier.dismiss();
הגדלה בעקבות אינטראקציה של משתמשים
תרחיש לדוגמה נפוץ עבור זכוכית המגדלת הוא לאפשר למשתמש להגדיל אזור של תצוגה על ידי
כשנוגעים בו, כפי שמוצג באיור 2.
איור 2. זכוכית המגדלת עוקבת אחר המגע של המשתמש. זה כן
הוחל על ViewGroup שמכיל 'ImageView' בצד שמאל
ו-TextView מימין.
ניתן לעשות זאת על ידי עדכון זכוכית המגדלת בהתאם לאירועי המגע שהתקבלו
לפי התצוגה, באופן הבא:
imageView.setOnTouchListener(newView.OnTouchListener(){@OverridepublicbooleanonTouch(Viewv,MotionEventevent){switch(event.getActionMasked()){caseMotionEvent.ACTION_DOWN:// Fall through.caseMotionEvent.ACTION_MOVE:{finalint[]viewPosition=newint[2];v.getLocationOnScreen(viewPosition);magnifier.show(event.getRawX()-viewPosition[0],event.getRawY()-viewPosition[1]);break;}caseMotionEvent.ACTION_CANCEL:// Fall through.caseMotionEvent.ACTION_UP:{magnifier.dismiss();}}returntrue;}});
שיקולים נוספים בזמן הגדלה של טקסט
לווידג'טים של טקסט בפלטפורמה, חשוב להבין מהי זכוכית מגדלת ספציפית
ולהפעיל את זכוכית המגדלת עבור תצוגת הטקסט המותאמת אישית שלכם באופן עקבי
בכל פלטפורמת Android. מה כדאי לעשות?
זכוכית המגדלת מופעלת מיד כאשר המשתמש אוחז הכנסה או
נקודת אחיזה לבחירה.
זכוכית המגדלת תמיד עוקבת בצורה חלקה אחר אצבע המשתמש לרוחב, בזמן
אנכית הוא קבוע למרכז קו הטקסט הנוכחי.
בתנועה אופקית, זכוכית המגדלת נעה רק בין שמאל לבין
הגבולות הימניים של הקו הנוכחי. יתרה מכך, כשהמגע של המשתמש עוזב
את הגבולות האלה ואת המרחק האופקי בין המגע
הגבול גדול ממחצית מהרוחב המקורי של תוכן חלון ההגדלה,
הגדלת התצוגה נסגרת, מכיוון שהסמן לא גלוי יותר בתוך
זכוכית מגדלת.
הגדלת התצוגה אף פעם לא מופעלת כשגופן הטקסט גדול מדי. הטקסט הוא
נחשב גדול מדי כשההבדל בין שגרת הגופן לבין
הטיפוס גדול יותר מגובה התוכן שמתאים למגדיל התצוגה.
הפעלת זכוכית המגדלת במקרה הזה לא מוסיפה ערך.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[[["התוכן קל להבנה","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-07-27 (שעון UTC)."],[],[],null,["# Implement a text magnifier\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to use text in Compose. \n[Modifier.magnifier() →](/reference/kotlin/androidx/compose/foundation/package-summary#(androidx.compose.ui.Modifier).magnifier(kotlin.Function1,kotlin.Function1,kotlin.Float,androidx.compose.foundation.MagnifierStyle,kotlin.Function1)) \n\n\u003cbr /\u003e\n\nAvailable in Android 9 (API level 28) and later, the magnifier widget is a\nvirtual magnifying glass that displays an enlarged copy of a `View` through an\noverlay pane that represents the lens. The feature improves the text insertion\nand selection user experience. When applying the magnifier to text, a user can\nprecisely position the cursor or the selection handles by viewing the magnified\ntext in a pane that follows their finger.\n\nFigure 1 shows how the magnifier facilitates selecting text. The magnifier APIs\naren't tied to text, and you can use this widget in a variety of use cases, such\nas reading small text or enlarging hard-to-see place names on maps.\n**Figure 1.** Magnifying text. When the user drags the right selection handle, the magnifier pops up to help with accurate placement.\n\nThe magnifier is already integrated with platform widgets such as `TextView`,\n`EditText`, and `WebView`. It provides consistent text manipulation across apps.\nThe widget comes with a simple API and can be used to magnify any `View`\ndepending on your app's context.\n\nAPI usage\n---------\n\nYou can use the magnifier programmatically on an arbitrary view as follows: \n\n### Kotlin\n\n```kotlin\nval view: View = findViewById(R.id.view)\nval magnifier = Magnifier.Builder(view).build()\nmagnifier.show(view.width / 2.0f, view.height / 2.0f)\n```\n\n### Java\n\n```java\nView view = findViewById(R.id.view);\nMagnifier magnifier = new Magnifier.Builder(view).build();\nmagnifier.show(view.getWidth() / 2, view.getHeight() / 2);\n```\n\nAssuming the view hierarchy has the first layout, the magnifier displays on the\nscreen and contains a region centered on the given coordinates within the view.\nThe pane appears above the center point of the content being copied. The\nmagnifier persists indefinitely until the user dismisses it.\n| **Note:** The arguments of [`show()`](/reference/android/widget/Magnifier#show(float,%20float)) are relative to the top-left corner of the view being magnified.\n\nThe following code snippet shows how to change the background of the magnified\nview: \n\n### Kotlin\n\n```kotlin\nview.setBackgroundColor(...)\n```\n\n### Java\n\n```java\nview.setBackgroundColor(...);\n```\n\nAssuming the background color is visible within the magnifier, the magnifier's\ncontent is stale, as a region of the view with the old background still\ndisplays. To refresh the content, use the\n[`update()`](/reference/android/widget/Magnifier#update()) method, as follows: \n\n### Kotlin\n\n```kotlin\nview.post { magnifier.update() }\n```\n\n### Java\n\n```java\nview.post(magnifier::update);\n```\n| **Note:** Post the update operation to make sure that by the time this executes, the view with the new background color is already drawn. This is because the magnifier content always stays a frame behind the magnified view.\n\nWhen finished, close the magnifier by calling the\n[`dismiss()`](/reference/android/widget/Magnifier#dismiss()) method: \n\n### Kotlin\n\n```kotlin\nmagnifier.dismiss()\n```\n\n### Java\n\n```java\nmagnifier.dismiss();\n```\n\nMagnify on user interaction\n---------------------------\n\nA common use case for the magnifier is to let the user enlarge a view region by\ntouching it, as shown in figure 2.\n**Figure 2.** The magnifier follows the user's touch. It is applied to a `ViewGroup` that contains an \\`ImageView\\` to the left and a `TextView` to the right.\n\nYou can do this by updating the magnifier according to the touch events received\nby the view, as follows: \n\n### Kotlin\n\n```kotlin\nimageView.setOnTouchListener { v, event -\u003e\n when (event.actionMasked) {\n MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -\u003e {\n val viewPosition = IntArray(2)\n v.getLocationOnScreen(viewPosition)\n magnifier.show(event.rawX - viewPosition[0], event.rawY - viewPosition[1])\n }\n MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -\u003e {\n magnifier.dismiss()\n }\n }\n true\n}\n```\n\n### Java\n\n```java\nimageView.setOnTouchListener(new View.OnTouchListener() {\n @Override\n public boolean onTouch(View v, MotionEvent event) {\n switch (event.getActionMasked()) {\n case MotionEvent.ACTION_DOWN:\n // Fall through.\n case MotionEvent.ACTION_MOVE: {\n final int[] viewPosition = new int[2];\n v.getLocationOnScreen(viewPosition);\n magnifier.show(event.getRawX() - viewPosition[0],\n event.getRawY() - viewPosition[1]);\n break;\n }\n case MotionEvent.ACTION_CANCEL:\n // Fall through.\n case MotionEvent.ACTION_UP: {\n magnifier.dismiss();\n }\n }\n return true;\n }\n});\n```\n| **Note:** The magnifier never displays magnified content that doesn't belong to the view, even when the view is included in scrollable containers and partially masked. When the coordinates passed to `show()` imply copying outside content, they are coerced inside the visible region of the view.\n\nAdditional considerations when magnifying text\n----------------------------------------------\n\nFor the platform text widgets, it's important to understand specific magnifier\nbehaviors and to enable the magnifier for your custom text view consistently\nacross the Android platform. Consider the following:\n\n- The magnifier is triggered immediately when the user grabs an insertion or selection handle.\n- The magnifier always smoothly follows the user's finger horizontally, while vertically it is fixed to the center of the current text line.\n- When moving horizontally, the magnifier moves only between the left and right bounds of the current line. Moreover, when the user's touch leaves these bounds and the horizontal distance between the touch and the closest bound is larger than half of the original width of the magnifier content, the magnifier is dismissed, as the cursor is no longer visible inside the magnifier.\n- The magnifier is never triggered when the text font is too large. Text is considered too large when the difference between the font's descent and ascent is larger than the height of the content that fits in the magnifier. Triggering the magnifier in this case doesn't add value."]]