0

我正在开发一个可互换显示视频和图像的 Android 应用程序。该应用程序正在运行,只是它在转换之间显示了一个短暂的空白屏幕。我想让过渡平滑。任何帮助表示赞赏。提前致谢。这是代码 -

public class Standee implements Runnable, OnCompletionListener {
    private Vector<File> files;
    private Vector<String> timings;
    private Handler h;
    private VideoView video;
    private ImageView image;
    private Context ctx;
    private int i;
    private MediaPlayer player;
    public MediaPlayer getMediaPlayer(){
        return player;
    }

    public Standee(Context ctx, VideoView video, ImageView image) {
        this.video=video;
        this.image=image;
        this.ctx=ctx;
        i=0;
        h=new Handler();
        files=new Vector<File>();
        timings=new Vector<String>();
        player=MediaPlayer.create(ctx, R.raw.background);
        try {
            FileReader fr=new FileReader(ConfigLoader.BETA_PATH+"media.tmr");
            BufferedReader br=new BufferedReader(fr);
            String s;
            while((s=br.readLine())!=null){
                String[] data=s.split(";");
                files.add(new File(ConfigLoader.BETA_PATH+data[0]));
                timings.add(data[1]);
            }
            br.close();
            video.setOnCompletionListener(this);
            video.setMediaController(null);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onCompletion(MediaPlayer arg0) {


    }

    @Override
    public void run() {
        if(files.elementAt(i).getAbsolutePath().endsWith(".jpg")){
            video.setVisibility(View.GONE);
            image.setVisibility(View.VISIBLE);
            if(video.isPlaying()){
                video.stopPlayback();
            }
            player=MediaPlayer.create(ctx, R.raw.background);
            try {
//              player.prepare();
                player.setLooping(true);
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            player.start();
            showPhoto();
        }else if(files.elementAt(i).getAbsolutePath().endsWith(".mp4")){
            if(player.isPlaying()){
                player.stop();
//              player.release();
            }
            image.setVisibility(View.GONE);
            video.setVisibility(View.VISIBLE);
            playVideo();
        }

        h.postDelayed(this, Integer.parseInt(timings.elementAt(i)));
        i++;
        if (i >= files.size())
            i = 0;

    }

    public void start(){
        h.post(this);
    }

    public void playVideo(){
        Uri uri = Uri.parse(files.get(i).getAbsolutePath());
        video.setVideoURI(uri);
        video.requestFocus();
        video.start();
    }

    public void showPhoto(){
        image.setImageBitmap(BitmapFactory.decodeFile(files.elementAt(i)
                .getAbsolutePath()));
    }
}
4

1 回答 1

0

你现在所做的就是让一件事消失,然后让另一件事出现。你可能想看看 android 动画系统来尝试更流畅的东西。例如,您可以对视图的 alpha 属性进行动画更改,以使事物淡入淡出。您可以查看http://developer.android.com/training/animation/crossfade.html,它可能会有所帮助。

于 2013-11-14T04:58:28.057 回答