ActivityCode
加入4个button,点击不同的按钮实现不同的动画效果
<?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:id="@+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度渐变" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:src="@drawable/cat" />
</LinearLayout>
在res下新建anim文件夹新建4个xml文件并设置相关属性
drawable下放入实现动画效果的图片
anim_alpha渐变
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="2000"
android:fillAfter="true"
android:fromAlpha="1.0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toAlpha="0.0" />
</set><!-- 透明度控制动画效果
浮点型:
fromAlpha 属性为动画最开始的透明度
toAlpha 属性为动画结束时的透明度(0.0表示完全透明-1.0表示完全不透明)
长整型:
duration: 属性为动画的持续时间(ms为单位)
-->
anim_rotate旋转
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720"></rotate>
<rotate
android:duration="2000"
android:fromDegrees="360"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="2000"
android:toDegrees="0"></rotate>
</set><!-- 旋转动画效果
fromDegrees 起始时实现动画效果物体的角度
toDegrees 结束时物体旋转的角度
-->
anim_scale尺寸伸缩
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="2000"
android:fillAfter="true"
android:fromXScale="1"
android:fromYScale="1"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXScale="2.0"
android:toYScale="2.0" />
</set><!-- 尺寸伸缩动画效果
属性: interpolator 指定一个动画的插入器
accelerate_decelerate_interpolator 加速-减速插入器
accelerate_interpolator 加速插入器
decelerate_interpolator 减速插入器
fromXScale 开始时对应x坐标上的伸缩尺寸
toXScale 结束时对应x坐标上的伸缩尺寸
fromYScale 开始时对应y坐标上的伸缩尺寸
toYScale 结束时对应y坐标上的伸缩尺寸
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
-->
anim_translate平移
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXDelta="860"
android:toYDelta="0"></translate>
</set><!-- 平移动画效果
fromXDelta 起始时x坐标上的位置
toXDelta 结束时x坐标上的位置
fromYDelta 起始时y坐标上的位置
toYDelta 结束时y坐标上的位置
-->
JavaCode
package com.example.animation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取“旋转”动画资源
final Animation rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
// 获取“平移”动画资源
final Animation translate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
// 获取“缩放”动画资源
final Animation scale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
// 获取“透明底变化”动画资源
final Animation alpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
// 获取要应用动画效果的ImageView
final ImageView iv = findViewById(R.id.imageView1);
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 播放旋转动画
iv.startAnimation(rotate);
}
});
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 播放平移动画
iv.startAnimation(translate);
}
});
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 播放缩放动画
iv.startAnimation(scale);
}
});
Button button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 播放透明度渐变动画
iv.startAnimation(alpha);
}
});
}
}