Android-视图动画

来源:互联网 发布:js url base64编码 编辑:程序博客网 时间:2024/05/21 14:52

1) AlphaAnimation

代码实现AlphaAnimation

@Overridepublic 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() {@Overridepublic void onClick(View v) {            AlphaAnimation aa = new AlphaAnimation(0,1);aa.setDuration(2000);v.startAnimation(aa);}    });    return root;}

XML实现AlphaAnimation

@Overridepublic 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() {@Overridepublic void onClick(View v) {//xml实现alphaAnimationv.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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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">    <alphaandroid:fromAlpha="0"android:toAlpha="1" />    <translateandroid: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() {@Overridepublic void onAnimationStart(Animation animation) {//动画开始时}@Overridepublic void onAnimationEnd(Animation animation) {        Toast.makeText(getContext(),"Animatioin End",Toast.LENGTH_SHORT).show();//动画结束时}@Overridepublic 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