补间动画基础备忘(1)
【摘要】
补间动画就是我们只需要指定动画的第一帧和最后一帧,其能够自动生成中间图像的一种动画。
Android SDK提供了4种补间动画效果:移动、缩放、旋转、透明度
移动补间动画:
移动是最常见的动画效果.我们可以通过配置动画文件(xml文件)或Java代码来实现补间动画的移动效果.
通过动画文件的方式:
补间动画文件需要放在re...
补间动画就是我们只需要指定动画的第一帧和最后一帧,其能够自动生成中间图像的一种动画。
Android SDK提供了4种补间动画效果:移动、缩放、旋转、透明度
移动补间动画:
移动是最常见的动画效果.我们可以通过配置动画文件(xml文件)或Java代码来实现补间动画的移动效果.
通过动画文件的方式:
补间动画文件需要放在res\anim目录中.在动画文件中通过<translate>标签设置移动效果.
xml代码:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="0"
android:toYDelta="260" />
android:interpolator:表示动画渲染器。通过android:interpolator属性可以设置3个动画渲染器:
accelerate_interpolator(动画加速器)、decelerate_interpolator(动画减速器)和accelerate_decelerate_interpolator(动画加速减速器)。
动画加速器使动画在开始时速度最慢,然后逐渐加速。
动画减速器使动画在开始时速度最快,然后逐渐减速。
动画加速减速器使动画在开始和结束时速度最慢,但在前半部分时开始加速,在后半部分时开始减速。
android:fromXDelta:动画起始位置的横坐标。
android:toXDelta:动画结束位置的横坐标。
android:fromXDelta:动画起始位置的纵坐标。
android:toYDelta:动画结束位置的纵坐标。
android:duration:动画的持续时间。单位是毫秒。也就是说,动画要在android:duration属性指定的时间内从起始点移动到结束点。
装载补间动画文件需要使用android.view.animation.AnimationUtils. loadAnimation方法,
public static Animation loadAnimation(Context context, int id)
(1)使用EditText类的startAnimation方法
editText.startAnimation(animation);
(2)使用Animation类的start方法
editText.setAnimation(animation);
animation.start();
animation.setRepeatCount(Animation.INFINITE);
Java代码的方式
如果想通过Java代码实现移动补间动画,可以创建TranslateAnimation对象。其构造方法的定义如下:
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);
setInterpolator:设置动画渲染器。该方法的参数类型是Interpolator,在Android SDK中提供了一些动画渲染器,例如LinearInterpolator、AccelerateInterpolator等,其中部分动画渲染器可以再动画文件的<translate>标签android:interpolator属性中进行设置,而有的动画渲染器需要使用Java代码 。
setDuration:设置动画的持续时间。该方法相当于设置了<translate>的android:duration属性。
补间动画有3个状态:动画开始、动画结束、动画循环。要想监听这3个状态,需要实现AnimationListener接口。该接口定义了3个方法:onAnimationStart、onAnimationEnd、onAnimationRepeat,这3个方法分别在动画开始、动画结束和动画循环时调用。
解释:
<translate android:fromXDelta="100%p"
android:toXDelta="0"
/>
100%p相对于父view,在屏幕坐标系内from to 方向来决定正负
示例代码:
public class Main extends Activity implements OnClickListener,
AnimationListener
{
private EditText editText;
private ImageView imageView;
private Animation animationRight;
private Animation animationBottom;
private Animation animationTop;
@Override
public void onAnimationEnd(Animation animation)
{
if (animation.hashCode() == animationBottom.hashCode())
imageView.startAnimation(animationTop);
else if (animation.hashCode() == animationTop.hashCode())
imageView.startAnimation(animationBottom);
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationStart(Animation animation)
{
}
@Override
public void onClick(View view)
{
// editText.startAnimation(animationLeft);
editText.setAnimation(animationRight);
animationRight.start();
animationRight.setRepeatCount(Animation.INFINITE);
editText.setVisibility(EditText.VISIBLE);
imageView.startAnimation(animationBottom);
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.edittext);
editText.setVisibility(EditText.INVISIBLE);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
imageView = (ImageView) findViewById(R.id.imageview);
animationRight = AnimationUtils.loadAnimation(this,
R.anim.translate_right);
animationBottom = AnimationUtils.loadAnimation(this,
R.anim.translate_bottom);
animationTop = AnimationUtils.loadAnimation(this, R.anim.translate_top);
animationBottom.setAnimationListener(this);
animationTop.setAnimationListener(this);
}
}
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/8796150
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)