Animation
//透明度
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000"
>
</alpha>
public void alpha(View v) {
//使用代码
/*AlphaAnimation alpha = new AlphaAnimation(0,3);
alpha.setDuration(1500);
v.startAnimation(alpha);*/
//使用xml文件
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha));
}
//旋转
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2500">
</rotate>
public void rotate(View v){
//绝对的坐标值
//RotateAnimation ra = new RotateAnimation(0,360,190,205);
//相对自身的坐标值
/*RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
ra.setDuration(2000);
v.startAnimation(ra);*/
//xml文件加载
//文件中直接写数值,当做绝对坐标值,百分比,相对于自身的坐标
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate));
}
//移动
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="150"
android:toYDelta="100"
android:duration="2000">
</translate>
public void translate(View v){
//这里的200是相对于现在的增量
/*TranslateAnimation ta = new TranslateAnimation(0,200,0,200);
ta.setDuration(2000);
v.startAnimation(ta);*/
//文件加载
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate));
}
//缩放
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000">
</scale>
public void scale(View v){
//ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1);
//ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1,100,20);
/* ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
v.startAnimation(sa);*/
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale));
}
//多个效果叠加,使用animationset
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:shareInterpolator="true"
>
<alpha
android:fromAlpha="0"
android:toAlpha="1">
</alpha>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%">
</rotate>
</set>
public void moreAnimation(View v){
//是否共用动画补间,执行属性,加速,匀速还是减速.
/*AnimationSet as = new AnimationSet(true);
as.setDuration(1000);
AlphaAnimation alpha = new AlphaAnimation(0,1);
alpha.setDuration(1000);
TranslateAnimation ta = new TranslateAnimation(0,200,0,200);
ta.setDuration(1000);
RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
ra.setDuration(1000);
ScaleAnimation sa = new ScaleAnimation(0.5f,1,0.2f,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
sa.setDuration(1000);
as.addAnimation(alpha);
//as.addAnimation(ta);
as.addAnimation(ra);
as.addAnimation(sa);
v.startAnimation(as);*/
//文件加载
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim));
}
//监听动画效果的开始,结束,重复
public void listenAnimation(View v){
Animation a = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim);
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//Toast.makeText(MainActivity.this,"animation start", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this,"animation end", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationRepeat(Animation animation) {
//Toast.makeText(MainActivity.this,"animation repeat", Toast.LENGTH_SHORT).show();
}
});
v.startAnimation(a);
}
import android.view.animation.Animation;
import android.view.animation.Transformation;
/**
* Created by sample on 16-10-22.
*/
public class CustomAnimation extends Animation {
//初始化的一些操作
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
System.out.println("initialize");
super.initialize(width, height, parentWidth, parentHeight);
}
//补间时间:假如设置为1 会慢慢从0-1,一直执行applyTransformation()函数.
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
//需要设置的效果应该都在这里
//System.out.println(interpolatedTime);
//t.getMatrix().setTranslate(200*interpolatedTime,200*interpolatedTime);
//设置周期变化移动
//t.getMatrix().setTranslate((float)Math.sin(interpolatedTime*10)*10,(float)Math.sin(interpolatedTime*10)*10);
//设置旋转
t.getMatrix().setRotate(interpolatedTime*180,0.5f,0.5f);
//设置透明度
//t.setAlpha(interpolatedTime);
super.applyTransformation(interpolatedTime, t);
}
}