我正在使用带有动画的视频视图 (video.setAnimation(slideinRight);) 一切正常,除了在过渡时,只有视频视图的布局是动画,而不是视频。当翻译动画发生时,我看到一个框在我的视频上移动和遮罩,但视频从不随之移动。我不知道现在该做什么。
3 回答
我目前正在尝试将动画放在 VideoView 上。
如果你看Android源码,VideoView基本上就是一个SurfaceView,再加上一个MediaPlayer,对播放器的状态机进行了基本的管理。
实际上,真正的“绘制”工作似乎是由 mediaPlayer 中的本机方法处理的(也就是你的 android 设备上的真正播放器引擎实现)
我们在不同的设备上测试了动画,发现 VideoView 的底层视频播放器行为/实现在不同的 Android 设备之间是不一样的:
- 一些设备正确处理玩家的视图动画
- 其他人不这样做,只是显示模糊、黑屏或错误显示...
最重要的是,VideoView 似乎是直接写在内存上的,所以任何“解决方法”(比如在前面放置一个不透明的视图,并在该视图上设置动画)似乎都不起作用。
我很高兴得到其他人对此的反馈:)
很抱歉回答这个问题比较晚,但它是值得的。如果您只想使用“幻灯片”动画。尝试将视频视图放在布局中并为布局设置动画。
我在代码中设置它的方式是;
AbsoluteLayout Animationlayout = (AbsoluteLayout)findViewById(R.string.frontlayout);
VideoView pvv = new VideoView(getApplicationContext());
pvv.getHolder().addCallback(this);
Animationlayout.addView(pvv);
// load video data in pvv.
然后你想让你的videoview动画滑动;
Animationlayout.animate().xBy(25).setDuration(500).setInterpolator(new BounceInterpolator());
请注意,这是 3.1 动画系统。
不确定经典的 2.1 动画方式是否会像这样工作,但它应该起到同样的作用。
像旋转/缩放布局这样的东西不起作用。平移和淡化布局似乎是唯一有效的几件事。
只需使用TextureView:
更多关于 TextureView的参考。
我在TextureView上完成了ZoomIn - ZoomOut动画。
在Res -> anim文件夹中添加动画 xml 。
zoom_in_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:duration="1000"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.2"
android:toYScale="1.2" />
<set android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:duration="1000"
android:fillBefore="false"
android:fromXScale="1.2"
android:fromYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
</set>
纹理布局.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextureView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textureView"
android:layout_margin="50dp"
android:background="@android:color/darker_gray"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextureView>
TextureViewActivity.java:
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import java.io.IOException;
public class TextureViewActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
private TextureView textureView;
private MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.texture_layout);
textureView = (TextureView)findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
textureView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation zoomAnimation = AnimationUtils.loadAnimation(TextureViewActivity.this, R.anim.zoom_in_out);
textureView.startAnimation(zoomAnimation);
}
});
}
private String getVideoPath(){
return "android.resource://" + getPackageName() + "/" + R.raw.promovideo;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer= new MediaPlayer();
mMediaPlayer.setDataSource(TextureViewActivity.this, Uri.parse(getVideoPath()));
mMediaPlayer.setSurface(surface);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setLooping(true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
完毕