一、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
2、布局文件
二、总体分为两类:
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;
}
}
{
//定义几个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;
}
}
点击(此处)折叠或打开
- 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=".MainActivity" >
-
- TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- Button
- android:id="@+id/alphaBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:text="alpha动画"
- />
- Button
- android:id="@+id/scaleBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@id/alphaBtn"
- android:text="scale动画"
- />
- Button
- android:id="@+id/rotateBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@id/scaleBtn"
- android:text="rotate动画"
- />
- Button
- android:id="@+id/translateBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@id/rotateBtn"
- android:text="translate动画"
- />
- ImageView
- android:id="@+id/imageViewId"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_centerInParent="true"
- android:layout_marginTop="100dip"
- android:src="@drawable/ic_launcher"
- />
- /RelativeLayout>