对于普通视图,动画效果非常好。但是当我在SurfaceView上使用它时,它会淡出,然后闪烁屏幕,然后启动视频。闪烁看起来很傻,我需要删除它。
public class MyDreamService extends DreamService implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
private MediaPlayer mediaPlayer;
private SurfaceHolder vidHolder;
private SurfaceView vidSurface;
private FrameLayout frameLayout;
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setInteractive(false);
setFullscreen(true);
}
@Override
public void onDreamingStarted() {
super.onDreamingStarted();
frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new
ViewGroup.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
vidSurface = new SurfaceView(MyDreamService.this);
vidHolder = vidSurface.getHolder();
vidHolder.addCallback(MyDreamService.this);
frameLayout.addView(vidSurface,
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(frameLayout);
}
@Override
public void onDreamingStopped() {
super.onDreamingStopped();
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
vidHolder = holder;
mediaPlayer = MediaPlayer.create(this, R.raw.voice_activated_corgi);
mediaPlayer.setLooping(true);
mediaPlayer.setDisplay(holder);
mediaPlayer.setOnPreparedListener(this);
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
vidHolder = holder;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
}
这播放视频很好,但如果我想制作动画:
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(frameLayout, "alpha", .3f, 1f);
fadeIn.setDuration(2000);
final AnimatorSet mAnimationSet = new AnimatorSet();
mAnimationSet.play(fadeIn);
mAnimationSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if(mediaPlayer != null) {
mediaPlayer.start();
}
if(view != null) {
view.setAlpha(0f);
}
}
});
mAnimationSet.start();
然后在多次迭代后,它会淡化为黑色,然后闪烁发射器一秒钟,然后启动视频;或者根本就没有动画!我该怎么办?
答案 0 :(得分:1)
我设法通过使用渐变为黑色的叠加视图来修复它,一旦动画结束,将视图设置为不可见而不是将其删除。
public class MyDreamService extends DreamService implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
private MediaPlayer mediaPlayer;
private SurfaceHolder vidHolder;
private SurfaceView vidSurface;
private FrameLayout frameLayout;
private View view;
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setInteractive(false);
setFullscreen(true);
}
@Override
public void onDreamingStarted() {
super.onDreamingStarted();
frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new
ViewGroup.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
setContentView(frameLayout);
vidSurface = new SurfaceView(MyDreamService.this);
vidHolder = vidSurface.getHolder();
vidHolder.addCallback(MyDreamService.this);
frameLayout.addView(vidSurface,
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
view = new View(this);
view.setLayoutParams(new ViewGroup.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
frameLayout.addView(view);
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(frameLayout, "alpha", .3f, 1f);
fadeIn.setDuration(2000);
final AnimatorSet mAnimationSet = new AnimatorSet();
mAnimationSet.play(fadeIn);
mAnimationSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if(mediaPlayer != null) {
mediaPlayer.start();
}
if(view != null) {
view.setAlpha(0f);
}
}
});
mAnimationSet.start();
}
@Override
public void onDreamingStopped() {
super.onDreamingStopped();
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mediaPlayer = MediaPlayer.create(MyDreamService.this, R.raw.voice_activated_corgi);
mediaPlayer.setLooping(true);
mediaPlayer.setDisplay(vidHolder);
mediaPlayer.setOnPreparedListener(MyDreamService.this);
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
vidHolder = holder;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onPrepared(MediaPlayer mp) {
}
}