Android动画-逐帧动画

xml中配置

一、生成配置文件

res/drawable中生成loading.xml






    
    
    

二、在activity_main.xml中加入View




    

    
        

三、代码中使用

package com.example.testframeanimation;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationBrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View view = findViewById(R.id.mainView);
        animationBrawable = (AnimationDrawable) view.getBackground();
        // 执行一次
        //animationBrawable.setOneShot(true);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.startButtom:
                animationBrawable.start();
                break;
            case R.id.stopButtom:
                animationBrawable.stop();
                break;
        }
    }
}

你可能感兴趣的:(Android动画-逐帧动画)