逐帧动画、补间动画、属性动画简单使用

逐帧动画

1、把图片资源放到 mipmap 文件夹中,在选择文件夹的时候选 mipmap-xxhdpi 文件夹
2、在 drawable 文件夹下创建 animation-list 文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/a" android:duration="100"/>
    <item android:drawable="@mipmap/b" android:duration="100"/>
    <item android:drawable="@mipmap/c" android:duration="100"/>
    <item android:drawable="@mipmap/d" android:duration="100"/>
    <item android:drawable="@mipmap/e" android:duration="100"/>
    <item android:drawable="@mipmap/f" android:duration="100"/>
    <item android:drawable="@mipmap/g" android:duration="100"/>
    <item android:drawable="@mipmap/h" android:duration="100"/>
    <item android:drawable="@mipmap/i" android:duration="100"/>
    <item android:drawable="@mipmap/j" android:duration="100"/>
    <item android:drawable="@mipmap/k" android:duration="100"/>
</animation-list>
3、在 xml 文件中应用 drawable 文件夹下的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/anim_frame"/>

</LinearLayout>
4、在 Activity 中
package com.example.anim_frame;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView image;

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

    private void initView() {
        image = (ImageView) findViewById(R.id.image);

        AnimationDrawable background = (AnimationDrawable) image.getBackground();
        background.start();
    }
}

关于解决 Android 帧动画卡顿问题
Android 解决帧动画卡顿问题

补间动画

补间动画.gif
1、在 res 文件夹下创建 anim 文件夹,在 anim 文件夹中创建 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0" />

    <scale
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="0.5"
        android:pivotY="0.5"
        android:toXScale="0"
        android:toYScale="0" />

    <translate
        android:fromXDelta="1"
        android:fromYDelta="1"
        android:toXDelta="5"
        android:toYDelta="5" />

    <rotate
        android:fromDegrees="0"
        android:pivotX="0.5"
        android:pivotY="0.5"
        android:toDegrees="360" />
</set>
2、在 xml 文件中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher_round"/>

</LinearLayout>
3、在 Activity 中
package com.example.anim_filling;

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

public class MainActivity extends AppCompatActivity {

    private ImageView image;

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

    private void initView() {
        image = (ImageView) findViewById(R.id.image);

        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim);
        animation.setDuration(5000);
        animation.setRepeatCount(-1); // 重复次数,-1 代表无限循环
        // 也可以在 anim 的 xml 中设置:android:repeatCount="infinite"
        image.setAnimation(animation);
        animation.start();
    }
}

3、属性动画

属性动画.gif
1、xml 文件中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>

</LinearLayout>
2、Activity 中
package com.example.anim_attribute;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView text;

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

    private void initView() {
        text = (TextView) findViewById(R.id.text);

        ValueAnimator animator = ValueAnimator.ofInt(5, 0);
        animator.setDuration(6000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                text.setText(value+"");
            }
        });
        animator.start();

        ObjectAnimator animator1 = ObjectAnimator.ofFloat(text, "alpha", 1.0f, 0.0f, 1.0f);
        animator1.setInterpolator(new LinearInterpolator());
        animator1.setDuration(10000);
        animator1.start();
    }
}

Android 属性动画卡顿的优化

Android自定义控件三部曲文章索引
上面这篇文章强烈推荐

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容