神马是Animations
Animations提供一系列动画效果,可用于大多数控件;
1.分类:
TweenedAnimations
包含旋转、移动、缩放、淡入淡出效果
2.Frame-by-Frame Animations
可以创建一个Drawable序列,按照指定的时间一帧帧显示。
第2种使用比较少,这里只讲第一种
具体实现有2种:
1.在xml文件中设置动画,在代码中加载;
2.也可以在代码中生成动画,然后播放。
第一种 xml中配置动画属性
1.旋转
Rotate的xml文件编写方法
50%表示相对于自己,计算旋转的中心点坐标
50%p表示相对父控件,计算旋转中心的点
2.移动
3.缩放
4.透明
res/anim/alpha.xml
具体代码中,如下实现:
在代码中实现
Animations提供一系列动画效果,可用于大多数控件;
1.分类:
TweenedAnimations
包含旋转、移动、缩放、淡入淡出效果
2.Frame-by-Frame Animations
可以创建一个Drawable序列,按照指定的时间一帧帧显示。
第2种使用比较少,这里只讲第一种
具体实现有2种:
1.在xml文件中设置动画,在代码中加载;
2.也可以在代码中生成动画,然后播放。
第一种 xml中配置动画属性
这里我们使用一个ImageView控件做演示,在布局文件描述:
<ImageView
android:id="@+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
1.旋转
Rotate的xml文件编写方法
res/anim/rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
如果android:pivotX ,android:pivotY值是:
50表示绝对定位,相对坐标系的原点计算旋转中心点50%表示相对于自己,计算旋转的中心点坐标
50%p表示相对父控件,计算旋转中心的点
2.移动
res/anim/translate.xml
<translate
android:fromXDelta="50%"
android:toXDelta="50%"
android:fromYDelta="50%"
android:toYDelta="50%"
android:duration="200"/>
3.缩放
res/anim/scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="500"/>
</set>
4.透明
res/anim/alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="2000"/>
</set>
具体代码中,如下实现:
Animation scaleAnimation= null; // 不是ScaleAnimation对象
Animation translateAnimation= null;
Animation rotateAnimation= null;
Animation alphaAnimation = null;
// 这里我们采用按键响应事件
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_UP:
{
// 创建透明像素动画
alphaAnimation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_animation);
imageView.startAnimation(alphaAnimation);
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
{
// 创建缩放动画
scaleAnimation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_animation);
imageView.startAnimation(scaleAnimation);
}
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
{
System.out.println("left.................");
// 创建一个画面位置移动的动画
translateAnimation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.trans_animation);
imageView.startAnimation(translateAnimation);
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
{
System.out.println("right.................");
// 构建一个旋转动画
rotateAnimation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_animation);
imageView.startAnimation(rotateAnimation);
}
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
break;
}
这里仍然使用一个ImageView控件,在布局文件描述:
<ImageView
android:id="@+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
在代码中实现
// 先获取控件
imageView = (ImageView)findViewById(R.id.img_view);
// 各种动画声明
ScaleAnimation scaleAnimation= null;
TranslateAnimation translateAnimation= null;
RotateAnimation rotateAnimation= null;
AlphaAnimation alphaAnimation = null;
// 这里我们采用按键响应事件
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_UP:
{
// 创建透明像素动画,时间3s
alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(3000);
imageView.startAnimation(alphaAnimation);
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
{
// 创建缩放动画,时间0.5s
scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(500);
imageView.startAnimation(scaleAnimation);
}
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
{
System.out.println("left.................");
// 创建一个画面位置移动的动画
AnimationSet animationset = new AnimationSet(true);
translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.3f, // 注意参数
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.4f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(2000);
animationset.addAnimation(translateAnimation);
imageView.startAnimation(animationset);
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
{
System.out.println("right.................");
// 构建一个旋转动画
AnimationSet animationset = new AnimationSet(true);
rotateAnimation = new RotateAnimation(0.0f, 360.0f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, // 注意参数是百分比
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
animationset.addAnimation(rotateAnimation);
imageView.startAnimation(animationset);
}
}