Android-视图动画

1) AlphaAnimation

代码实现AlphaAnimation

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.aa_fragment,container,false);

root.findViewById(R.id.btnAnimateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
            AlphaAnimation aa = new AlphaAnimation(0,1);
aa.setDuration(2000);
v.startAnimation(aa);
}
    });
    return root;
}

XML实现AlphaAnimation

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.aa_fragment,container,false);

root.findViewById(R.id.btnAnimateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//xml实现alphaAnimation
v.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.aa));
}
    });
    return root;
}

res/anim/aa.xml

<?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>

2) RotateAnimation
代码实现RotateAnimation

root.findViewById(R.id.btnRotateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//              RotateAnimation ra = new RotateAnimation(0,360,100,100);//0表示初始旋转角度,360表示终止旋转角度,100是z坐标像素和y坐标像素,

                //相对自身旋转,0.5f 为百分比,相对于自身的50%
RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

ra.setDuration(1000);
v.startAnimation(ra);

}
        });

XML 实现RotateAnimation

v.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.ra));

R.anim.ra.xml

<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%"/>

3) TranslateAnimation
代码实现:

root.findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
        TranslateAnimation ta = new TranslateAnimation(0,200,0,0);//0相对于自身位置
ta.setDuration(1000);
v.startAnimation(ta);
}
});

xml实现

root.findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//                TranslateAnimation ta = new TranslateAnimation(0,200,0,0);//0相对于自身位置
//                ta.setDuration(1000);
//                v.startAnimation(ta);
v.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.ta));
}
        });

R.anim.ta.xml:

<translate xmlns:android="http://schemas.android.com/apk/res/android"

android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="0"
android:duration="1000"/>

4) ScaleAnimation
代码实现:

root.findViewById(R.id.btnScaleMe).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//默认左上角开始缩放,可以设成中心点缩放
ScaleAnimation 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);
}
});

xml实现:

root.findViewById(R.id.btnScaleMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//                //默认左上角开始缩放,可以设成中心点缩放
//                ScaleAnimation 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);

v.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.sa));
}
        });

R.anim.sa.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android"

android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:pivotY="50%"
android:pivotX="50%"
android:duration="1000"
/>

5) 混合动画效果(两个动画效果同时启动),也可以设置分别启动
代码实现:

root.findViewById(R.id.btnAnimateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
        AnimationSet as = new AnimationSet(true);
as.setDuration(2000);

AlphaAnimation aa = new AlphaAnimation(0,1);
aa.setDuration(2000);
as.addAnimation(aa);

TranslateAnimation ta = new TranslateAnimation(200,0,200,0);
ta.setDuration(2000);
as.addAnimation(ta);

v.startAnimation(as);
}
});

XML实现

root.findViewById(R.id.btnAnimateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//                AnimationSet as = new AnimationSet(true);
//                as.setDuration(2000);
//
//                AlphaAnimation aa = new AlphaAnimation(0,1);
//                aa.setDuration(2000);
//                as.addAnimation(aa);
//
//                TranslateAnimation ta = new TranslateAnimation(200,0,200,0);
//                ta.setDuration(2000);
//                as.addAnimation(ta);
//
//                v.startAnimation(as);

v.startAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.as));
}
        });

R.anim.as.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"

android:duration="1000"
android:shareInterpolator="true">
    <alpha
android:fromAlpha="0"
android:toAlpha="1" />
    <translate
android:fromXDelta="200"
android:fromYDelta="200"
android:toXDelta="0"
android:toYDelta="0" />
</set>

6) 侦听动画的效果:

Animation a = AnimationUtils.loadAnimation(getActivity(), R.anim.as);
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//动画开始时
}

@Override
public void onAnimationEnd(Animation animation) {
        Toast.makeText(getContext(),"Animatioin End",Toast.LENGTH_SHORT).show();
//动画结束时
}

@Override
public void onAnimationRepeat(Animation animation) {
//动画重复时
}
});
v.startAnimation(a);

7) 自定义动画效果:

->定义CustomAnimation extends Animation
->重写applyTransformation()

protected void applyTransformation(float interpolatedTime, Transformation t) {


//        t.setAlpha(interpolatedTime);

if (interpolatedTime < 0.5) {
            t.getMatrix().setTranslate(500 * interpolatedTime, 0);
countX = 500 * interpolatedTime;
} else {
countY = interpolatedTime - 0.5f;
t.getMatrix().setTranslate(countX, countY * 300);

}

//        t.getMatrix().setTranslate((float) (Math.rint(interpolatedTime*10)*10),0);

super.applyTransformation(interpolatedTime, t);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

githan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值