【问题标题】:Moving an ImageView in the screen在屏幕中移动 ImageView
【发布时间】:2020-05-05 04:10:39
【问题描述】:

我正在尝试根据按下的按钮移动 ImageView。目前我正在尝试向上和向下移动 ImageView,向下的东西工作得很好,但问题是当我试图让它上升时。当 ImageView 不在其初始位置时,ImageView 会完美上升(直到它到达其初始位置)但是当它处于其初始位置时它只会上升一次,然后当我再次按下向上按钮时它会下降到其初始位置。

(只是让你知道,屏幕的方向是横向的)

所以,要向上移动我的 ImageView,我只需降低 translationY 的值 imageView.translationY = imageView.translationY.absoluteValue - 25

我尝试使用ObjectAnimator.ofFloat(),但 imageView 在动画结束后又回到了它的初始位置。

在布局中我只是使用了一个约束布局,我将 ImageView 约束在屏幕的中心。

感谢您的帮助!

【问题讨论】:

    标签: android xml android-studio kotlin imageview


    【解决方案1】:

    您好,我认为约束布局将适合这种情况。我创建了一个示例项目来检查这一点。这使用 MotionLayoutMotionScene

    想法是创建 MotionLayout 作为您的父布局,然后定义子级以在您的情况下对其 ImageView 进行动画处理,这是示例布局文件。在某些活动或某些片段中使用此文件以查看实际情况

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_dark"
        android:orientation="vertical"
        app:layoutDescription="@xml/up_down_descriptor">
    
        <ImageView
            android:id="@+id/ivStar"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_star_outline" />
    
        <Button
            android:id="@+id/btnUp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="200dp"
            android:text="up"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/ivStar"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btnDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="200dp"
            android:text="Down"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/ivStar"
            app:layout_constraintTop_toTopOf="parent" />
    
    
    </androidx.constraintlayout.motion.widget.MotionLayout>
    

    你在res/xml

    中定义了一个布局描述文件up_down_descriptor
    <?xml version="1.0" encoding="utf-8"?>
    <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <Transition
            app:constraintSetEnd="@+id/down"
            app:constraintSetStart="@+id/center"
            app:duration="2000">
    
            <OnClick
                app:clickAction="toggle"
                app:targetId="@id/btnDown" />
    
        </Transition>
    
        <Transition
            app:constraintSetEnd="@+id/up"
            app:constraintSetStart="@+id/center"
            app:duration="2000">
    
            <OnClick
                app:clickAction="toggle"
                app:targetId="@id/btnUp" />
    
        </Transition>
    
        <ConstraintSet android:id="@+id/center">
            <Constraint
                android:id="@+id/ivStar"
                android:layout_width="40dp"
                android:layout_height="40dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </ConstraintSet>
    
        <ConstraintSet android:id="@+id/down">
    
            <Constraint
                android:id="@+id/ivStar"
                android:layout_width="40dp"
                android:layout_height="40dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
    
        </ConstraintSet>
    
        <ConstraintSet android:id="@+id/up">
    
            <Constraint
                android:id="@+id/ivStar"
                android:layout_width="40dp"
                android:layout_height="40dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </ConstraintSet>
    
    </MotionScene>
    

    MotionLayout 会处理剩下的事情。

    【讨论】:

    • 感谢您的回答。我尝试了您的建议,但事实是我不希望 imageView 直接从屏幕中心到边缘,但它必须在到达边缘之前执行更多步骤(例如移动一定数量的像素每次按下按钮)。
    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多