- 描述:使用Animation动画,实现渐变、缩放、旋转、平移的效果
- 难度:初级
- 知识点:Animation
使用Animation实现渐变、缩放、旋转、平移的效果
一、渐变
关于xml-set-translate的知识,需要学习上一篇博客(http://t.csdn.cn/OJYan)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:interpolator="@android:anim/linear_interpolator"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
</set>
二、缩放
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:repeatMode="reverse"
android:repeatCount="infinite"
android:duration="3000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.5"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
三、旋转
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:duration="1000"/>
</set>
四、平移
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0.0"
android:fromYDelta="0.0"
android:toXDelta="0.0"
android:toYDelta="-100"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:duration="4000"/>
</set>
五、详细使用
1、UI界面设计
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image_xm"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/xiongmao"
android:layout_marginTop="100dp"
android:layout_marginLeft="150dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_below="@id/image_xm"
android:gravity="center">
<Button
android:id="@+id/bin_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="渐变"/>
<Button
android:id="@+id/bin_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"/>
<Button
android:id="@+id/bin_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"/>
<Button
android:id="@+id/bin_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移"/>
</LinearLayout>
</RelativeLayout>
2、Activity编码
ImageView XioMao;
Button alpha,rotate,scale,translate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化
init();
}
private void init(){
XioMao = findViewById(R.id.image_xm);
alpha = findViewById(R.id.bin_alpha);
rotate = findViewById(R.id.bin_rotate);
scale = findViewById(R.id.bin_scale);
translate = findViewById(R.id.bin_translate);
alpha.setOnClickListener(this);
rotate.setOnClickListener(this);
scale.setOnClickListener(this);
translate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bin_alpha:
// 方法一
// Animation alpha = AnimationUtils.loadAnimation(this,R.anim.alpha_anim);
// XioMao.startAnimation(alpha);
// 方法二
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f,1.0f);
alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE);
alphaAnimation.setDuration(5000);
alphaAnimation.setRepeatCount(AlphaAnimation.INFINITE);
XioMao.startAnimation(alphaAnimation);
break;
case R.id.bin_rotate:
Animation rotate = AnimationUtils.loadAnimation(this,R.anim.rotate_anim);
XioMao.startAnimation(rotate);
break;
case R.id.bin_scale:
Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale_anim);
XioMao.startAnimation(scale);
break;
case R.id.bin_translate:
Animation translate = AnimationUtils.loadAnimation(this,R.anim.translate_anim);
XioMao.startAnimation(translate);
break;
}
}