安卓 Animation一

Animation 是实现动画效果的API,这些动画效果可以应用到多种控件,动画分Tweened Animation和Franme-by-Frame Animation,实现方式为代码中实现动画或者是XML中实现动画。Animation   这是动画的抽象类,其他的几个实现的动画类继承这个类AnimationSet这是动画的属性的集合类AnimationUtils   这个类是定义动画工具类

Tweened有四类:

1、Alpha:淡入淡出 透明度变化,两个float参数,第一个是开始透明度,第二个是结束透明度,取值范围是(0.0到1.0,;0.0代表不透明,1.0代表完全透明)

2、scale:缩放(fromDegrees:开始旋转的角度,.toDegrees:结束旋转的角度
                            .pivotXType:在X坐标方向的伸缩的方式,pivotYType:在Y坐标方向的伸缩方式;
                             pivotXValue:在X坐标方向的伸缩值,pivotYValue:在Y坐标方向的伸缩值)

                      参数说明:fromX: 起始的X坐标,toX:结束的X坐标,fromY:起始的Y坐标,toY:结束的Y坐标
                            .pivotXType:在X坐标方向的伸缩的方式,pivotYType:在Y坐标方向的伸缩方式;                     
                             pivotXValue:在X坐标方向的伸缩值,pivotYValue:在Y坐标方向的伸缩值

3、Roate:旋转float fromDegrees, float toDegrees, float pivotX, float pivotY,fromDegrees:开始旋转的角度,.toDegrees:结束旋转的角度
                            .pivotXType:在X坐标方向的伸缩的方式,pivotYType:在Y坐标方向的伸缩方式;
                             pivotXValue:在X坐标方向的伸缩值,pivotYValue:在Y坐标方向的伸缩值

4、Translate:移动,四个参数float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值;

    float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值;

  float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值;

  float toYDelta)这个参数表示动画开始的点离当前View Y坐标上的差值;

实现步骤:

1、在布局文件设置Button按钮和imageView

2、在mainAcitivity创建AnimationSet对象,

3、根据需要建立相应的Animation对象

4、根据软件动画需求为Animation设置数据

5、将Animation对象添加到AnimationSet对象

6、使用imagevIew.start启动动画

代码如下:

layout的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button android:id="@+id/scaleButtonId" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_alignParentBottom="true"
  android:text="测试scale动画效果" />
     <Button android:id="@+id/rotateButtonId" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_above="@id/scaleButtonId"
  android:text="测试rotate动画效果" />
       <Button android:id="@+id/alphaButtonId" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_above="@id/rotateButtonId"
  android:text="测试alpha动画效果" />
       <Button android:id="@+id/translateButtonId" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_above="@id/alphaButtonId"
  android:text="测试translate动画效果" />
     <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent">

  <ImageView android:id="@+id/imageViewId"
   android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:layout_centerInParent="true" android:layout_marginTop="100dip"
   android:src="@drawable/kaola" />
</LinearLayout>

</RelativeLayout>

这里图片是自己天添加到drawable里面的

mainAcitivity

public class MainActivity extends Activity {

/** Called when the activity is first created. */
private ImageView imageView = null;
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       imageView = (ImageView)findViewById(R.id.imageViewId);
      
       rotateButton = (Button) findViewById(R.id.rotateButtonId);
  rotateButton.setOnClickListener(new RotateButtonListener());

  scaleButton = (Button) findViewById(R.id.scaleButtonId);
  scaleButton.setOnClickListener(new ScaleButtonListener());
 
  alphaButton = (Button) findViewById(R.id.alphaButtonId);
  alphaButton.setOnClickListener(new AlphaButtonListener());
 
  translateButton = (Button) findViewById(R.id.translateButtonId);
  translateButton.setOnClickListener(new TranslateButtonListener());
    }
   
    private class  AlphaButtonListener implements OnClickListener{
     @Override
     public void onClick(View view){
      AnimationSet animationSet = new AnimationSet(true);
      AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.1f);
      alphaAnimation.setDuration(1000);
   animationSet.addAnimation(alphaAnimation);
   imageView.startAnimation(animationSet);
     }
    }
    private class TranslateButtonListener implements OnClickListener{
     @Override
     public void onClick(View view){
      AnimationSet animationSet = new AnimationSet(true);
      TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
     Animation.RELATIVE_TO_SELF, 0.5f,
     Animation.RELATIVE_TO_SELF, 0f,
     Animation.RELATIVE_TO_SELF, 1.0f);
      translateAnimation.setDuration(2000);
      animationSet.addAnimation(translateAnimation);
      imageView.startAnimation(animationSet);
     }
    }
    private class RotateButtonListener implements OnClickListener {

  @Override
  public void onClick(View view) {
   AnimationSet animationSet = new AnimationSet(true);
   RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
     Animation.RELATIVE_TO_PARENT, 1f,
     Animation.RELATIVE_TO_PARENT, 0f);
   rotateAnimation.setDuration(5000);
   animationSet.addAnimation(rotateAnimation);
   imageView.startAnimation(animationSet);
  }
}
   
    private class ScaleButtonListener implements OnClickListener{
     @Override
     public void onClick(View view){
      AnimationSet animationSet = new AnimationSet(true);
      ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
     Animation.RELATIVE_TO_SELF, 0.5f,
     Animation.RELATIVE_TO_SELF, 0.5f);
     }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
}


public class MainActivity extends Activity {

/** Called when the activity is first created. */
private ImageView imageView = null;
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       imageView = (ImageView)findViewById(R.id.imageViewId);
      
       rotateButton = (Button) findViewById(R.id.rotateButtonId);
  rotateButton.setOnClickListener(new RotateButtonListener());

  scaleButton = (Button) findViewById(R.id.scaleButtonId);
  scaleButton.setOnClickListener(new ScaleButtonListener());
 
  alphaButton = (Button) findViewById(R.id.alphaButtonId);
  alphaButton.setOnClickListener(new AlphaButtonListener());
 
  translateButton = (Button) findViewById(R.id.translateButtonId);
  translateButton.setOnClickListener(new TranslateButtonListener());
    }
   
    private class  AlphaButtonListener implements OnClickListener{
     @Override
     public void onClick(View view){
      AnimationSet animationSet = new AnimationSet(true);
      AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.1f);
      alphaAnimation.setDuration(1000);
   animationSet.addAnimation(alphaAnimation);
   imageView.startAnimation(animationSet);
     }
    }
    private class TranslateButtonListener implements OnClickListener{
     @Override
     public void onClick(View view){
      AnimationSet animationSet = new AnimationSet(true);
      TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
     Animation.RELATIVE_TO_SELF, 0.5f,
     Animation.RELATIVE_TO_SELF, 0f,
     Animation.RELATIVE_TO_SELF, 1.0f);
      translateAnimation.setDuration(2000);
      animationSet.addAnimation(translateAnimation);
      imageView.startAnimation(animationSet);
     }
    }
    private class RotateButtonListener implements OnClickListener {

  @Override
  public void onClick(View view) {
   AnimationSet animationSet = new AnimationSet(true);
   RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
     Animation.RELATIVE_TO_PARENT, 1f,
     Animation.RELATIVE_TO_PARENT, 0f);
   rotateAnimation.setDuration(5000);
   animationSet.addAnimation(rotateAnimation);
   imageView.startAnimation(animationSet);
  }
}
   
    private class ScaleButtonListener implements OnClickListener{
     @Override
     public void onClick(View view){
      AnimationSet animationSet = new AnimationSet(true);
      ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
     Animation.RELATIVE_TO_SELF, 0.5f,
     Animation.RELATIVE_TO_SELF, 0.5f);
     }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值