Android开发之Animations(六)

AnimationListener

什么是AnimationListener?

1.AnimationListener是一个监听器;

2.该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;

3.主要包含以下三个方法:

onAnimationEnd(Animation animation);

onAnimationRepeat(Animation animation);

onAnimationStart(Animation animation);


AnimationListener的使用:

1.为主布局文件为各个控件添加id

2.在MainActivity.java中创建各个控件对象,包括ViewGroup对象等

3.在按钮监听器的onClick()方法中创建淡出(或淡入)对象,然后设置属性,最后为Animation对象绑定AnimatinListener

4.为Animation新建监听器类继承AnimationListener,并重写上述三个方法,在方法内部添加自己的代码


activity_main.java:

<?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/viewGroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.animationlistener.MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:src="@drawable/bigdog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/addButton"
        android:text="添加图片"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:id="@+id/removeButton"
        android:text="移除图片"
        android:layout_above="@id/addButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java:
package com.mycompany.animationlistener;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private Button removeButton;
    private Button addButton;
    private ViewGroup viewGroup;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        removeButton = (Button) findViewById(R.id.removeButton);
        addButton = (Button) findViewById(R.id.addButton);
        viewGroup = (ViewGroup) findViewById(R.id.viewGroup);
        imageView = (ImageView) findViewById(R.id.imageView);

        removeButton.setOnClickListener(new RemoveButtonListener());
        addButton.setOnClickListener(new AddButtonListener());
    }

    class RemoveButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            //  创建一个淡出效果的Animation对象
            AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
            //  设置Animation对象的属性
            animation.setDuration(2000);
            //  为Animation对象绑定AnimationListener
            animation.setAnimationListener(new RemoveAnimationListener());
            //  启动动画
            imageView.startAnimation(animation);
        }
    }

    class RemoveAnimationListener implements Animation.AnimationListener{
        //  在动画开始时调用
        @Override
        public void onAnimationStart(Animation animation) {
            System.out.println("AnimationStart");
        }
        //  在动画结束时调用
        @Override
        public void onAnimationEnd(Animation animation) {
            System.out.println("AnimationEnd");
            viewGroup.removeView(imageView);
        }
        //  在动画重复时调用
        @Override
        public void onAnimationRepeat(Animation animation) {
            System.out.println("AnimationRepeat");
        }
    }

    class AddButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            //  创建一个淡入的Animation对象
            AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
            animation.setDuration(2000);
            //  创建一个ImageView对象用来显示图片
            ImageView newImageView = new ImageView(MainActivity.this);
            newImageView.setImageResource(R.drawable.bigdog);
            //  将新建的ImageView对象添加至ViewGroup对象中
            viewGroup.addView(newImageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            //  启动动画
            newImageView.startAnimation(animation);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值