【Android】Android动画

文章目录

1.帧动画

实现人物不停走动

在drawable文件夹中定义drawable_run_anim.xml

csharp 复制代码
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/run1" android:duration="100"/>
    <item android:drawable="@drawable/run2" android:duration="100"/>
    <item android:drawable="@drawable/run3" android:duration="100"/>
    <item android:drawable="@drawable/run4" android:duration="100"/>
    <item android:drawable="@drawable/run5" android:duration="100"/>
    <item android:drawable="@drawable/run6" android:duration="100"/>
    <item android:drawable="@drawable/run7" android:duration="100"/>
</animation-list>
csharp 复制代码
public class MainActivity extends AppCompatActivity {

    private ImageView my_run_image;

    private boolean flag = true;

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

        my_run_image.setImageResource(R.drawable.drawable_run_anim);
        AnimationDrawable animationDrawable = (AnimationDrawable) my_run_image.getDrawable();
        animationDrawable.setOneShot(false);
        my_run_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag){
                    animationDrawable.start();
                    flag = false;
                }else{
                    animationDrawable.stop();
                    flag = true;
                }
            }
        });
    }
}

2.补间动画

透明度

通过在anim文件夹创建anim_alpha.xml

csharp 复制代码
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1"
    android:toAlpha="0"
    android:duration="2000">

</alpha>
csharp 复制代码
public class TweenAnimAlphaCodeActivity extends AppCompatActivity {

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

        ImageView iv_image_alpha = findViewById(R.id.iv_image_alpha);

        //Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_alpha);
        AlphaAnimation animation = new AlphaAnimation(1f,0f);
        animation.setDuration(1000);
        iv_image_alpha.setAnimation(animation);
    }
}

旋转

通过在anim文件夹创建anim_rotation.xml

  • pivotX pivotY代表以什么为中心旋转
csharp 复制代码
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%">

</rotate>
csharp 复制代码
public class TweenAnimRotationXmlActivity extends AppCompatActivity {

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

//        Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_rotation);
//        iv_image_rotation.setAnimation(animation);

        RotateAnimation animation = new RotateAnimation(0f,180f,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);

        animation.setDuration(1000);
        iv_image_rotation.setAnimation(animation);
    }
}

缩放

通过在anim文件夹创建anim_scale.xml

csharp 复制代码
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="1.2"
    android:toYScale="1.2"
    android:pivotX="50%"
    android:pivotY="50%">

</scale>
csharp 复制代码
public class TweenAnimScaleXmlActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView iv_image_scale = findViewById(R.id.iv_image_scale);
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_scale);
        iv_image_scale.setAnimation(animation);

//        ScaleAnimation animation = new ScaleAnimation(1f,1.2f,1f,1.2f,
//                Animation.RELATIVE_TO_SELF,0.5f,
//                Animation.RELATIVE_TO_SELF,0.5f);

    }
}

平移

通过在anim文件夹创建anim_translate.xml

csharp 复制代码
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="400"
    android:toYDelta="400"
    android:duration="2000">

</translate>
csharp 复制代码
public class TweenAnimTranslateXmlActivity extends AppCompatActivity {

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

        ImageView robotImageView = findViewById(R.id.iv_robot);
//        Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_translate);
//        robotImageView.startAnimation(animation);

        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f
        ,Animation.RELATIVE_TO_SELF,0.5f
        ,Animation.RELATIVE_TO_SELF,0f
        ,Animation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(1000);
        animation.setRepeatCount(2);
        animation.setRepeatMode(2);
        robotImageView.setAnimation(animation);
    }
}

3.属性动画

在res文件夹下创建animator文件夹,创建animator_rotate_x

  • rotationX是Android中用于描述对象在X轴上旋转的角度的属性。
csharp 复制代码
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotationX"
    android:valueType="floatType"
    android:valueFrom="0"
    android:valueTo="360"
    android:duration="1000"
    android:startOffset="1000">

</objectAnimator>
csharp 复制代码
public class PropertyAnimRotateXmlActivity extends AppCompatActivity {

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

        TextView propertyAnimTv = findViewById(R.id.tv_property);
        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this,R.animator.animator_rotate_x);
        animator.setTarget(propertyAnimTv);
        animator.start();

//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(propertyAnimTv, View.ROTATION_X,0f,360f);
//        objectAnimator.setDuration(1000);
//        objectAnimator.start();
    }
}
相关推荐
程序员清风几秒前
快手二面:乐观锁是怎么用它来处理多线程问题的?
java·后端·面试
一线大码16 分钟前
SpringBoot 优雅实现接口的多实现类方式
java·spring boot·后端
2501_9159214319 分钟前
iOS 应用代上架流程,多工具组合与使用 开心上架 跨平台自动化上传指南
android·ios·小程序·uni-app·自动化·cocoa·iphone
花伤情犹在21 分钟前
Java Stream 高级应用:优雅地扁平化(FlatMap)递归树形结构数据
java·stream·function·flatmap
日日行不惧千万里24 分钟前
2025最新仿默往 IM 即时通讯系统源码(PC + Web + iOS + Android)完整版发布!
android·ios
歪歪10024 分钟前
React Native开发Android&IOS流程完整指南
android·开发语言·前端·react native·ios·前端框架
雪芽蓝域zzs30 分钟前
uniapp 修改android包名
android·uni-app
yaoxin52112333 分钟前
212. Java 函数式编程风格 - Java 编程风格转换:命令式 vs 函数式(以循环为例)
java·开发语言
摇滚侠44 分钟前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 属性优先级 行内写法 变量选择 笔记42
java·spring boot·笔记
滑水滑成滑头1 小时前
**发散创新:多智能体系统的探索与实践**随着人工智能技术的飞速发展,多智能体系统作为当今研究的热点领域,正受到越来越多关注
java·网络·人工智能·python