逐帧动画
定义:把几张图片快速播放形成的一个效果
MainActivity
package a.b.c.lele_anim;
import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity{ private boolean flag = true; @Override protected void onCreate(Bundle savadInstanceState){ super.onCreate(savedInstance); setContentView(R.layout.activity_main); RelativeLayout relativeLaout = findViewById(R.id.rl); AnimationDrawable anim = (AnimationDrawable) relativeLayoutl.getBackground(); relativeLayout.setOnClickListener(new View.OnClickListener(){ @Override public void onCLick(View view){ if(flag){ anim.start(); flag = false; }else{ anim.stop(); flag = true; } } }); } }
|
activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/frame" android:id="@+id/rl" /> frame
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tx" android:duration="120"></item> <item android:drawable="@drawable/bg" android:duration="120"></item> </animation-list>
|
补间动画
定义:我们设置一个初始值以及结束值,然后设置一个变化时间,安卓会自动给我们补全
创建一个文件夹
new 一个Animation Resource File
MainActivity package a.b.c.animtweened; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { 透明度 Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha); 旋转 Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate); 缩放 Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale); 平移 Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate); imageView.startAnimation(animation); } }); } }
|
alpha.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000" /> </set>
rotate.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" /> </set>
scale.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:duration="2000" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.5" android:toYScale="0.5" /> </set>
translate.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="400" android:toYDelta="400" /> </set>
|
属性动画
MainActivity package a.b.c.animproperty;
import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.os.Bundle; import android.util.Log; import android.widget.TextView;
import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f,1f); valueAnimator.setDuration(2000); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) { float value = (float) valueAnimator.getAnimatedValue(); Log.e("lele", "onAnimationUpdate: " + value ); } }); valueAnimator.start();
TextView textView = findViewById(R.id.tv); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"alpha",0f,1f); objectAnimator.setDuration(40000); objectAnimator.start(); objectAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(@NonNull Animator animator) {
}
@Override public void onAnimationEnd(@NonNull Animator animator) {
}
@Override public void onAnimationCancel(@NonNull Animator animator) {
}
@Override public void onAnimationRepeat(@NonNull Animator animator) {
} }); objectAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); } }); } }
|
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
|