简体   繁体   English

Android MjpegDemo修改

[英]Android MjpegDemo Modification

I am trying to understand Android MjpegDemo code that I found. 我试图了解我发现的Android MjpegDemo代码。 This code streams IP camera video to android app. 此代码将IP摄像机视频流式传输到android应用。 In the original app Mjpeg view takes up an entire screen and doesn't use an activity.xml in the layout dir (which is what I am used to seeing). 在原始应用程序中,Mjpeg视图占据整个屏幕,并且在布局目录中不使用activity.xml(这是我经常看到的内容)。 This is partial code for the MjpegSample.java which loads as main activity. 这是作为主要活动加载的MjpegSample.java的部分代码。 I think I understand that setContentView(mv) and WindowManager.LayoutParams.FLAG_FULLSCREEN is the reason everything fills the screen. 我想我知道setContentView(mv)和WindowManager.LayoutParams.FLAG_FULLSCREEN是一切都填满屏幕的原因。 Is there a way to work with this type of an Activity and still add other objects, like buttons or backgrounds? 有没有一种方法可以处理这种活动类型并仍然添加其他对象,例如按钮或背景?

public class MjpegSample extends Activity {
private MjpegView mv;

public void onCreate(Bundle myBundle) {          
    super.onCreate(myBundle);

    String URL = "http://someURL/mjpg/video.mjpg"; 

    requestWindowFeature(Window.FEATURE_NO_TITLE);          
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mv = new MjpegView(this);
    setContentView(mv);  

    mv.setSource(MjpegInputStream.read(URL));
    mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);     
}

} }

Here is a detailed example on how to achieve this. 这是有关如何实现此目的的详细示例。 Here, we use a LinearLayout object as the main view content, then we add our View objects to this (I also included an example LinearLayout that is nested inside the main one, to show how you can embed View containers within other View containers): 在这里,我们使用LinearLayout对象作为主要的视图内容,然后向其中添加View对象(我还包括了一个嵌套在主要对象内部的示例LinearLayout,以展示如何将View容器嵌入其他View容器中):

public class MjpegSample extends Activity {
    private MjpegView mv;

    private LinearLayout ll1;
    private LinearLayout nestedL;
    private Button btn1;
    private TextView txt1;

    public void onCreate(Bundle myBundle) {          
        super.onCreate(myBundle);

        //allow legacy-style network queries on UI thread (in case you compile for Android 3.0+, which do not allow
        //network transactions in main UI thread by default)
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        final String URL = "http://someURL/mjpg/video.mjpg"; 

        requestWindowFeature(Window.FEATURE_NO_TITLE);          
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Create new button programmatically
        btn1 = new Button(this);
        btn1.setText("Test button 1");
        //set so that content is wrapped in its parent container (which will be the nestedL object below)
        btn1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        //Create new TextView programmatically
        txt1 = new TextView(this);
        txt1.setText("Test text 1");
        //set so that content is wrapped in its parent container (which will be the nestedL object below)
        txt1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        //Create new MjpegView programmatically
        mv = new MjpegView(this);
        //set so that content is wrapped in its parent container (which will be the ll1 object below)
        mv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        nestedL = new LinearLayout(this);
        nestedL.setOrientation(LinearLayout.HORIZONTAL);
        //set so that content is wrapped in its parent container (which will be the ll1 object below)
        nestedL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        //we add btn1 and txt1 in nestedL LinearLayout (they will be ordered horizontally, according to nestedL orientation
        //as configured above, and all those widgets will not be stretched either)
        nestedL.addView(btn1);
        nestedL.addView(txt1);

        //create the main LinearLayout widget which will be set as the view content using setContentView(...)
        ll1 = new LinearLayout(this);
        ll1.setOrientation(LinearLayout.VERTICAL);
        //we add the other LinearLayout (horizontal one) on top
        ll1.addView(nestedL);
        //then we add the video player thing
        ll1.addView(mv);

        //ll1 is set as the main view for your activity
        setContentView(ll1);  


        mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
        mv.setSource(MjpegInputStream.read(URL));


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM