一、动画
1、View Animation: 补间动画,(旋转,位移,缩放,透明度,set)
原理: 通过父容器来绘制自己动画时的样子,当布局旋转到看不到的位置只是隐藏了,你点击原来的位置还会有效果
(1)、AlphaAnimation 透明度动画;
// Alpha动画0.0完全透明
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
// 设置动画播放的时间毫秒为单位
aa.setDuration(3000);
// 界面停留在动画结束状态
aa.setFillAfter(true);
(2)、ScaleAnimation 缩放动画效果
ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, //横向由0到1,纵向由0到1
// 设置锚点
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,//以x轴的一半,y轴的一半为轴
0.5f);
// 显示时间
sa.setDuration(3000);
// 界面停留在结束状态
sa.setFillAfter(true);
(3)、TranslateAnimation sss位移动画效果
animation = new TranslateAnimation(0.1f, 100.0f, //向右平移
1.0f, 100.0f);//向下平移
animation.setDuration(3000);
imageView.setAnimation(animation);
(4)、RotateAnimation 旋转动画效果
RotateAnimation rotate = new RotateAnimation(0, 360,
// 设置锚点
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 显示时间
rotate.setDuration(3000);
// 界面停留在结束状
rotate.setFillAfter(true);
(5)动画集
AnimationSet set = new AnimationSet(true);
set.addAnimation(alphaAnimation);
set.addAnimation(animation);
set.addAnimation(scaleAnimation);
set.setDuration(3000);
imageView.setAnimation(set);
(6)动画的更多设置
private void hideLevel(RelativeLayout container, long startOffset) {
// container.setVisibility(View.GONE);
RotateAnimation animation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 1f);
animation.setDuration(400);
animation.setFillAfter(true);//设置动画停留在最后的状态
animation.setStartOffset(startOffset);// 设置动画延时执行
animation.setAnimationListener(new AnimationListener() {//设置动画的监听事件
@Override
public void onAnimationStart(Animation animation) {
// 动画开始播放的回调方法
mAnimationCount++;//判断有多个动画播放
//判断一个动画 mAnimationFlag (false or true)
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// 动画结束播放的回调方法
mAnimationCount--;
}
});
container.startAnimation(animation);
}
2、Property Animation: 属性动画(补间动画可以做到的,都可以做到,做不到的也可以做到)
原理: 通过改变自身的属性来显示动画的
package org.itheima62.propertyanimation;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView iv;
private View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
v = findViewById(R.id.v);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "点击了我",
Toast.LENGTH_SHORT).show();
}
});
}
public void click(View view) {
// 位移动画
// 补间动画的实现
// TranslateAnimation animation = new TranslateAnimation(0, 300, 0, 0);
// animation.setDuration(4000);
// iv.startAnimation(animation);
// android 3.0之后的操作
// target:谁要做动画
//public static ObjectAnimation ofFloat(Object target, String propertyName, float... values){}
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX",
0f, 300f);
animator.setDuration(4000);
animator.start();
// setXXXX(参数为1)
// 参数类型
// v.setBackgroundColor();
ObjectAnimator colorOA = ObjectAnimator.ofObject(v, "backgroundColor",
new ArgbEvaluator(), Color.RED, Color.BLACK, Color.BLUE);
colorOA.setDuration(2000);
colorOA.setRepeatCount(ObjectAnimator.INFINITE);
colorOA.setRepeatMode(ObjectAnimator.REVERSE);
colorOA.start();
}
}
3、Drawable Animation: 帧动画