【问题标题】:How to animate an ImageView so that once it reaches the upper end of the screen it starts showing from the bottom end of the screen?如何为 ImageView 设置动画,以便一旦它到达屏幕的上端,它就会从屏幕的底端开始显示?
【发布时间】:2021-04-22 12:57:47
【问题描述】:

这是我可以实现的目标:

package game.card.translationproblem;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView ball;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);


        ball = findViewById(R.id.coloured_ball);
        ball.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.move_the_ball);
                set.setTarget(ball);
                set.start();
            }
        });

    }

}

这里是动画文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <objectAnimator
        android:propertyName="translationY"
        android:valueFrom="0dp"
        android:valueTo="-500dp"
        android:duration="5000"/>

    <objectAnimator
        android:propertyName="translationY"
        android:valueFrom="320dp"
        android:valueTo="0dp"
        android:duration="5000"
        android:startOffset="5000"/>
</set>

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="700dp"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/coloured_ball"
            android:layout_width="138dp"
            android:layout_height="141dp"
            android:gravity="center"
            app:srcCompat="@drawable/ball" />
    </LinearLayout>


    </LinearLayout>

我想沿屏幕执行平移,以便超出屏幕上边缘的图像的任何部分立即从屏幕底部显示出来。 我想使用 2 个相同的图像,并同时从底部显示一个,而另一个从上边缘消失,但对这个解决方案的主要反对意见是我不能保证屏幕的大小,我可能只是在显示两张完整的图像。

【问题讨论】:

  • 这个问题清楚还是...
  • 如果我必须完成这个问题,我会和你一样思考,通过两张图片。如果需要,Android 可以巧妙地以有限的尺寸显示图像——例如使用wrap_contentmatch_parent 的组合的屏幕高度,因此您可以控制想要的图像大小。我想说你想弄清楚如何找出顶部图像超出屏幕顶部的距离,以便你可以将相同的反向翻译应用于底部图像。
  • 是的@Chris 我想你理解我的问题。我希望有一些我不知道但似乎没有的快速解决方案。

标签: android xml animation translation


【解决方案1】:

可以通过屏幕大小创建动画:

void moveBall(){
    // getScreenHeight
    Display display = getDisplay();
    Point outSize = new Point();
    display.getSize(outSize);
    int height = outSize.y;
    //create animator via height
    Animation animator = new TranslateAnimation(0F, 0F, 0F, -(height / 2 + ball.getMeasuredHeight()));
    animator.setDuration(2000);
    animator.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Animation animator = new TranslateAnimation(0F, 0F, +(height / 2 + ball.getMeasuredHeight()), 0F);
            animator.setDuration(2000);
            ball.startAnimation(animator);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    ball.startAnimation(animator);
}

【讨论】:

  • 谢谢安娜的努力,我刚刚看到你回答了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2017-01-28
  • 2018-05-20
  • 2022-12-14
  • 1970-01-01
相关资源
最近更新 更多