【问题标题】:Play video in Activity with sliding up layouts使用向上滑动布局在 Activity 中播放视频
【发布时间】:2016-09-01 13:01:15
【问题描述】:

我正在编写一个应用程序,我想在其中循环流式传输视频,其中布局滑动到 50% 的垂直标记。我还想将布局背景设为白色,透明度约为 50-60%。我已经找到了如何循环视频,我在单独的活动中有我需要的布局,我让动画上下滑动,我感到困惑的是如何将我的 XML 文件布局映射到布局列表以及如何调用它们向上滑动以及如何处理他们的内部事件,也就是 OnClick-s 等等。

MainActivity:

public class MainActivity extends Activity{


    private VideoView myVideoView;
    private MediaController mediaControls=null;
    private int position = 0;
    private String ppm_val=null;
    private TextView distance;
    private AdkReadTask mAdkReadTask;
    private int myInt = -10;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLed);
        distance  = (TextView) findViewById(R.id.textViewIntro);
        buttonLED.setVisibility(View.INVISIBLE);
        tv = (TextView) findViewById(R.id.ppm);


        Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.slide_down);

        final Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.slide_up);



        myVideoView = (VideoView) findViewById(R.id.videoView);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        myVideoView.getHolder().setFixedSize(size.x, size.y);
        tv.setText(""+size.x+":"+size.y);

        try {
            //set the uri of the video to be played
            myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test758));

        } catch (Exception e) {
//          Log.e("Error", e.getMessage());
//          e.printStackTrace();
        }

        //we also set an setOnPreparedListener in order to know when the video file is ready for playback
        myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer) {

                mediaPlayer.setLooping(true);
                myVideoView.seekTo(position);
                if (position == 0) {
                    myVideoView.start();
                } else {
                    //if we come from a resumed activity, video playback will be paused
                    myVideoView.pause();
                }
            }
        });

        if (((MyApplication) this.getApplication()).checkNull()) {
            ((MyApplication) this.getApplication()).init(this);
            ((MyApplication) this.getApplication()).open();

        }

        if (mAdkReadTask==null) {
            mAdkReadTask = new AdkReadTask();
            mAdkReadTask.execute();
        }
    }



    @Override
    public void onResume() {
        super.onResume();
        ((MyApplication) this.getApplication()).write("1");
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    public String output="";
    String[] sarr = new String[3];
    /* 
     * We put the readSerial() method in an AsyncTask to run the 
     * continuous read task out of the UI main thread
     */
    private class AdkReadTask extends AsyncTask<Void, String, String> {

        private boolean running = true;

        public void pause(){
            running = false;
        }
        public void start(){
            running = true;
        }
        private int cnt = 0;
        @Override
        protected String doInBackground(Void... params) {

            while(running) {


                if (this.isCancelled())
                    break;

                String s = ((MyApplication) getApplication()).read();
                if (s.charAt(0)=='R'){
                    running = false;
                }
                cnt++;
                publishProgress(s);
            }

            return output;
        }

        @Override
        protected void onProgressUpdate(String... progress) {
            if(progress[0].charAt(0)=='P') {
                tv.setText("Air quality: " + progress[0].substring(1, progress[0].length()));
                ppm_val = progress[0].substring(1, progress[0].length());
                ((MyApplication) getApplication()).setPPM(ppm_val);
            }
        }
        @Override
        protected void onPostExecute(String result){
            next();
        }
    }

    private void next() {
        final Intent intent = new Intent(this, BuyRecycleActivity.class );
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        startActivity(intent);
                    }
                },
                1000);
    }

}

slide_up.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1000"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>

slide_down.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="100%" />
</set>

编辑 1: 添加了代码。我所需要的只是将 BuyRecycleActivity 的布局从主要活动的底部向上滑动 50%,同时视频仍在播放

【问题讨论】:

  • 把你的代码放在这里

标签: android android-layout android-animation


【解决方案1】:

使用include 布局解决。然后将它们默认设置为不可见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多