簡體   English   中英

立即將“ImageView”移動到 Kotlin 中的另一個 position(無動畫)?

[英]Move “ImageView” instantly to another position (Without Animation) in Kotlin?

我已經用谷歌搜索了幾個小時,以盡可能最有效的方式做到這一點(所有設備都支持)但沒有用:

我有一個包含在 ConstraintLayout 中的 ImageView,因此它沒有絕對坐標,它會根據設備屏幕的寬度移動。

我想做的是將它移動到一個新的 position 與 dp 單位不是像素。 (所以我確保它在所有設備上都在視覺上兼容)

例如,立即將 ImageView 向左移動 10 dp。

我不需要 animation,我只需要即時翻譯。 我找到的所有答案都包括 animation 但我只是希望它盡可能快,其他答案給出了像素方面的解決方案。

我正在使用 Kotlin。 而且我目前沒有可以正確執行任何操作的代碼。

我相信您需要手動更新ConstraintLayout 我不知道你的觀點到底如何,所以我只是創建一個例子。

在 XML 中,我們有圖像視圖的初始 position(想象它的父級是一個約束布局):

 android:id="@+id/image_view"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_marginRight="20dp"

當涉及到移動圖像視圖時,您將手動更新約束,在這種情況下,右邊距(將其向左移動):

val constraintLayout: ConstraintLayout = findViewById(R.id.constraint_layout)
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayout)
constraintSet.clear(R.id.image_view, ConstraintSet.RIGHT);
constraintSet.connect(R.id.image_view, ConstraintSet.RIGHT, R.id.constraint_layout, ConstraintSet.RIGHT, 40)
constraintSet.applyTo(constraintLayout)

更新約束似乎有點冗長,但不幸的是它們就是這樣工作的(您制作一個副本或創建一組全新的約束並將其應用於視圖)。

在此示例中,您將右邊距從 20dp 更改為 40dp,立即將圖像視圖向右移動 20dp,而沒有 animation。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM