android Animation开发实例

2025-11-22 15:19:49

1、新建一个android工程,名称AnimationShow,其他参数可以自己配置,之后点击完成,生成项目

android Animation开发实例

android Animation开发实例

2、在main.xml中配置三个按钮和一个图片控件

           <Button 

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:layout_marginTop="10dp"

        android:text="移动动画"/>

          <Button 

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         android:layout_centerInParent="true"

        android:layout_marginTop="10dp"

        android:text="旋转动画"/>

          <Button 

        android:id="@+id/button3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:layout_marginTop="10dp"

        android:text="透明度"/>

 <ImageView  

        android:id="@+id/myImageView"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:layout_gravity="center"  

        android:layout_marginTop="50dp"  

        android:scaleType="matrix"  

        android:src="@drawable/info" >  

    </ImageView>  

android Animation开发实例

3、配置移动动画的配置文件move.xml 在layout文件夹下

<?xml version="1.0" encoding="utf-8"?>  

<set xmlns:android="http://schemas.android.com/apk/res/android" >  

    <translate      

        android:repeatCount="2"      //动画重复次数

        android:fromXDelta="-30"    // 属性为动画起始时 X坐标上的伸缩尺寸 

        android:fromYDelta="-30"  // 属性为动画起始时 Y坐标上的伸缩尺寸   

        android:toXDelta="-80"  //属性为动画结束时 X坐标上的伸缩尺寸         

        android:toYDelta="200"   // 属性为动画结束时 Y坐标上的伸缩尺寸  

        android:duration="3000"      //持续时间

    />      

  

</set>  

android Animation开发实例

4、配置旋转动画的配置文件rotate.xml 在layout文件夹下

<?xml version="1.0" encoding="utf-8"?>  

<set xmlns:android="http://schemas.android.com/apk/res/android">  

<rotate  

      //    加速-动画插入器

        android:interpolator="@android:anim/accelerate_interpolator"  

        android:repeatCount="2"      //重复次数

        android:fromDegrees="0"   //fromDegrees 为动画起始时物件的角度 

        android:toDegrees="+270"     //当角度为正数——表示顺时针旋转 

        android:pivotX="50%"     //为动画相对于物件的X、Y坐标的开始位 

        android:pivotY="50%"      

        android:duration="3000"   //持续时间   

    />     

</set>

5、配置透明度动画配置文件see.xml,实现透明度的展示,在layout文件夹下

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha

        android:duration="1000"   //持续时间

        android:fromAlpha="0.1"  //之前的透明度

        android:repeatCount="infinite"  //有限次数

        android:repeatMode="reverse" //模式反转

        android:toAlpha="1.0" >     //目标透明度

    </alpha>

</set>

android Animation开发实例

6、在主体函数中添加,按钮和事件响应函数,以及对应的动画展示。主要代码:

   imageView=(ImageView)this.findViewById(R.id.myImageView);

        one.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {

// TODO Auto-generated method stub

Animation ani = AnimationUtils.loadAnimation(getApplicationContext(), R.layout.move);  

imageView.startAnimation(ani);  

}

       

        });

android Animation开发实例

android Animation开发实例

android Animation开发实例

7、运行程序,可以看见效果图,依次展示动画的移动,旋转,透明度变化

android Animation开发实例

android Animation开发实例

android Animation开发实例

android Animation开发实例

android Animation开发实例

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢