我正在使用一个带有动画的视频视图(video.setAnimation(slideinRight);),除了在过渡时只有视频视图的布局在进行动画,而视频并没有跟随动画移动之外,一切都运行正常。当翻译动画发生时,我看到一个框移动并遮盖我的视频,但视频从未随之移动。我现在不知道该怎么办。
我正在使用一个带有动画的视频视图(video.setAnimation(slideinRight);),除了在过渡时只有视频视图的布局在进行动画,而视频并没有跟随动画移动之外,一切都运行正常。当翻译动画发生时,我看到一个框移动并遮盖我的视频,但视频从未随之移动。我现在不知道该怎么办。
我目前正在尝试在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.
那么你希望将你的视频视图动画化以便滑动;
Animationlayout.animate().xBy(25).setDuration(500).setInterpolator(new BounceInterpolator());
请注意,这是3.1动画系统。
不确定经典的2.1动画方式是否能像这样工作,但它应该起到同样的作用。
旋转/缩放布局之类的东西将无法工作。移动布局并使其逐渐消失似乎是唯一几个可行的操作。
只需使用TextureView:
更多关于TextureView的参考在此处。
我已经在TextureView上完成了放大-缩小动画。
将动画xml添加到Res->anim文件夹中。
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>
texture_layout.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) {
}
}
完成