why my video doesnot fit the size of the texture a

2019-08-31 09:19发布

问题:

Iam trying to animate a video rendered using texture view, the animation works fine , but when i scale the texture to a different size the media player does not scale to fit the size of the texture,the video is always playing full screen in the background and i can only see part of the video when i scale the texture
my code works fine on some android devices running android 4.0 but does not work on devices with later versions of android . heres my code for the animation kindly tell me where the problem is ..

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:

If you want to scale the TextureView's contents, use setTransform(). For an example, see adjustAspectRatio() in Grafika's PlayMovieActivity class, which changes the size of the TextureView contents to match the aspect ratio of the video being played.