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;}});
[[["容易理解","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 (世界標準時間)。"],[],[],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."]]