为什么我的视频不适合纹理的大小,因为我改变了纹理的大小

时间:2014-11-27 06:44:10

标签: android android-mediaplayer scaling textureview

Iam尝试使用纹理视图渲染的视频动画,动画效果很好,但是当我将纹理缩放到不同的大小时,媒体播放器无法缩放以适应纹理的大小,视频始终在全屏播放在后台,当我缩放纹理时,我只能看到视频的一部分   我的代码在运行android 4.0的某些Android设备上工作正常,但在更高版本的android设备上无效。继承我的动画代码,请告诉我问题出在哪里..

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener{
AnimationSet animset;
TextureView myTexture;
MediaPlayer mMediaPlayer;
Surface s;
int height;
static final String LogFileName = "Log";
int width;
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  myTexture = new TextureView(this);
  myTexture.setSurfaceTextureListener(this);
  setContentView(myTexture);    
   }

   @Override
   public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
   int arg2) {


   String mediaStorageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
s = new Surface(arg0);

   try {
       mMediaPlayer= new MediaPlayer();
       mMediaPlayer.setSurface(s);
       mMediaPlayer.setDataSource(mediaStorageDir+"/AirIndia.mp4");
       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
       mMediaPlayer.prepareAsync();
       mMediaPlayer.setLooping(true);
       mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

                mMediaPlayer.start();

            }
        });   


// to animate the texture   

     animset =new  AnimationSet(true);
     animset.addAnimation(scaling);
     animset.addAnimation(translate);
     animset.setStartOffset(20000);

  Animation fulscaling = new ScaleAnimation(1.0f, 1.3333f, 1.0f, 1.3333f);
     TranslateAnimation fulltranslate = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, -0.3333f,        0, 0.0f, 0, 0.0f);
          fulltranslate.setStartOffset(10000);

        animset.setDuration(1000);
        animset.setFillAfter(true);
        animset.setFillEnabled(true);
        fulscaling.setStartOffset(10000);

        animset.addAnimation(fulscaling);
    animset.addAnimation(fulltranslate);  

    myTexture.setAnimation(animset); 


    animset.setAnimationListener(new AnimationListener() {

          @Override
          public void onAnimationEnd(Animation arg0) {

              myTexture.startAnimation(animset);
          }

          @Override
          public void onAnimationRepeat(Animation arg0) {
              // TODO Auto-generated method stub

          }

          @Override
          public void onAnimationStart(Animation arg0) {
              // TODO Auto-generated method stub

          }

      });








} catch (IllegalArgumentException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} catch (SecurityException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} catch (IllegalStateException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} catch (IOException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} 

}

1 个答案:

答案 0 :(得分:6)

如果要缩放TextureView的内容,请使用setTransform()。有关示例,请参阅Grafika的PlayMovieActivity类中的adjustAspectRatio(),它会更改TextureView内容的大小以匹配正在播放的视频的宽高比。