栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android动画

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android动画

一、android中的动画 动画分类: 1、帧动画 2、补间动画 3、属性动画 二、帧动画

类似于电影,一张张图片连续播放

1、xml方式

anim.xml



    
    
    
    
    
    
    
    
    
    
    
    
 
2、代码方式

activity_main.xml



    

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image_view);
        imageView.setImageResource(R.drawable.anim);
        findViewById(R.id.start).setOnClickListener(v -> {
            AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getDrawable();
            if (animationDrawable != null && !animationDrawable.isRunning()) {
                animationDrawable.start();
            }
        });

        findViewById(R.id.stop).setOnClickListener(v -> {
            AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
            if (animationDrawable != null && animationDrawable.isRunning()) {
                animationDrawable.stop();
            }
        });

        findViewById(R.id.java_code).setOnClickListener(v -> {
            frameAnimation();
        });
    }

    private void frameAnimation() {
        AnimationDrawable animationDrawable = new AnimationDrawable();
        animationDrawable.addframe(getDrawable(R.drawable.anim1), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim2), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim3), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim4), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim5), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim6), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim7), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim8), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim9), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim10), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim11), 100);
        animationDrawable.addframe(getDrawable(R.drawable.anim12), 100);
        animationDrawable.setOneShot(true);
        imageView.setImageDrawable(animationDrawable);
        animationDrawable.start();
    }
}
三、补间动画


其中AnimationSet是其余几种以及其他AnimationSet的组合
基本属性:


其中,
Duration:持续时间,单位是毫秒
Interpolator:插值器
插值器列表

Alpha属性

Rorate属性

Scale属性

Translate 属性

AnimationSet

    
    
    
    
    
        ...
    
示例

alpha.xml



rorate.xml



scale.xml



translate.xml



activity_main.xml




    

    

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image_view);

        findViewById(R.id.alpha).setOnClickListener(v -> {
            alpha();
        });
        findViewById(R.id.scale).setOnClickListener(v -> {
            scale();
        });
        findViewById(R.id.translate).setOnClickListener(v -> {
            translate();
        });
        findViewById(R.id.rotate).setOnClickListener(v -> {
            rotate();
        });
        findViewById(R.id.animation_set).setOnClickListener(v -> {
            animation_set();
        });
        findViewById(R.id.java_code).setOnClickListener(v -> {
            java_code();
        });
        findViewById(R.id.serial_play).setOnClickListener(v -> {
            serial_play();
        });
    }

    private void alpha() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
        imageView.startAnimation(animation);
    }

    private void scale() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
        imageView.startAnimation(animation);
    }

    private void translate() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
        imageView.startAnimation(animation);
    }

    private void rotate() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        imageView.startAnimation(animation);
    }

    private void animation_set() {
        AnimationSet animationSet = new AnimationSet(true);
        Animation alpha = AnimationUtils.loadAnimation(this, R.anim.alpha);
        Animation scale = AnimationUtils.loadAnimation(this, R.anim.scale);
        Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
        Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);

        animationSet.addAnimation(alpha);
        animationSet.addAnimation(scale);
        animationSet.addAnimation(translate);
        animationSet.addAnimation(rotate);
        imageView.startAnimation(animationSet);
    }

    private void java_code() {
//        AlphaAnimation animation = new AlphaAnimation(0, 1);
//        ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2);
//        TranslateAnimation animation = new TranslateAnimation(100, 100, 200, 300);
        RotateAnimation animation = new RotateAnimation(0, 135);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatMode(Animation.REVERSE);
        animation.setRepeatCount(2);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.i("Simon", "onAnimationStart");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.i("Simon", "onAnimationEnd");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("Simon", "onAnimationRepeat");
            }
        });
        imageView.startAnimation(animation);
    }

    private void serial_play() {
        anim1();
    }

    private void anim1() {
        AlphaAnimation animation = new AlphaAnimation(0, 1);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatMode(Animation.REVERSE);
        animation.setRepeatCount(2);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.i("Simon", "onAnimationStart");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.i("Simon", "onAnimationEnd");
                anim2();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("Simon", "onAnimationRepeat");
            }
        });
        imageView.startAnimation(animation);
    }
    private void anim2() {
        ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatMode(Animation.REVERSE);
        animation.setRepeatCount(2);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.i("Simon", "onAnimationStart");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.i("Simon", "onAnimationEnd");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("Simon", "onAnimationRepeat");
            }
        });
        imageView.startAnimation(animation);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号