一直想自学Android动画,今天终于着手学了!虽然只是一些最基本的,一点也不酷炫,但是当真的运行在手机上的时候,还是超级兴奋的!
1.首先在res下新建一个anim文件夹,然后设置动画属性;
如:渐变
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fromAlpha="0.1"
android:toAlpha="1.0">
</alpha>
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:startOffset="3000"> //在3秒之后执行
</alpha>
旋转:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="370"
android:pivotX="50%" //确定以目标哪一点为旋转点(50%,50%以中心点)
android:pivotY="50%"
android:duration="3000"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"/>//设置动画速度(加速减速等)
</set>
缩放:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="3000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"/>
</set>
平移:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromXDelta="20"
android:fromYDelta="10"
android:toXDelta="100"
android:toYDelta="100"/>
</set>
2.然后在MainActivity中应用:
package com.example.animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView image;
private Button alpha;
private Button calpha;
private Button rotate;
private Button scale;
private Button translate;
private Button zoonin;
Animation loadanimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView) findViewById(R.id.image);
alpha=(Button) findViewById(R.id.alpha);
calpha=(Button) findViewById(R.id.button1);
rotate=(Button) findViewById(R.id.button2);
scale=(Button) findViewById(R.id.button3);
translate=(Button) findViewById(R.id.button4);
zoonin=(Button) findViewById(R.id.button5);
alpha.setOnClickListener(this);
calpha.setOnClickListener(this);
rotate.setOnClickListener(this);
scale.setOnClickListener(this);
translate.setOnClickListener(this);
zoonin.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Animation loadAnimation;
switch (view.getId()) {
case R.id.alpha: {
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
image.startAnimation(loadAnimation);
break;
}
case R.id.button1:{
loadAnimation=AnimationUtils.loadAnimation(this, R.anim.continue_alpha);
image.startAnimation(loadAnimation);
break;
}
case R.id.button2:{
loadAnimation=AnimationUtils.loadAnimation(this, R.anim.rotate);
image.startAnimation(loadAnimation);
break;
}
case R.id.button3:{
loadAnimation=AnimationUtils.loadAnimation(this, R.anim.scale);
image.startAnimation(loadAnimation);
break;
}
case R.id.button4:{
loadAnimation=AnimationUtils.loadAnimation(this, R.anim.translate);
image.startAnimation(loadAnimation);
break;
}
case R.id.button5:{
loadAnimation=AnimationUtils.loadAnimation(this, R.anim.zoon_in);
image.startAnimation(loadAnimation);
break;
}
}
}}
</set>