动画

一、帧动画

在res/drawable/文件夹下面添加animation_list.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >
    <item
        android:drawable="@drawable/xyy1"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/xyy2"
        android:duration="100">
    </item>
</animation-list>

在res/drawable/文件夹下面添加lihua_animation.xml

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

    <item
        android:drawable="@drawable/bomb1"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb2"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb3"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb4"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb5"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb6"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb7"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb8"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb9"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb10"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb11"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb12"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb13"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb14"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb15"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb16"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb17"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb18"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb19"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb20"
        android:duration="100">
    </item>
    <item
        android:drawable="@drawable/bomb21"
        android:duration="100">
    </item>

</animation-list>
public class MainActivity extends Activity implements OnClickListener {

    private Button button1, button2, button3, button4;
    private ImageView imageView, imageView2;
    private AnimationDrawable anDrawable, lihuAnimation;
    private AnimationDrawable animationDrawable;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);

        imageView = (ImageView) findViewById(R.id.imageView1);
        imageView2 = (ImageView) findViewById(R.id.imageView2);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);

    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            imageView.setBackgroundResource(R.drawable.animation_list);
            anDrawable = (AnimationDrawable) imageView.getBackground();
            anDrawable.start();
            break;

        case R.id.button2:
            anDrawable.stop();
            break;
        case R.id.button3:

            // imageView2.setBackgroundResource(R.drawable.lihua_animation);
            // lihuAnimation = (AnimationDrawable) imageView2.getBackground();
            // lihuAnimation.start();

            animationDrawable = new AnimationDrawable();
            // 在代码中获取帧资源(图片资源)
            for (int i = 1; i < 22; i++) {

                 //getIdentifier(资源的名字, 资源的位置, 包名)
                int id = getResources().getIdentifier("bomb" + i, "drawable", "com.guo.zhenanimation");
                //通过id找到每一张图片,将其转换为drawable对象
                Drawable drawable = getResources().getDrawable(id);
                //将每张图片添加到animationDrawable对象中(动画中)
                animationDrawable.addFrame(drawable, 100);
            }
            imageView2.setBackground(animationDrawable);
            animationDrawable.setOneShot(false);
            animationDrawable.start();

            break;
        case R.id.button4:
            //lihuAnimation.stop();
            animationDrawable.stop();
            break;

        }
    }

}

这里写图片描述

二、补间动画
使用方式一的时候需要在res/anim/文件夹下面新建bujian_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromAlpha="1.0"
    android:repeatCount="5"
    android:repeatMode="reverse"
    android:toAlpha="0.0" >

</alpha>
public class MainActivity extends Activity implements OnClickListener {

    private Button button;
    private ImageView imageView;
    private Animation anniAnimation;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        imageView = (ImageView) findViewById(R.id.imageView1);

        button.setOnClickListener(this);
        imageView.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            //方式一:
            // 通过将图片的资源设置转化成animation对象
            // anniAnimation = AnimationUtils.loadAnimation(this,
            // R.anim.bujian_animation);
            // // 将imageview控件的特效设置成定义好的
            // imageView.setAnimation(anniAnimation);
            // imageView.startAnimation(anniAnimation);
            // //button.setAnimation(anniAnimation);
            // button.startAnimation(anniAnimation);

            //方式二:通过代码设置图片的效果
            AlphaAnimation alphanimation = new AlphaAnimation(1.0f, 0.0f);
            alphanimation.setDuration(3000);
            imageView.setAnimation(alphanimation);
            imageView.startAnimation(alphanimation);

            //给动画设置监听
            alphanimation.setAnimationListener(new AnimationListener() {
                //动画开始可以执行的操作
                public void onAnimationStart(Animation animation) {

                }

                public void onAnimationRepeat(Animation animation) {

                }
                //动画结束可以执行的操作
                public void onAnimationEnd(Animation animation) {
                    //button.startAnimation(animation);
                }
            });
            break;
        }
    }
}

这里写图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值