La sexta unidad entre la animación y el uso de la animación de fotogramas.

Explicación detallada de la animación

Descripción general de la introducción https://www.jianshu.com/p/35d25cc205e7

Animación de interpolación

Introducción https://www.jianshu.com/p/733532041f46

Propiedades:
Inserte la descripción de la imagen aquí
donde AnimationSet es una combinación de varios, y el resto de AnimationSet otras
propiedades básicas:
Inserte la descripción de la imagen aquí
donde,
la Duración: duración en milisegundos
el Interpolador:
interpolador lista de interpolación
Inserte la descripción de la imagen aquí
Propiedades alfa
Inserte la descripción de la imagen aquí
Rorate
Inserte la descripción de la imagen aquí
Propiedad de escala de atributos
Inserte la descripción de la imagen aquí
Traducir propiedades
Inserte la descripción de la imagen aquí
AnimationSet

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@[package:]anim/interpolator_resource"
    android:shareInterpolator=["true" | "false"] >
    <alpha
        android:fromAlpha="float"
        android:toAlpha="float" />
    <scale
        android:fromXScale="float"
        android:toXScale="float"
        android:fromYScale="float"
        android:toYScale="float"
        android:pivotX="float"
        android:pivotY="float" />
    <translate
        android:fromXDelta="float"
        android:toXDelta="float"
        android:fromYDelta="float"
        android:toYDelta="float" />
    <rotate
        android:fromDegrees="float"
        android:toDegrees="float"
        android:pivotX="float"
        android:pivotY="float" />
    <set>
        ...
    </set></set>

Muestra
alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0"
    android:toAlpha="1"
    >

</alpha>

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    >
<!--   fromDegrees 角度 -->
<!--    旋转中心点 pivotX pivotY-->

</rotate>

scale.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXScale="1"
    android:toXScale="1.5"
    android:fromYScale="1"
    android:toYScale="0.5"
    android:pivotY="50%"
    android:pivotX="50%"
    >
<!--toXScale 放大的倍数 1.5 放大  0.5 缩小-->
</scale>

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromYDelta="0"
    android:toYDelta="200"
    android:fromXDelta="0"
    android:toXDelta="200"
    >
</translate>

código en actividad

package com.fenghongzhang.anim.tween;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.fenghongzhang.anim.R;

public class TweenActivity extends AppCompatActivity {
    
    

    private Button btn;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private ImageView img;

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

    public void btn(View view) {
    
    
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
        img.startAnimation(animation);
    }

    public void btn1(View view) {
    
    
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        img.startAnimation(animation);
    }

    public void btn2(View view) {
    
    
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
        img.startAnimation(animation);
    }

    public void btn3(View view) {
    
    
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
        img.startAnimation(animation);
    }

    private void initView() {
    
    
        btn = (Button) findViewById(R.id.btn);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        img = (ImageView) findViewById(R.id.img);
    }

    public void btn4(View view) {
    
    
        AnimationSet animationSet = new AnimationSet(true);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
        Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.scale);

        animationSet.addAnimation(animation);
        animationSet.addAnimation(animation2);

        img.startAnimation(animationSet);
    }

    //动态创建动画
    public void btn5(View view) {
    
    
        TranslateAnimation translateAnimation = new TranslateAnimation(0, 300, 0, 300);
        translateAnimation.setDuration(3000);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
    
    
            @Override
            public void onAnimationStart(Animation animation) {
    
    
                Toast.makeText(TweenActivity.this, "start", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationEnd(Animation animation) {
    
    
                Toast.makeText(TweenActivity.this, "end", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
    
    
                Toast.makeText(TweenActivity.this, "重复", Toast.LENGTH_SHORT).show();
            }
        });
        img.startAnimation(translateAnimation);
    }
}

Animación fotograma a fotograma

Introducción https://www.jianshu.com/p/225fe1feba60

método uno

Uso especifico

  1. Paso 1: coloque los recursos de animación (es decir, cada recurso de imagen) en la carpeta dibujable

Sugerencias:
busque la animación gif que necesite.
Utilice un software de descomposición de gif (como GifSplitter) para descomponer el gif en imágenes.

  1. Pasos: configurar e iniciar la animación

Hay dos formas de configurar e iniciar la animación cuadro por cuadro: en código XML / Java.

Cree un archivo de animación effect.xml en la carpeta res / drawable .
Establezca recursos de animación (recursos de imagen)

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

    <item android:drawable="@drawable/a1"></item>
    <item android:drawable="@drawable/a2"></item>
    <item android:drawable="@drawable/a3"></item>
    <item android:drawable="@drawable/a4"></item>
    <item android:drawable="@drawable/a5"></item>
    <item android:drawable="@drawable/a6"></item>
    <item android:drawable="@drawable/a7"></item>
    <item android:drawable="@drawable/a8"></item>
    <item android:drawable="@drawable/a9"></item>
</animation-list>
  1. Paso: cargar e iniciar la animación en código Java
package com.fenghongzhang.anim;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
    
    

    private Button start;
    private Button stop;
    private ImageView img;

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

    public void start(View view) {
    
    
        img.setImageResource(R.drawable.pikaqiu);
        AnimationDrawable drawable = (AnimationDrawable) img.getDrawable();
        drawable.start();


    }

    public void stop(View view) {
    
    
        AnimationDrawable drawable = (AnimationDrawable) img.getDrawable();
        drawable.stop();
    }

    private void initView() {
    
    
        start = (Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);
        img = (ImageView) findViewById(R.id.img);
    }
}

Método 2: realizar en código Java

package com.fenghongzhang.anim;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
    
    

    private Button start;
    private Button stop;
    private ImageView img;

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

           //<-- 直接从drawable文件夹获取动画资源(图片) -->
         animationDrawable = new AnimationDrawable();
        for (int i = 1; i <= 9; i++) {
    
    
            int id = getResources().getIdentifier("a" + i, "drawable", getPackageName());
            Drawable drawable = getResources().getDrawable(id);
            animationDrawable.addFrame(drawable, 100);
        }
    }

    public void start(View view) {
    
    
        img.setImageResource(R.drawable.pikaqiu);
        AnimationDrawable drawable = (AnimationDrawable) img.getDrawable();
        drawable.start();


    }

    public void stop(View view) {
    
    
        AnimationDrawable drawable = (AnimationDrawable) img.getDrawable();
        drawable.stop();
    }

    private void initView() {
    
    
        start = (Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);
        img = (ImageView) findViewById(R.id.img);
    }

    public void btn1(View view) {
    
    
        //只执行一次. 不会停止就停止了
//        animationDrawable.setOneShot(true);
        img.setImageDrawable(animationDrawable);
//        animationDrawable.stop();
        animationDrawable.start();

    }

    public void btn2(View view) {
    
    
//        animationDrawable.setOneShot(true);
        animationDrawable.stop();
    }
}

Supongo que te gusta

Origin blog.csdn.net/shuai_ge_feng/article/details/113768865
Recomendado
Clasificación