android学习之Animation(一)

简介: 一、Animations:提供一系列的动画效果,可以应用在绝大多数控件二、总体分为两类:     1、Tween Aniomations 提供旋转、移动、伸展、淡出等效果     2、Freme-by-Frame Animations 创建一个Drawable序列,这些序列按照一...
一、Animations:提供一系列的动画效果,可以应用在绝大多数控件
二、总体分为两类:
    1、Tween Aniomations 提供旋转、移动、伸展、淡出等效果
    2、Freme-by-Frame Animations 创建一个Drawable序列,这些序列按照一定的时间间隔逐个显示
三、 TweenAnimations分类:
    1、Alpha 淡入淡出
    2、Scale 缩放效果
    3、Rotate 旋转效果
    4、Translate 移动效果
四、使用animation的步骤
    1、创建一个AnimationSet对象
    2、根据需要创建对应的Animation对象
    3、根据动画的需求为Animation对象设置数据
    4、将Animation对象添加到AnimationSet当中
    5、使用控件对象执行AnimationSet
五、TweenAnimation的通用属性
    1、setFillAfter(boolean fillAfter);   当fillAfter为true,控件停留在动画执行结束的状态
    2、setFillBefore(boolean fillBefore); 当fillBefore为true,控件停留在动画执行之前的状态
    3、setStartOffSet(long startOffSet); 设置动画执行前等待的时间
    4、setRepeatCount(int repeatCount); 设置动画重复的次数
    5、setDuration(long durationMills); 设置动画执行的时间
六、Animation的实现方法
    1、在代码中实现
    2、在xml文件中实现
七、在代码中实现Animation
    1、MainActicity.java

点击(此处)折叠或打开

public class MainActivity extends Activity
{
    //定义几个button
    private Button alphaButton = null;
    private Button scaleButton = null;
    private Button rotateButton = null;
    private Button translateButton = null;
    private ImageView imageView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到控件
        alphaButton = (Button)findViewById(R.id.alphaBtn);
        scaleButton = (Button)findViewById(R.id.scaleBtn);
        rotateButton = (Button)findViewById(R.id.rotateBtn);
        translateButton= (Button)findViewById(R.id.translateBtn);
        imageView = (ImageView)findViewById(R.id.imageViewId);
        //为控件添加事件
        alphaButton.setOnClickListener(new btnListener());
        scaleButton.setOnClickListener(new btnListener());
        rotateButton.setOnClickListener(new btnListener());
        translateButton.setOnClickListener(new btnListener());
    }
    
    class btnListener implements OnClickListener
    {

        @Override
        public void onClick(View v)
        {
            //创建AnimationSet对象
            AnimationSet animationSet = new AnimationSet(true);
            // TODO Auto-generated method stub
            switch(v.getId())
            {
                case R.id.alphaBtn:
                    //透明效果
                    //创建一个alphaAnimation,从完全透明到完全不透明
                    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
                    //动画执行的时间,单位ms
                    alphaAnimation.setDuration(1000);
                    
                    //将animation添加到AnimationSet对象中
                    animationSet.addAnimation(alphaAnimation);
                    //开始执行动画
                    imageView.startAnimation(animationSet);
                    break;
                case R.id.scaleBtn:
                    //缩放效果
                    //创建ScaleAnimation,横纵都是从原来缩小到10%
                    ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
                                Animation.RELATIVE_TO_SELF, 0.5f,
                                Animation.RELATIVE_TO_SELF, 0.5f);
                    //动画执行的时间,单位ms
                    scaleAnimation.setDuration(3000);
                    //将animation添加到AnimationSet对象中
                    animationSet.addAnimation(scaleAnimation);
                    //开始执行动画
                    imageView.startAnimation(animationSet);
                    break;
                case R.id.rotateBtn:
                    /*旋转效果
                    第一个参数是起始角度(12点位置是0度)
                    第二个参数是停止角度
                    第三个参数旋转圆心横坐标类型
                    第四个参数旋转圆心横坐标大小
                    第五个参数旋转圆心纵坐标类型
                    第六个参数旋转圆心纵坐标大小
                    Animation.RELATIVE_TO_PARENT相对于父控件的坐标
                    Animation.RELATIVE_TO_SELF相对于自身的坐标
                    手机的原点在左上角
                    */
                    RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
                                                        Animation.RELATIVE_TO_PARENT, 1f,
                                                        Animation.RELATIVE_TO_PARENT, 0f);
                    //设置动画时间
                    rotateAnimation.setDuration(3000);
                    //将animation添加到AnimationSet对象中
                    animationSet.addAnimation(rotateAnimation);
                    //开始执行动画
                    imageView.startAnimation(animationSet);
                    break;
                case R.id.translateBtn:
                    TranslateAnimation translateAnimation = new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF, 0f,
                            Animation.RELATIVE_TO_SELF, 0.5f,
                            Animation.RELATIVE_TO_SELF, 0f,
                            Animation.RELATIVE_TO_SELF, 0.5f
                            );
                    //设置动画时间
                    translateAnimation.setDuration(3000);
                    //将animation添加到AnimationSet对象中
                    animationSet.addAnimation(translateAnimation);
                    //开始执行动画
                    imageView.startAnimation(animationSet);
                    break;
                default:
                        break;
            }
            
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
    2、布局文件

点击(此处)折叠或打开

  1. RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="@string/hello_world" />
  14.     Button
  15.         android:id="@+id/alphaBtn"
  16.         android:layout_width="fill_parent"
  17.         android:layout_height="wrap_content"
  18.         android:layout_alignParentBottom="true"
  19.         android:text="alpha动画"
  20.         />
  21.     Button
  22.         android:id="@+id/scaleBtn"
  23.         android:layout_width="fill_parent"
  24.         android:layout_height="wrap_content"
  25.         android:layout_above="@id/alphaBtn"
  26.         android:text="scale动画"
  27.         />
  28.     Button
  29.         android:id="@+id/rotateBtn"
  30.         android:layout_width="fill_parent"
  31.         android:layout_height="wrap_content"
  32.         android:layout_above="@id/scaleBtn"
  33.         android:text="rotate动画"
  34.         />
  35.     Button
  36.         android:id="@+id/translateBtn"
  37.         android:layout_width="fill_parent"
  38.         android:layout_height="wrap_content"
  39.         android:layout_above="@id/rotateBtn"
  40.         android:text="translate动画"
  41.         />
  42.     ImageView
  43.         android:id="@+id/imageViewId"
  44.         android:layout_height="wrap_content"
  45.         android:layout_width="wrap_content"
  46.         android:layout_centerInParent="true"
  47.         android:layout_marginTop="100dip"
  48.         android:src="@drawable/ic_launcher"
  49.         />
  50. /RelativeLayout>
相关文章
|
15天前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
105 0
|
15天前
|
安全 Linux Android开发
Android安全启动学习(一):AVB校验是什么?
Android安全启动学习(一):AVB校验是什么?
151 0
|
15天前
|
存储 安全 Linux
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
152 0
|
14天前
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
13天前
|
存储 定位技术 开发工具
Android 开发前的设计,Android之内存泄漏调试学习与总结
Android 开发前的设计,Android之内存泄漏调试学习与总结
|
13天前
|
算法 安全 Java
2024年Android最新知识体系最强总结(全方面覆盖Android知识结构,BAT面试&学习进阶)
2024年Android最新知识体系最强总结(全方面覆盖Android知识结构,BAT面试&学习进阶)
|
14天前
|
Java Android开发 Dart
50家大厂面试万字精华总结android编程基础学习
50家大厂面试万字精华总结android编程基础学习
|
14天前
|
Java Android开发 消息中间件
flutter面试,字节大牛教你手撕Android学习
flutter面试,字节大牛教你手撕Android学习
|
15天前
|
网络协议 Shell Android开发
Android 深入学习ADB调试原理(1)
Android 深入学习ADB调试原理(1)
36 1
|
15天前
|
缓存 网络协议 编译器
针对Android系统工程师的C/C++学习目录
针对Android系统工程师的C/C++学习目录
11 0