[英]How do make Android confetti by picture drop from top animation?
I want to make an animation animation on clicking the button as follows: 我要在单击按钮时制作一个动画,如下所示:
But I don't know how to make it disappear when it touch the bottom read line. 但是我不知道当它触及底部读取线时如何使其消失。
I want to upload a gif file, but it seems that stackoverflow doesn't support gif at this moment. 我想上传一个gif文件,但目前看来stackoverflow不支持gif。
The code of MainActivity.kt
: 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)
}
}
}
With the drop_down.xml
for the animation description: 使用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>
The layout file is just a RelativeLayout: 布局文件只是一个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>
Use this animation for disappearing - 使用此动画消失-
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.