动画图像从底部向顶部开始

5
我知道我的问题可能会被嘲笑,但我想给我的添加动画效果。例如,当我点击按钮时,它可以从底部出现并向上移动,就像这张图片一样。

请看我的回答,我已经添加了可行的代码。希望这能有所帮助。 - Ferdous Ahamed
1个回答

7

1.创建move.xml文件以定义animation(动画)

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0%p"
        android:duration="1000" />
</set>

2. 创建 activity_animation.xml 用于显示 ButtonImageView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_animation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_centerHorizontal="true"
        android:visibility="gone"/>

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

3. 你的AnimationActivity应该像这样:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class AnimationActivity extends AppCompatActivity implements Animation.AnimationListener {

    ImageView imageIcon;
    Button btnStart;

    // Animation
    Animation animMoveToTop;

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

        imageIcon = (ImageView) findViewById(R.id.icon);
        btnStart = (Button) findViewById(R.id.btnStart);

        // load the animation
        animMoveToTop = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);

        // set animation listener
        animMoveToTop.setAnimationListener(this);

        // button click event
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                imageIcon.setVisibility(View.VISIBLE);

                // start the animation
                imageIcon.startAnimation(animMoveToTop);
            }
        });
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // Take any action after completing the animation
        // check for move animation
        if (animation == animMoveToTop) {
            Toast.makeText(getApplicationContext(), "Animation Stopped", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
    }
}

输出:

输入图像描述

希望这能有所帮助~


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接