【网学网提醒】:网学会员编辑为广大网友搜集整理了:Android动画详细探究(An-Beer工作室)绩等信息,祝愿广大网友取得需要的信息,参考学习。
Android动画详细探究
Animation是一个用于View,Surfaces和其它对象实现动画效果的抽象类,其中常用的类是TranslateAnimation用于控制位置的改变一下列出一些重要的属性和方法Xml属性android:duration:运行动画的时间android:interpolator:定义用于平滑动画运动的时间内插android:repeatCount:定义动画重复的时间方法:set:RepeatCount(int):定义动画重复的时间setRepeatMode(int):通过设置重复时间定义动画的行为setStartOffset(long):以毫秒为单位的动画运行前的延迟,一旦开始时间就达到了Cancel():取消动画hasStarted():判断动画是否已在运行initialize(intwidth,intheight,intparentWidth,intparentHeight):初始化动画reset():重置动画Start()启动动画其中还有一些常量RESTART:重新运行INFINITE:永无终止地运行
将动画用于指定的控件,所有继承自View的控件都有startAnimation(Animation)方法,通过调用此方法来应用动画于控件
AnimationUtils类介绍为应用动画提供了通用的的方法,它有一个很重要的方法loadAnimation(Context,Animation)用于加载Animation的实例。
下面是一个实例源码
下载 Animationpage.xml是布局文件
android:layout_height="wrap_content"android:textSize="26sp"android:text="震荡"/>
android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dip"android:text="请选择你喜欢的人"/>
tViewandroid:id="@+id/target"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="26sp"android:text="java语言"/>
Animlayout.xml是动画文件 android:interpolator="@anim/cycle_7"/>
。。。
主程序packagewjq.WidgetDemo;
importandroid.app.Activity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.animation.Animation;importandroid.view.animation.AnimationUtils;importandroid.view.animation.Transformation;importandroid.view.animation.TranslateAnimation;importandroid.widget.AdapterView;importandroid.widget.ArrayAdapter;importandroid.widget.Spinner;importandroid.widget.ViewFlipper;importandroid.widget.AdapterView.OnItemSelectedListener;
/***动画Animation示例*@author记忆的永恒**/publicclassAnimationDemoextendsActivityimplementsOnClickListener,OnItemSelectedListener{
privateViewv;privateString[]mStrings={"向上","向右","穿越","旋转"};
privatestaticfinalString[]INTERPOLATORS={"加速","Decelerate","减速","左右","Overshoot",
"Anticipate/Overshoot","弹回"};privateViewFlippermFlipper;privateSpinnerspinner;privateSpinnerspinner1;privateArrayAdapteraa;privateArrayAdapteraa1;/**(non-Javadoc)**@seeandroid.app.Activity#onCreate(android.os.Bundle)*/@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.animationpage);v=findViewById(R.id.login);v.setOnClickListener(this);mFlipper=(ViewFlipper)findViewById(R.id.flipper);
//反转mFlipper.startFlipping();
spinner=(Spinner)findViewById(R.id.spinner);aa=newArrayAdapter(this,android.R.layout.simple_spinner_item,mStrings);aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner.setAdapter(aa);spinner.setOnItemSelectedListener(this);
spinner1=(Spinner)findViewById(R.id.spinner1);aa1=newArrayAdapter(this,android.R.layout.simple_spinner_item,INTERPOLATORS);aa1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner1.setAdapter(aa1);spinner1.setOnItemSelectedListener(this);}
@OverridepublicvoidonClick(Viewv){Animationshake=AnimationUtils.loadAnimation(this,R.anim.animlayout);findViewById(R.id.pw).startAnimation(shake);}
@OverridepublicvoidonItemSelected(Adapte
rView>parent,Viewview,intposition,longid){if(parent.getAdapter()==aa){switch(position){
case0:mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_up_in));mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_up_out));break;case1:mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));break;case2:mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));break;default:mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.hyperspace_in));mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.hyperspace_out));break;
}}
else{finalViewtarget=findViewById(R.id.target);finalViewtargetParent=(View)target.getParent();Animationanm=newTranslateAnimation(0.0f,targetParent.getWidth()-target.getWidth()-targetParent.getPaddingLeft()-targetParent.getPaddingRight(),0.0f,0.0f);
anm.setDuration(1000);anm.setStartOffset(300);anm.setRepeatMode(Animation.RESTART);anm.setRepeatCount(Animation.INFINITE);
switch(position){case0:anm.setInterpolator(AnimationUtils.loadInterpolator(this,android.R.anim.accelerate_interpolator));break;case1:anm.setInterpolator(AnimationUtils.loadInterpolator(this,android.R.anim.decelerate_interpolator));break;case2:anm.setInterpolator(AnimationUtils.loadInterpolator(this,android.R.anim.accelerate_decelerate_interpolator));break;case3:anm.setInterpolator(AnimationUtils.loadInterpolator(this,android.R.anim.anticipate_interpolator));break;case4:anm.setInterpolator(AnimationUtils.loadInterpolator(this,android.R.anim.overshoot_interpolator));
break;case5:anm.setInterpolator(AnimationUtils.loadInterpolator(this,android.R.anim.anticipate_overshoot_interpolator));break;case6:anm.setInterpolator(AnimationUtils.loadInterpolator(this,android.R.anim.bounce_interpolator));break;}
target.startAnimation(anm);}
}
@OverridepublicvoidonNothingSelected(AdapterViewparent){//TODOAuto-generatedmethodstub
}
}