1、新建文件。【注意:文件名只能命名为anim】

2、新建文件

3、在新建的文件中添加代码:【以下代码仅供参考】
3.1、改变动画的透明度:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:interpolator="@android:anim/linear_interpolator"android:repeatMode="reverse"android:repeatCount="infinite"android:duration="1000" //持续时间android:fromAlpha="1.0"android:toAlpha="0.0"/>
</set>
3.2、动画旋转 :
<?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 反向旋转--><rotateandroid:fromDegrees="0"android:toDegrees="360" //旋转360°android:pivotX="50%"android:pivotY="50%"android:repeatCount="-1"android:repeatMode="reverse" android:duration="1000"/><!-- 正向旋转-->
<!-- <rotate-->
<!-- android:fromDegrees="0"-->
<!-- android:toDegrees="360"-->
<!-- android:pivotX="50%"-->
<!-- android:pivotY="50%"-->
<!-- android:repeatCount="-1"-->
<!-- android:repeatMode="restart"-->
<!-- android:duration="1000"/>--></set>
3.3、动画缩放:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><scaleandroid:fromXScale="1.0"android:fromYScale="1.0"android:toXScale="0.5"android:toYScale="0.5"android:pivotX="50%"android:pivotY="50%"android:repeatMode="reverse"android:repeatCount="infinite"android:duration="3000"/>
</set>
3.4、动画平移:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0.0"android:fromYDelta="0.0"android:toXDelta="100"android:toYDelta="0.0"android:repeatCount="infinite"android:repeatMode="reverse"android:duration="4000"/>
</set>
Java代码如下:【仅供参考】
//这里用到了多个按钮,直接实现OnClickListener()方法
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private ImageView ima;private Button btn1;private Button btn2;private Button btn3;private Button btn4;private Button btn5;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {ima = (ImageView) findViewById(R.id.ima);btn1 = (Button) findViewById(R.id.btn1);btn2 = (Button) findViewById(R.id.btn2);btn3 = (Button) findViewById(R.id.btn3);btn4 = (Button) findViewById(R.id.btn4);btn5 = (Button) findViewById(R.id.btn5);btn1.setOnClickListener(this);btn2.setOnClickListener(this);btn3.setOnClickListener(this);btn4.setOnClickListener(this);btn5.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.btn1:
Animation alpha = AnimationUtils.loadAnimation(this,R.anim.alpha_animation);//透明度,渐变ima.startAnimation(alpha);//开启动画break;case R.id.btn2:
Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate_animation);//旋转ima.startAnimation(rotate);break;case R.id.btn3:
Animation translate = AnimationUtils.loadAnimation(this,R.anim.translate_animation);//平移ima.startAnimation(translate);break;case R.id.btn4:
Animation scale =AnimationUtils.loadAnimation(this,R.anim.scale_animation);//缩放ima.startAnimation(scale);break;case R.id.btn5:ima.clearAnimation();//动画暂停break;}}
}
效果演示参考如下链接:
Android studio 补间动画显示-CSDN













