我如何在 android 中从左到右应用淡出动画

How do i apply fade out animation from left to right in android

我正在尝试制作 View 的淡出动画,但我不想要标准动画。我希望淡出从左到右(或从右到左)开始。关于如何做的任何想法?我真的没有在 SO 上找到 android 的任何内容。这是我目前的代码:

val fadeOut = AlphaAnimation(1f, 0f)
            fadeOut.interpolator = AccelerateInterpolator()
            fadeOut.duration = 3000L

            fadeOut.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {

                }

                override fun onAnimationEnd(animation: Animation) {
                   mTextView.visibility = View.VISIBLE
                }

                override fun onAnimationRepeat(animation: Animation) {}
            })

            mTextView.startAnimation(fadeOut)

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
    <translate
        android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="500" />
</set>

据我所知,您似乎想要显示动画而不是淡入淡出 in/out 动画。最好的办法是编写自己的显示 Animation,但如果您准备好接受更简单的 hack,则可以使用 ViewAnimationUtils 中提供的圆形显示动画。只需从远离 left/right 的地方开始圆形显示,这样圆形显示感觉就像线性显示。

final View myView = findViewById(R.id.animatedImageView);

int cx = myView.getMeasuredWidth() * 2;
int cy = myView.getMeasuredHeight() / 2;
int finalRadius = (int) Math.hypot(myView.getWidth()*2, myView.getHeight());

Animator anim;
// for reveal
anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
// for hiding
// anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, finalRadius, 0);

// Do note that you'll need to factor in the 'reveal' time taken in by the extra empty space.
// You could probably get around this by creating your own interpolator, 
// but in which case, you might as well just create your own reveal animation.
anim.setDuration(3000);

// make the view visible and start the animation
myView.setVisibility(View.VISIBLE);
anim.start();