帧动画
动画
帧动画
- Drawable Animation / Frame Animation
res/drawable
animation-list
动画一帧接一帧的播放
This animation runs for just three frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop. With this XML saved as rocket_thrust.xml in the res/drawable/ directory of the project, it can be added as the background image to a View and then called to play. Here’s an example Activity, in which the animation is added to an ImageView and then animated when the screen is touched:
1. 注意事项
* 放在自定义的res/drawable下
* 在自定义的drawable下自定义xml,并选择animation-list
* 将API代码拷贝到XML
1.
* android:oneshot="true"> true表示只播放一次
补间动画-透明度\旋转\平移\缩放\ (熟悉)
- View Animation / Tween Animation
- 代码实现
- 通过xml文件实现
- 查看
file:///D:/Develop/adt-bundle-windows-x86_64_20140101/sdk/docs/guide/topics/resources/animation-resource.html#View
- 查看
动画从开始到结束, 连续地进行
属性动画-透明度\旋转\平移\缩放\ (掌握) (*)
Property Animation / Object Animation
- 代码实现
- 通过xml文件实现
- 动画集合
兼容低版本
- Github -> Jake Wharton
补间动画和属性动画的区别
- 补间动画只是一个动画的效果, 事件还在原来位置.
- 属性动画真正修改了View的属性. 真正的控件操作区域都发生变化.
MainActivity.java
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView iv_image;
private ObjectAnimator alphaAnimator;
private ObjectAnimator rotationAnimator;
private ObjectAnimator translationXAnimator;
private ObjectAnimator scaleAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_image = (ImageView) findViewById(R.id.iv_image);
}
// 透明动画
public void alpha(View v){
// iv_image.setAlpha(0.7f);
// target 要执行动画的对象 iv_image
// propertyName 要更新的属性名称
// values 要更新的变化值
alphaAnimator = ObjectAnimator.ofFloat(iv_image, "alpha", 0.0f, 1.0f);
alphaAnimator.setDuration(1000);
alphaAnimator.setRepeatCount(2);// 重复次数
alphaAnimator.setRepeatMode(ObjectAnimator.REVERSE); // 重复模式
alphaAnimator.start();
}
// 旋转动画
public void rotate(View v){
// iv_image.setRotation(45f);
rotationAnimator = ObjectAnimator.ofFloat(iv_image, "rotation", 0.0f, 360f);
rotationAnimator.setDuration(1000);
rotationAnimator.setRepeatCount(2);// 重复次数
rotationAnimator.setRepeatMode(ObjectAnimator.REVERSE); // 重复模式
rotationAnimator.start();
}
// 平移动画
public void translate(View v){
// iv_image.setTranslationX(80f);
// translationXAnimator = ObjectAnimator.ofFloat(iv_image, "translationX", 0.0f, 150f);
// translationXAnimator.setDuration(1000);
// translationXAnimator.setRepeatCount(2);// 重复次数
// translationXAnimator.setRepeatMode(ObjectAnimator.REVERSE); // 重复模式
// translationXAnimator.start();
// AnimationUtils.loadAnimation(context, id)
Animator loadAnimator = AnimatorInflater.loadAnimator(this, R.animator.animator_y_iv);
loadAnimator.setTarget(iv_image);
loadAnimator.start();
}
// 缩放动画
public void scale(View v){
// iv_image.setScaleX(1.5f);
// iv_image.setScaleY(1.5f);
PropertyValuesHolder xpvh = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 2.0f);
PropertyValuesHolder ypvh = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 2.0f);
scaleAnimator = ObjectAnimator.ofPropertyValuesHolder(iv_image, xpvh, ypvh);
// ObjectAnimator animator = ObjectAnimator.ofFloat(iv_image, "scaleX", 0.5f, 2.0f);
scaleAnimator.setDuration(1000);
scaleAnimator.setRepeatCount(2);// 重复次数
scaleAnimator.setRepeatMode(ObjectAnimator.REVERSE); // 重复模式
scaleAnimator.start();
// iv_image.animate().scaleX(0.5f).scaleY(0.5f).translationX(50f).rotation(90f);
}
// 组合动画
public void set(View v){
AnimatorSet set = new AnimatorSet();
set.playTogether(
alphaAnimator,
rotationAnimator,
translationXAnimator,
scaleAnimator
);
// 透明度和旋转一起执行, 在平移之前, 在缩放之后
// 缩放 -> 透明度和旋转 -> 平移
// set.play(alphaAnimator).with(rotationAnimator).before(translationXAnimator).after(scaleAnimator);
// set.setStartDelay(1500); // 设置开始动画延时时间
set.start();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="alpha"
android:text="透明" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rotate"
android:text="旋转" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="translate"
android:text="平移" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scale"
android:text="缩放" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="set"
android:text="组合" />
</LinearLayout>
<ImageButton
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
animator_y_iv_xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="y"
android:duration="1000"
android:startOffset="1000"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="200"
android:valueType="floatType" >
</objectAnimator>
补间动画和XML加载动画
XML加载动画
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="2.0"
android:fromYScale="0.5"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="restart"
android:duration="1000" >
</scale>