【问题标题】:Up and Down animation on Android viewAndroid 视图上的上下动画
【发布时间】:2016-04-22 12:26:49
【问题描述】:

我正在制作一个启动画面,我希望图像视图像悬浮一样不断上升然后下降。这将在数​​据库在后台加载(AsyncTask)时发生。我试过动画视图,但它只在一个方向上,而且只有一次。我该如何做到这一点?提前谢谢你:D

【问题讨论】:

  • 我编辑了答案并稍微更改了代码
  • 我所要做的就是改变时间,但它可以按我的意愿工作。非常感谢! :D

标签: android animation android-animation


【解决方案1】:

我要做的就是像你以前那样为视图设置动画,在 onAnimationEnd 中使用一种无​​限循环:

 //in onPreExecute do levitate(ivSplashLogo, 300, true)
 //in onPostExecute do levitate(ivSplashLogo, 300, false)

 public void levitate (final View movableView,final float Y,boolean animated){
   if(animated) {
        final long yourDuration = 200;
        final TimeInterpolator yourInterpolator = new DecelerateInterpolator();
        movableView.animate().
                translationYBy(Y).
                setDuration(yourDuration).
                setInterpolator(yourInterpolator).
                setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        levitate(movableView, -Y, true);
                    }
                });
    }
 }

我还没有尝试过,但你可以试一试告诉我

【讨论】:

  • 我不知道 Interpolator 的用途以及它与 Duration 有何不同。
  • 插值器是一条贝塞尔曲线,它将对动画应用不同的效果。 Here 是一个视频,它将向您展示各种插值器的结果。如果你愿意,这里是documentation
  • 我明白了。谢谢!我从 OnProgressUpdate 调用它,但它没有移动:
【解决方案2】:

从下到上:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="100%p"
    android:toYDelta="0%p" />

从上到下:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="100%p" />

代码:

if (findViewById(R.id.llIncludeBottom).getVisibility() == View.VISIBLE) {
                    findViewById(R.id.llIncludeBottom).setVisibility(View.GONE);
                    findViewById(R.id.llIncludeBottom).setAnimation(
                            AnimationUtils.loadAnimation(this, R.anim.top_bottom));
                } else {
                    findViewById(R.id.llIncludeBottom).setVisibility(View.VISIBLE);
                    findViewById(R.id.llIncludeBottom).setAnimation(
                            AnimationUtils.loadAnimation(this, R.anim.bottom_top));
                }

【讨论】:

    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多