补间动画需要在java代码设置
动画需要在res里面建个anim文件夹;里面创建需要动画文件 比如透明度 旋转 缩放 平移
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="false"
android:repeatMode="restart"
android:startOffset="1000">
<!--set中属性值 -->
<!-- android:duration="1000" 动画的时间-->
<!-- android:fillAfter="false" 动画在播放完毕后 是否保持动画最后的状态-->
<!-- android:repeatMode="restart" 循环的模式 restart 表示重复 reverse表示反转动画-->
<!-- android:startOffset="1000" 开始延迟效果 一般用在多个动画一起执行的时候-->
<!--透明度 alpha中属性值 注意set中的属性值可以放到alpha中-->
<!-- android:fromAlpha="1.0" 开始透明度值-->
<!-- android:repeatCount="infinite" 常量代表无限循环 其他值都是次数 注意这个属性不能放set里面-->
<!-- android:toAlpha="0.0" 结束透明度值-->
<alpha
android:fromAlpha="1.0"
android:repeatCount="infinite"
android:toAlpha="0.0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500">
<!--set中属性值 -->
<!-- android:duration="1000" 动画的时间-->
<!-- android:fillAfter="false" 动画在播放完毕后 是否保持动画最后的状态-->
<!-- android:repeatMode="restart" 循环的模式 restart 表示重复 reverse表示反转动画-->
<!-- android:startOffset="1000" 开始延迟效果 一般用在多个动画一起执行的时候-->
<!--旋转 rotate中属性值 注意set中的属性值可以放到alpha中-->
<!-- android:fromDegrees 旋转的起始角度值 正数顺时针 负数逆时针-->
<!-- android:toDegrees 旋转的终点角度值 正数顺时针 负数逆时针-->
<!-- android:pivotX 旋转轴点x(旋转中心点x)-->
<!-- android:pivotY 旋转轴点y(旋转中心点Y)-->
<rotate
android:fromDegrees="0"
android:pivotX="100%"
android:pivotY="0"
android:toDegrees="90" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800" android:startOffset="400">
<!--set中属性值 -->
<!-- android:duration="1000" 动画的时间-->
<!-- android:fillAfter="false" 动画在播放完毕后 是否保持动画最后的状态-->
<!-- android:repeatMode="restart" 循环的模式 restart 表示重复 reverse表示反转动画-->
<!-- android:startOffset="1000" 开始延迟效果 一般用在多个动画一起执行的时候-->
<!--缩放 scale中属性值 注意set中的属性值可以放到alpha中-->
<!--android:fromXScale="0.5" 开始缩放时X值【0-1】 -->
<!--android:fromYScale="0.5" 开始缩放时Y值【0-1】-->
<!--android:pivotX="50%" 图片x的百分多少-->
<!--android:pivotY="50%" 图片y的百分多少-->
<!--android:toXScale="1.0" 缩放后X值(0-1)-->
<!--android:toYScale="1.0" 缩放后Y值(0-1) -->
<scale
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="0%"
android:pivotY="0%"
android:toXScale="0"
android:toYScale="0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="400">
<!--set中属性值 -->
<!-- android:duration="1000" 动画的时间-->
<!-- android:fillAfter="false" 动画在播放完毕后 是否保持动画最后的状态-->
<!-- android:repeatMode="restart" 循环的模式 restart 表示重复 reverse表示反转动画-->
<!-- android:startOffset="1000" 开始延迟效果 一般用在多个动画一起执行的时候-->
<!--平移 translate中属性值 注意set中的属性值可以放到alpha中-->
<!--android:fromXDelta="50%" 平移时开始的x值 (50%指图片X的一半)-->
<!--android:fromYDelta="0" 平移时开始的Y值-->
<!--android:toXDelta="0" 平移后的x值-->
<!--android:toYDelta="100" 平移后的Y值 -->
<translate
android:fromXDelta="50%"
android:fromYDelta="0"
android:toXDelta="500"
android:toYDelta="0" />
</set>
XML代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" /> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="位移" /> </LinearLayout> <Button android:id="@+id/btn5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="组合动画" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--如果想参考动画变化轨迹 可以设置显示ImageButton--> <ImageButton android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/ic_launcher_round" /> <ImageButton android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/ic_launcher_round" /> </RelativeLayout> </LinearLayout>
java代码;
/**
* 补间动画-->透明度 旋转 缩放 平移
*/
public class animation2Activity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
iv = (ImageView) findViewById(R.id.iv);
final Button btn1 = (Button) findViewById(R.id.btn1);
final Button btn2 = (Button) findViewById(R.id.btn2);
final Button btn3 = (Button) findViewById(R.id.btn3);
final Button btn4 = (Button) findViewById(R.id.btn4);
final Button btn5 = (Button) findViewById(R.id.btn5);
//获取 透明度动画
final Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.alpha);
//获取 旋转动画
final Animation animationAlphaRotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
//获取 缩放动画
final Animation animationAlphaScale = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
//获取 平移动画
final Animation animationAlphaTranslate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
//透明度动画
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置动画到view上 启动动画
iv.startAnimation(animationAlpha);
}
});
//旋转动画
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置动画到view上 启动动画
iv.startAnimation(animationAlphaRotate);
}
});
//缩放动画
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置动画到view上 启动动画
iv.startAnimation(animationAlphaScale);
}
});
//平移动画
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置动画到view上 启动动画
iv.startAnimation(animationAlphaTranslate);
}
});
//平移动画
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//多个动画组合时
AnimationSet set = new AnimationSet(true);
//添加动画
set.addAnimation(animationAlphaTranslate);
set.addAnimation(animationAlphaScale);
//哪个动画先添加就先执行 注意当一个动画还没有执行完 两个个动画开始时间到了,两个动画会同时执行
//建议两个动画时间 第二个是第一个的两倍 ;或者第二个延迟第一个时间后执行(对应属性startOffset)
//设置动画到view上
iv.startAnimation(set);
}
});
}
}