1.代码
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnAlphaAnimationByCode:
{
AlphaAnimation aa = new AlphaAnimation( 0,1);
aa.setDuration(1000);
v.startAnimation(aa);
}
break;
case R.id.btnAlphaAnimationByXml:
{
v.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.alpha_animation));
}
break;
case R.id.btnRotationAnimationByCode:
{
RotateAnimation ra ;
// ra = new RotateAnimation(0,360,100,100);//
// ra = new RotateAnimation(0,360,100,100);//从0-360度,第三,四个参数是旋转中心点的x,y坐标相对与本视图左上角.
ra = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//相对于自身的中心点旋转.
ra.setDuration(1000);
v.startAnimation(ra);
}
break;
case R.id.btnRotationAnimationByXml:
{
v.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.rotation_animation));
}
break;
case R.id.btnTranslateAnimationView:
{
getFragmentManager().beginTransaction()
.addToBackStack("T")
.replace(R.id.container, new TranslationFragment())
.commit();
}
break;
case R.id.btnScaleAnimationByCode:
{
ScaleAnimation sa ;
//float fromX, float toX, float fromY, float toY 默认锚点是左上
// sa = new ScaleAnimation(0,1,0,1);
//后两个参数是锚点坐标,是从自身的左上开始的absolute绝对坐标值.
// sa = new ScaleAnimation(0,1,0,1,200,200);
sa = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
v.startAnimation(sa);
}
break;
case R.id.btnScaleAnimationByXml:
{
v.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.scale_animation));
}
break;
case R.id.btnTranslateAndAlphaByCode:
{
AnimationSet as = new AnimationSet(true);
as.setDuration(1000);
AlphaAnimation aa = new AlphaAnimation(0,1);
as.addAnimation(aa);
TranslateAnimation ta = new TranslateAnimation(100,0,100,0);
as.addAnimation(ta);
v.startAnimation(as);
}
break;
case R.id.btnTranslateAndAlphaByXml:
Animation a = AnimationUtils.loadAnimation(getActivity(), R.anim.set_animation);
a.setAnimationListener(this);
v.startAnimation(a);
break;
case R.id.btnCustomAnimation:
{
getFragmentManager().beginTransaction()
.addToBackStack("C")
.replace(R.id.container, new CustomAnimationFragment())
.commit();
}
break;
}
}
2.xml配置动画
每个xm一个独立文件 ,在res/anim 下.
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000"
>
</alpha>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
>
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
<?xml version="1.0" encoding="utf-8"?>
<set android:duration="1000"
android:shareInterpolator="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0" android:toAlpha="1"></alpha>
<translate android:fromXDelta="0" android:toXDelta="100"
android:fromYDelta="0" android:toYDelta="100"></translate>
</set>
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="100"
android:duration="1000"
>
</translate>