I want this image to appear with Zoom In Fade In animation, but when I set it to my ImageView it shows at full size and alpha first, then it appears with animation.
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="200"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0" />
<alpha
android:duration="200"
android:fromAlpha="0"
android:toAlpha="1" />
</set>
val zoomInFadeInAnim = AnimationUtils.loadAnimation(this, R.anim.zoom_in_fade_in)
imageView.setImageResource(R.drawable.ic_cross)
imageView.startAnimation(zoomInFadeInAnim)
Please help!!!!!!!!!!
CodePudding user response:
Use this code:
imageView.setAlpha(0);
imageView.setScaleX(0);
imageView.setScaleY(0);
imageView.setImageResource(R.drawable.ic_cross)
imageView.animate().setDuration(200)
.alphaBy(0).alpha(1)
.scaleX(0).scaleY(0)
.scaleXBy(1).scaleY(1)
.start();
CodePudding user response:
You can easily do it this way in the Kotlin programming language.
setAlpha()
deprecated. You should use alpha
.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/apply.html
If you are using Kotlin, I recommend using apply
. It increases the readability of the code and saves code duplication.
class YourActivity : AppCompatActivity() {
private lateinit var imageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.yourActivityId)
imageView = findViewById(R.id.yourImageViewId)
imageView.apply {
alpha = 0f
scaleX = 0f
scaleY = 0f
setImageResource(R.drawable.ic_cross)
animate().setDuration(200)
.alphaBy(0f).alpha(1f)
.scaleX(0f).scaleY(0f)
.scaleXBy(1f).scaleY(1f)
.start()
}
}
}