/** * 作者:David Zheng on 2015/11/7 15:38 * *网站:http://www.93sec.cc * *微博:http://weibo.com/mcxiaobing * *微博:http://weibo.com/93sec.cc */ 个人交流QQ986945193
一、分类:
(一)、概要:
- 属性动画:Property Animation
- 帧动画:Frame Animation (Drawable Animation)
- 补间动画:Tween Animation (View Animation)
- 透明度补间动画
- 缩放补间动画
- 旋转补间动画
- 移动补间动画
public class MainActivity extends Activity {
private ImageView imageView_main;
private Animation animation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
}
public void clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_alpha:
animation = new AlphaAnimation(0.0f, 1.0f);
break;
case R.id.button_main_scale:
animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
break;
case R.id.button_main_translate:
animation = new TranslateAnimation(0, 150, 0, 0);
break;
case R.id.button_main_rotate:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
break;
default:
break;
}
animation.setDuration(3000);
imageView_main.setAnimation(animation);
}
}
一、res/anim/frame_animation.xml的代码:
<animation-listxmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <itemandroid:drawable="@drawable/anim1"android:duration="50"/> <itemandroid:drawable="@drawable/anim2"android:duration="50"/> <itemandroid:drawable="@drawable/anim3"android:duration="50"/> <itemandroid:drawable="@drawable/anim4"android:duration="50"/> <itemandroid:drawable="@drawable/anim5"android:duration="50"/> <itemandroid:drawable="@drawable/anim6"android:duration="50"/> <itemandroid:drawable="@drawable/anim7"android:duration="50"/> <itemandroid:drawable="@drawable/anim8"android:duration="50"/> <itemandroid:drawable="@drawable/anim9"android:duration="50"/> <itemandroid:drawable="@drawable/anim10"android:duration="50"/> <itemandroid:drawable="@drawable/anim11"android:duration="50"/> <itemandroid:drawable="@drawable/anim12"android:duration="50"/> </animation-list>
二、MainActivity.java代码:
public class MainActivity extends Activity {
private ImageView imageView_main_show;
private AnimationDrawable animationDrawable = null;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main_show = (ImageView) findViewById(R.id.imageView_main_show);
imageView_main_show.setBackgroundResource(R.anim.frame_animation);
animationDrawable = (AnimationDrawable) imageView_main_show.getBackground();
}
public void clickButton(View
view) {
switch (view.getId()) {
case R.id.button_main_start:
if (!animationDrawable.isRunning()) {
//一组动画是否只播放一次
animationDrawable.setOneShot(false);
animationDrawable.start();
}
break;
case R.id.button_main_stop:
if (animationDrawable.isRunning()) {
animationDrawable.stop();
}
break;
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if (!animationDrawable.isRunning()) {
animationDrawable.setOneShot(false);
animationDrawable.start();
}
}
}
- Duration动画的持续时间,默认300ms。
- Time interpolation:时间插值。LinearInterpolator、AccelerateDecelerateInterpolator,定义动画的变化率。
- Repeat count and behavior:重复次数、以及重复模式;可以定义重复多少次;重复时从头开始,还是反向。
- Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。
- Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默认为10ms,但最终依赖系统的当前状态;基本不用管。
- ObjectAnimator 动画的执行类(常用属性:alpha,rotation,rotationX,rotationY,translationX,translationY,scaleX,scaleY)
- ValueAnimator 动画的执行类
- AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
- AnimatorInflater 用户加载属性动画的xml文件
- TypeEvaluator 类型估值,主要用于设置动画操作属性的值。
- TimeInterpolator 时间插值
- 总的来说,属性动画就是,动画的执行类来设置动画操作的对象的属性、持续时间,开始和结束的属性值,时间差值等,然后系统会根据设置的参数动态的变化对象的属性。
一、res/anim/property_anim.xml的代码:
<setxmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <objectAnimator android:duration="4000" android:propertyName="x" android:valueTo="300" android:valueType="intType"/> <objectAnimator android:duration="4000" android:propertyName="y" android:valueTo="400" android:valueType="intType"/> <objectAnimator android:duration="4000" android:propertyName="x" android:valueTo="0" android:valueType="intType"/> <objectAnimator android:duration="4000" android:propertyName="y" android:valueTo="0" android:valueType="intType"/> </set>
二、MainActivity.java代码:
public class MainActivity extends Activity
{
private ImageView imageView_main_obj;
private Move move;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main_obj = (ImageView) findViewById(R.id.imageView_main_obj);
move = new Move();
imageView_main_obj.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
setTitle(move.getX() + ":" + move.getY());
}
});
}
class Move {
private int y;
private int x;
publicint getY() {
return y;
}
public void setY(int y)
{
this.y = y;
imageView_main_obj.layout(imageView_main_obj.getLeft(), y,imageView_main_obj.getRight(),y
+ imageView_main_obj.getMeasuredHeight());
}
public int getX() {
return x;
}
publi cvoid setX(int x)
{
this.x = x;
imageView_main_obj.layout(x, imageView_main_obj.getTop(), x + imageView_main_obj.getMeasuredWidth(),imageView_main_obj.getBottom());
}
}
public void clickButton(View
view) {
// 装载属性动画资源
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this,
R.animator.property_anim);
// 设置要控制的对象
set.setTarget(move);
// 开始动画
set.start();
}
}
【备注说明:】
<objectAnimator android:duration="4000" android:propertyName="x" android:valueTo="300" android:valueType="intType"/>
- android:ordering说明一系列动画动作的执行顺序,有两个选择: sequentially 和together,顺序执行还是一起执行;
- objectAnimator 是设定动画实施的对象;
- duration是该动画动作执行从开始到结束所用的时间;
- android:repeatCount="infinite" 可以是整数或者infinite
- android:repeatMode="restart" 可以是restart 或者 reverse
- android:valueFrom=" " 整数|浮点数|颜色
2015/11/7 15:38 * *
* *
* *
*/ 个人交流QQ986945193