Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

android - Move "ImageView" instantly to another position (Without Animation) in Kotlin?

I've been googling for hours to do this the most efficient way possible (To be supported on all devices) but with no use:

I have an ImageView contained within a ConstraintLayout, so it has no absolute coordinates, it moves depending on the width of the device's screen.

What I wanna do is MOVE it to a new position with dp units not pixels. (So I make sure it's visually compatible across all devices)

For example, moving ImageView 10 dp to the left INSTANTLY.

I don't need animation, I just need instant translation. All the answers I've found include animation but I just want it to be as fast as instantaneous can be, other answers give the solution in terms of pixels.

I am using Kotlin. And I have no code for the moment that does anything properly.

question from:https://stackoverflow.com/questions/65887848/move-imageview-instantly-to-another-position-without-animation-in-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe you'll need to manually update the ConstraintLayout. I don't know exactly how you have your views, so i'll just create an example.

In the XML, we have the initial position of the image view (imagine it's parent is a constraint layout):

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

When then it comes to moving the image view, you would manually update the constraints, in this case, the right margin (to move it left):

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)

Updating the constraints seems a bit long winded but unfortunately that's just how they work (you make a copy or create a whole new set of constraints and apply it to the view).

In this example, you'd changing the right margin from 20dp to 40dp, shifting the image view right by 20dp instantly with no animation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...