Android 动画
Android 动画
帧动画
动画的启动和停止
-
获取动画的 Drawable资源
-
AnimationDrawable anim = (AnimationDrawable)relativeLayout.getBackground();
-
-
//启动动画
-
anim.start();
-
-
停止动画
-
anim.stop();
-
-
得到frame帧布局
-
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/meinv" android:duration="120"/> <item android:drawable="@drawable/meinv2" android:duration="120"/> <item android:drawable="@drawable/meinv3" android:duration="120"/> <item android:drawable="@drawable/meinv4" android:duration="120"/> </animation-list>
-
-
创建RelativeLayout 布局
-
<RelativeLayout android:id="@+id/rl" android:layout_height="match_parent" android:layout_width="match_parent" android:background="@drawable/frame" xmlns:android="http://schemas.android.com/apk/res/android" />
-
-
设置点击开始 和 结束
-
private boolean flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout relativeLayout = findViewById(R.id.rl); final AnimationDrawable animationDrawable = (AnimationDrawable) relativeLayout.getBackground(); relativeLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(flag){ animationDrawable.start(); }else { animationDrawable.stop(); flag = true; } } }); }
-
补间动画
- alpha 透明度
- ratate 旋转
- scale 缩放
- translate 平移
alpha
-
设置alpha动画
-
<alpha android:fromAlpha="0" android:toAlpha="1" android:duration = "2000" />
-
-
设置 动画 图片
-
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:adjustViewBounds="true" android:maxWidth="300dp" android:maxHeight="300dp" android:src="@drawable/meinv"/>
-
-
设置点击事件
-
imageview = findViewById(R.id.iv); imageview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 通过加载xml动画设置文件,来创建一个 Animation 对象 Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageview.startAnimation(animation); } });
-
rotate
-
设置rotate布局
-
<rotate android:duration = "2000" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" />
-
-
设置点击事件
-
imageview = findViewById(R.id.iv); imageview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 通过加载xml动画设置文件,来创建一个 Animation 对象 Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); imageview.startAnimation(animation); } });
-
scale
-
设置scale帧
-
<scale android:fromXScale="1" android:fromYScale="1" android:toXScale="0.5" android:toYScale="0.5" android:pivotY="50%" android:pivotX="50%" android:duration="2000" />
-
translate
-
设置布局
-
<translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="400" android:toYDelta="400" android:duration = "2000" />
-
属性动画
-
ValueAnimator
-
改变animator值的
-
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f,1f); valueAnimator.setDuration(2000); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float)animation.getAnimatedValue(); Log.e("leo","onAnimationUpdate" + value); } }); valueAnimator.start();
-
-
ObjectAnimator
-
继承 ValueAnimator 可以直接针对对象 控制控件
-
设置图片控件
-
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:adjustViewBounds="true" android:maxWidth="300dp" android:maxHeight="300dp" android:src="@drawable/meinv"/>
-
-
设置图片渐进
-
ImageView imageView = findViewById(R.id.iv); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView,"alpha", 0f,1f ); objectAnimator.setDuration(4000); objectAnimator.start();
-
-
-
监听器
-
onAnimationStart() 动画开始的时候调用
-
onAnimationEnd() 动画结束的时候调用
-
onAnimationCancel() 动画被取消的时候调用
-
onAnimationRepeat() 动画重复执行的时候调用
-
·
objectAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); objectAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); } });
-
-