【问题标题】:How do make Android confetti by picture drop from top animation?如何从顶部动画中通过图片制作 Android 五彩纸屑?
【发布时间】:2019-06-25 13:57:31
【问题描述】:

我想制作一个点击按钮的动画动画如下:

但我不知道如何让它在触及底部读取线时消失。

我想上传一个gif文件,但是stackoverflow目前似乎不支持gif。

MainActivity.kt的代码:

class MainActivity : AppCompatActivity() {

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

        val btn: Button = findViewById(R.id.button)

        btn.setOnClickListener {
            val animation = AnimationUtils.loadAnimation(this@MainActivity, R.anim.drop_down)
            val confetti: ImageView = findViewById(R.id.place_holder_animation)
            val resId = R.drawable.confetti

            animation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationRepeat(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {
                    confetti.visibility = View.GONE
                }

                override fun onAnimationStart(animation: Animation?) {
                }
            })

            confetti.setImageDrawable(resources.getDrawable(resId))
            confetti.startAnimation(animation)
        }
    }
}

drop_down.xml为动画说明:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     >
    <translate android:fromYDelta="-80%p" android:toYDelta="10%p" android:duration="2000"/>
</set>

布局文件只是一个RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
            android:id="@+id/place_holder_animation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/jbean"
            android:id="@+id/imageView2"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/jbean"
            android:id="@+id/imageView3"
            android:layout_toEndOf="@+id/imageView2"
            android:layout_marginStart="20dp"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/jbean"
            android:layout_toEndOf="@+id/imageView3"
            android:id="@+id/imageView4"
            android:layout_marginStart="20dp"/>
    <Button
            android:id="@+id/button"
            android:text="Show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:layout_below="@+id/imageView3"
            android:layout_alignStart="@+id/imageView3"/>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout kotlin android-animation


    【解决方案1】:

    使用此动画消失 -

    <?xml version="1.0" encoding="utf-8"?>
    <set 
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator">
       <alpha
            android:duration="2000"
            android:fromAlpha="1.0"
            android:toAlpha="0.0" >
       </alpha>
     </set>
    

    【讨论】:

    • 谢谢,我更新了我的代码,添加了setAnimationListeneronAnimationEnd 以使五彩纸屑消失。
    • 不错。快乐编码!
    • 是的,我还在研究如何让五彩纸屑只从果冻豆的区域滴落。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2021-07-17
    • 2021-11-22
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多