简体   繁体   English

如何正确设置路径值来描述 Android 中的弧形动画

[英]How to properly set path values to describe an arc animation in Android

I have one view "whiteCircle" inside a button which I want to move from it's initial position to the white box "imageSquared" describing an arc and then get back to its initial position.我在一个按钮内有一个视图“whiteCircle”,我想从它的初始位置移动到描述弧的白框“imageSquared”,然后返回到它的初始位置。

What I have tried is:我尝试过的是:

private fun startArcAnimation() {

    val path = Path()

    val location = IntArray(2)
    imageSquared.getLocationOnScreen(location)

    path.arcTo(
        0f,
        0f,
        location[0].toFloat() + imageSquared.width,
        location[0].toFloat() + imageSquared.height,
        180f,
        180f,
        true
    )

    val animator = ObjectAnimator.ofFloat(whiteCircle, View.X, View.Y, path)
    animator.duration = 1000
    animator.start()

}

And this is the result:这是结果:

在此处输入图片说明

Can you help me setting path values correct?你能帮我设置正确的路径值吗? I have been struggling with arcTo properties without success.我一直在努力使用 arcTo 属性,但没有成功。

Thanks in advance.提前致谢。

Here is my implementation:这是我的实现:

gif

arcTo method create oval which is placed into rect. arcTo方法创建放置到矩形中的椭圆。 So first of all you need to create correct rect.所以首先你需要创建正确的矩形。 Your path should start from 180 degree in oval and move 180 degrees clockwise (Zero angle of oval is in right side).您的路径应该从椭圆形的 180 度开始并顺时针移动 180 度(椭圆形的零角在右侧)。

方案

I suggest to animate translationX and translationY properties of view.我建议为视图的translationXtranslationY属性设置动画。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        greenButton.setOnClickListener {
            startArcAnimation()
        }
    }

    private fun startArcAnimation() {
        if (red.translationX != 0f) {
            //return to start position animation
            red.animate().translationX(0f).translationY(0f).start()
            return
        }

        val rectHeight = 600
        val left = 0f
        val top = -rectHeight / 2
        val right = white.x - red.x
        val bottom = white.y + rectHeight / 2 - red.y

        val path = Path()
        path.arcTo(left, top.toFloat(), right, bottom, 180f, 180f, true)

        val animator = ObjectAnimator.ofFloat(red, View.TRANSLATION_X, View.TRANSLATION_Y, path)
        animator.duration = 1000
        animator.start()
    }
}

Here is activity_main.xml这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:padding="16dp">

    <Button
        android:id="@+id/greenButton"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="#0a0"
        android:stateListAnimator="@null" />

    <ImageView
        android:id="@+id/white"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical|right"
        android:background="#fff" />

    <ImageView
        android:id="@+id/red"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="70dp"
        android:layout_marginBottom="10dp"
        android:background="#f00" />

</FrameLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中实现弧形菜单动画 - How to implement arc menu animation in android 如何为IntellJ IDE社区版正确设置android SDK路径? - How to set the android SDK path properly for IntellJ IDE community edition? 如何在Android中正确重用Animation? - How to properly reuse Animation in Android? [Android]我必须将图像从一个 position 移动到另一个弧形路径 animation - [Android]I have to move an image from one position to another with arc path animation 如何在Android中使用Canvas Paint在弧顶上绘制线路径 - How to draw line path on top of arc using Canvas paint in Android 如何在Android中绘制在特定路径(Arc)中移动的位图? - How to draw a bitmap that moves in specific path(Arc) in android? 如何在Android的微调器中正确设置列表或适配器的值? - How to set properly the values of a list or adapter in a spinner in Android? 如何使用密封类来描述具有关联值的有限案例集以及较小的此类值集? - How can I use sealed classes to describe a finite set of cases with associated values, and a smaller set of such values? 如何在Android中创建弧 - How to create a Arc in Android 如何在android中制作圆弧 - How to make arc in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM