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
- Android动画--视图动画
- android-属性动画、视图动画
- Android动画机制 视图动画
- Android动画之视图动画
- Android动画之视图动画
- Android 动画之视图动画
- android视图动画
- android视图动画
- Android视图动画
- Android-视图动画
- Android中的视图动画
- Android视图动画
- Android View 视图动画
- Android视图动画浅析
- Android视图动画---View Animation
- Android视图动画(View Animation)
- Android视图动画框架详解
- Android中视图动画详解
- 第8周OJ项目5 打印数字图形
- Qt/C++ 跑马灯效果~
- VS2010中C#实现远程调试功能(转)
- 【POJ 3041 Asteroids】+ 二分图
- 第11节--function(上)
- Android-视图动画
- 【C/C++开发】__stdcall,__cdecl,__fastcall的区别
- JDK1.7 深入理解 LinkedHashMap
- IOS 省市区三级联动
- android开发中handler的各种用法实践总结
- Git 与 SVN 区别
- leetcode-24 Swap Nodes in Pairs
- 项目访问跳转
- Android-属性动画