xragdollqueen
xragdollqueen

Reputation: 113

How to make a custom animator work in Kotlin (animating the alpha property)

I want to animate the ALPHA property on an ImageView by means of an AnimatorSet (I actually have two animations, but I'm using the alpha one only for now in order to check if it works) I'm using Kotlin (and I'm new to it) I have a layout with the ImageView inside, an xml with the AnimatorSet and a Kotlin class related to the layout.

Here are some options I've already tried, all of them in the onCreate method and making use of the Kotlin extensions:

mLogo.post {
        slideUpAndFadeIn = AnimatorInflater.loadAnimator(this, 
R.animator.slide_up_and_fade_in) as AnimatorSet
        slideUpAndFadeIn.duration = 250
        slideUpAndFadeIn.setTarget(mLogo)
        slideUpAndFadeIn.start()
}

mLogo.animate().apply {
        interpolator = LinearInterpolator()
        duration = 500
        alpha(1f)
        startDelay = 1000
        start()
    }

val objectAnimator = ObjectAnimator.ofFloat(mLogo, View.ALPHA, 0.0f, 1.0f)
    objectAnimator.duration = 250
    objectAnimator.interpolator = AccelerateDecelerateInterpolator()
    objectAnimator.start()

This is the code as I have it now:

MainActivity.kt

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var slideUpAndFadeIn : AnimatorSet

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

    slideUpAndFadeIn = AnimatorInflater.loadAnimator(this, 
    R.animator.slide_up_and_fade_in) as AnimatorSet
    slideUpAndFadeIn.duration = 250
    slideUpAndFadeIn.setTarget(mLogo)
    slideUpAndFadeIn.start()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center">

    <ImageView
        android:id="@+id/mLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/main_logo"
        android:layout_margin="@dimen/dimen_margin_main_logo"
        android:visibility="invisible"/>

</LinearLayout>

slide_up_and_fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
    <objectAnimator
        android:propertyName="translationY"
        android:valueFrom="1"
        android:valueTo="50"
        android:valueType="intType"
        android:interpolator="@android:interpolator/fast_out_linear_in"/>

<objectAnimator
        android:propertyName="alpha"
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:valueType="floatType"/>
</set>

The app doesn't crash, so there's no NullPointerException, but the animation doesn't work. It never "fades in". Any help is appreciated :)

Upvotes: 1

Views: 1429

Answers (1)

underoid
underoid

Reputation: 439

'visibility' and 'alpha' are different attributes. As you are animating alpha you should use android:alpha="0" in your xml layout instead of android:visibility="invisible"

Upvotes: 2

Related Questions