Animations(动画)
一、Animations分为两大类:
(1)Tweened Animations:该类提供了淡入淡出、缩放、旋转、移动动画效果。
(2)Frame--by--Frame Animations:该类可以创建一个Drawable序列,这些Drawable可以按照指定时间一个个显示。
二、Animations的使用:
(1)创建一个AnimationSet对象
(2)根据需要创建相应的Animation对象
(3)为Animation设置相应的数据
(4)将Animation动画放入AnimationSet中
(5)使用控件对动画进行执行AnimationSet中
三、Tweened Animations的通用属性
setDuration:设置动画执行时间
setFillAfter:控制停止在执行结束的状态上
setBefore:动画停止在执行前的状态
setStartOffset:设置动画开始执行的时间
setRepeatCount:设置动画执行的次数
四、Tweened Animations:通过代码来实现按钮的淡入淡出、缩放、旋转、移动动画效果。
xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="淡入淡出"/> <Button android:id="@+id/scale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放"/> <Button android:id="@+id/rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转"/> <Button android:id="@+id/translate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移动"/> </LinearLayout>
活动代码:
public class Sencond extends AppCompatActivity implements View.OnClickListener{ private Button mAlphaBtn; private Button mScaleBtn; private Button mRotateBtn; private Button mTranslateBtn; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sencond); mAlphaBtn = (Button) findViewById(R.id.alpha); mScaleBtn = (Button) findViewById(R.id.scale); mRotateBtn = (Button) findViewById(R.id.rotate); mTranslateBtn = (Button) findViewById(R.id.translate); mAlphaBtn.setOnClickListener(this); mScaleBtn.setOnClickListener(this); mRotateBtn.setOnClickListener(this); mTranslateBtn.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.alpha://淡入淡出 //第一步:创建AnimationSet对象 AnimationSet animationSet1 = new AnimationSet(true); //第二步:创建相应的动画 AlphaAnimation alpha = new AlphaAnimation(1,0); //第三步:设置动画执行的时间 alpha.setDuration(2000); //第四步:将动画放入动画集中 animationSet1.addAnimation(alpha); //第五步:直接作用在控件上 mAlphaBtn.startAnimation(animationSet1); break; case R.id.scale://缩放 AnimationSet animationSet2 = new AnimationSet(true); ScaleAnimation scale = new ScaleAnimation(1,0.1f,1,0.1f, Animation.RELATIVE_TO_SELF,1, Animation.RELATIVE_TO_SELF,1); scale.setDuration(2000); animationSet2.addAnimation(scale); mScaleBtn.startAnimation(animationSet2); break; case R.id.rotate://旋转 AnimationSet animationSet3 = new AnimationSet(true); RotateAnimation rotate = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotate.setDuration(2000); animationSet3.addAnimation(rotate); mRotateBtn.startAnimation(animationSet3); break; case R.id.translate://移动 AnimationSet animationSet4 = new AnimationSet(true); TranslateAnimation translate = new TranslateAnimation( Animation.RELATIVE_TO_PARENT,0f, Animation.RELATIVE_TO_PARENT,1f, Animation.RELATIVE_TO_PARENT,0f, Animation.RELATIVE_TO_PARENT,1f); translate.setDuration(2000); animationSet4.addAnimation(translate); mTranslateBtn.startAnimation(animationSet4); break; } } }
                    
                
                
            
        
浙公网安备 33010602011771号