您的位置:首页 > 移动开发 > Android开发

Android 模拟电视机开关机的动画效果

2017-06-16 16:42 309 查看
话不多说先上图:



关机动画

public class TVOffAnimation extends Animation {
private int halfWidth;
private int halfHeight;

@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);
setDuration(1000);
setFillAfter(true);
//保存View的中心点
halfWidth = width / 2;
halfHeight = height / 2;
setInterpolator(new AccelerateDecelerateInterpolator());

}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {

final Matrix matrix = t.getMatrix();
if (interpolatedTime < 0.8) {
matrix.preScale(1+0.625f*interpolatedTime, 1-interpolatedTime/0.8f+0.01f,halfWidth,halfHeight);
}else{
matrix.preScale(7.5f*(1-interpolatedTime),0.005f,halfWidth,halfHeight);
}
}

}


开机动画

public class TVOpenAnimation extends Animation {
private int halfWidth;
private int halfHeight;

@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);
setDuration(1000);
setFillAfter(true);
//保存View的中心点
halfWidth = width / 2;
halfHeight = height / 2;
setInterpolator(new AccelerateDecelerateInterpolator());

}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {

final Matrix matrix = t.getMatrix();
/*  if (interpolatedTime < 0.8) {
matrix.preScale(1 + 0.625f * interpolatedTime, 1 - interpolatedTime / 0.8f + 0.01f, halfWidth, halfHeight);
} else {
matrix.preScale(7.5f * (1 - interpolatedTime), 0.01f, halfWidth, halfHeight);
}*/
if (interpolatedTime<0.4)
{
matrix.preScale( interpolatedTime / (1-0.8f),0.005f,halfWidth,halfHeight);
}else {
matrix.preScale(1,interpolatedTime,halfWidth,halfHeight);
}

}
}


MainActivity调用

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
final View view = findViewById(R.id.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.img);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.startAnimation(new TVOffAnimation());
}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.duang);
imageView.startAnimation(new TVOpenAnimation());
}
});
//这是弹球效果的代码
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mHeight = imageView.getHeight();
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(ObjectAnimator.ofFloat(imageView,"translationY",-mHeight,0).setDuration(2000));
animatorSet.setInterpolator(new BounceInterpolator());
animatorSet.start();
}
});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 动画 电视