Android开发之Animaions(一 )
来源:互联网 发布:windows查看网络端口 编辑:程序博客网 时间:2024/06/06 07:47
什么是Animations?
使用Animations可以实现动画效果。
Animations分为两大类:
第一类:Tweened Animations
该类Animations提供了旋转、移动、伸展和淡出等效果。
Tweened Animations的分类:
1.Alpha:淡入淡出效果
2.Scale:缩放效果
3.Rotate:旋转效果
4.Translate:移动效果
第二类:Frame-by-Frame Animations
这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。
Animations的第一种使用方法(在代码中实现)
1.创建一个AnimationSet对象
2.根据需要创建相应的Animation对象
3.根据软件动画的需求,为Animation对象设置相应的数据
4.将Animation对象添加到AnimationSet对象当中
5.使用控件对象开始执行AnimationSet
Tween Animations的通用属性:
setDuration(long durationMills):设置动画持续时间(单位:毫秒)
setFillAfter(boolean fillAfter):如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
setFillBefore(boolean fillBefore):如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
setStartOffSet(long startOffSet):设置动画执行之前的等待时间
setRepeatCount(int repeatCount):设置动画重复执行的次数(注意:设置ScaleAnimation对象的循环次数才能实现循环效果,设置AnimationSet对象的循环次数不能实现动画循环效果)
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.mycompany.animations.MainActivity"> <ImageView android:id="@+id/imageView" android:src="@drawable/bigdog" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:orientation="vertical" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/alpha" android:text="淡入淡出动画效果" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/scale" android:text="缩放动画效果" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/rotate" android:text="旋转动画效果" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/translate" android:text="移动效果" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout></RelativeLayout>
MainActivity.java:
package com.mycompany.animations;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.app.NotificationCompat;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageButton;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView imageView; private Button alphaButton; private Button scaleButton; private Button rotateButton; private Button translateButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); alphaButton = (Button) findViewById(R.id.alpha); scaleButton = (Button) findViewById(R.id.scale); rotateButton = (Button) findViewById(R.id.rotate); translateButton = (Button) findViewById(R.id.translate); alphaButton.setOnClickListener(new AlphaButtonListener()); rotateButton.setOnClickListener(new RotateButtonListener()); scaleButton.setOnClickListener(new ScaleButtonListener()); translateButton.setOnClickListener(new TranslateButtonListener()); } class AlphaButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { // 创建一个AnimationSet对象 AnimationSet animationSet = new AnimationSet(true); // 创建一个AlphaAnimation对象,并通过构造函数形参设置透明度 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); // 设置动画持续时间 animationSet.setDuration(2000); // 将AlphaAnimation对象加入到AnimationSet对象中 animationSet.addAnimation(alphaAnimation); // 将动画效果显示在ImageView对象上 imageView.startAnimation(animationSet); } } class ScaleButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); // 创建ScaleAnimation对象并设置缩放程度和缩放中心 ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animationSet.setDuration(1500); // 设置循环次数为无限循环 // 需要注意的是:设置ScaleAnimation对象的循环次数才能实现循环效果, // 设置AnimationSet对象的循环次数不能实现动画循环效果 scaleAnimation.setRepeatCount(Animation.INFINITE); animationSet.addAnimation(scaleAnimation); imageView.startAnimation(animationSet); } } class RotateButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); // 创建RotateAnimation对象并通过形参设置旋转中心 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 1); animationSet.setDuration(1500); animationSet.addAnimation(rotateAnimation); imageView.startAnimation(animationSet); } } class TranslateButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); // 创建TranslateAnimation对象并设置移动轨迹 TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1); animationSet.setDuration(1500); animationSet.addAnimation(translateAnimation); imageView.startAnimation(animationSet); } }}
0 0
- Android开发之Animaions(一 )
- Android游戏开发之旅(一)
- Android开发之Animation(一)
- Android开发之AppWidget应用(一)
- Android开发之AppWidget应用(一)
- Android 开发之路(一)
- android之wifi开发(一)
- Android开发之AppWidget应用(一)
- Android开发之handler(一)
- Android NDK 开发之(一) 管中窥豹
- android之wifi开发(一)
- android之wifi开发(一)
- Android开发之SQLite (一)
- android之wifi开发(一)
- 漫谈android开发之菜单(一)
- android之wifi开发(一)
- android之wifi开发(一)
- android之wifi开发(一)
- 初始化一个Django项目
- Centos7上搭建SVN服务器并实现自动同步至web目录
- 常见的设计模式与原则
- float布局:高度塌陷以及解决办法
- JQuery 小技巧大全
- Android开发之Animaions(一 )
- TextView 小技巧
- HDU 2955 Robberies by Assassin
- 史上最严重数据泄露:5亿雅虎账户于2014年被盗
- H5基础——(5)怪异盒模型、弹性盒模型、倒影及阴影
- poj 1679
- 2017校招滴滴笔试编程题(深搜+剪枝)
- 写一函数free,将上题用new函数占用的空间释放。Free(p)表示将p指向的单元以后的内存段释放
- HDU-2022