Die sechste Einheit Tween-Animation und die Verwendung von Frame-Animation

Detaillierte Erklärung der Animation

Einführungsübersicht https://www.jianshu.com/p/35d25cc205e7

Tween-Animation

Einführung https://www.jianshu.com/p/733532041f46

Eigenschaften:
Fügen Sie hier eine Bildbeschreibung ein
wobei AnimationSet eine Kombination aus mehreren und der Rest von AnimationSet andere
grundlegende Eigenschaften ist:
Fügen Sie hier eine Bildbeschreibung ein
wobei
die Dauer: Dauer in Millisekunden
die Interpolator: Interpolator-
Interpolationsliste
Fügen Sie hier eine Bildbeschreibung ein
Alpha-Eigenschaften
Fügen Sie hier eine Bildbeschreibung ein
Rorate Attribute
Fügen Sie hier eine Bildbeschreibung ein
Scale-Eigenschaft
Fügen Sie hier eine Bildbeschreibung ein
Eigenschaften übersetzen
Fügen Sie hier eine Bildbeschreibung ein
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>

Beispiel
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>

Code in Aktivität

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);
    }
}

Bild für Bild Animation

Einführung https://www.jianshu.com/p/225fe1feba60

Methode eins

Spezifische Verwendung

  1. Schritt 1: Legen Sie die Animationsressourcen (dh jede Bildressource) in den Zeichenordner

Tipps:
Suchen Sie die
gewünschte GIF- Animation. Verwenden Sie die GIF-Zerlegungssoftware (z. B. GifSplitter), um das GIF in Bilder zu zerlegen.

  1. Schritte: Richten Sie die Animation ein und starten Sie sie

Es gibt zwei Möglichkeiten, Frame-für-Frame-Animationen festzulegen und zu starten: in XML / Java-Code.

Erstellen Sie eine animation effect.xml-Datei im Ordner res / drawable . Legen
Sie Animationsressourcen ( Bildressourcen ) fest.

<?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. Schritt: Laden und Starten der Animation in Java-Code
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);
    }
}

Methode 2: In Java-Code realisieren

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();
    }
}

Ich denke du magst

Origin blog.csdn.net/shuai_ge_feng/article/details/113768865
Empfohlen
Rangfolge