2014-07-12 8 views
0

발견 한 Android MjpegDemo 코드를 이해하려고합니다. 이 코드는 IP 카메라 비디오를 Android 앱에 스트리밍합니다. 원래 응용 프로그램에서 Mjpeg보기는 전체 화면을 차지하고 layout 디렉토리에서 activity.xml을 사용하지 않습니다 (이는 내가 보았던 것입니다). 이것은 주 활동으로로드되는 MjpegSample.java의 부분 코드입니다. 나는 setContentView (mv)와 WindowManager.LayoutParams.FLAG_FULLSCREEN이 모든 것을 화면에 채우는 이유라고 생각한다. 이 유형의 Activity로 작업하고 버튼이나 배경과 같은 다른 객체를 추가 할 수있는 방법이 있습니까? 여기Android MjpegDemo 수정

답변

0

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);  
} 

}는이를 달성하는 방법에 대한 구체적인 예이다. 여기서는 LinearLayout 객체를 기본 뷰 컨텐츠로 사용하고, 여기에 View 객체를 추가합니다 (다른 View 컨테이너에 View 컨테이너를 내장하는 방법을 보여주기 위해 기본 객체 내에 중첩 된 LinearLayout 예제가 포함됨).

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)); 


    } 
} 
+0

아주 잘 작동합니다. 따라서 프로그램 적으로 객체를 추가하고 뷰를 계층화 할 수있는 것처럼 보입니다. 이 방법은 "인터페이스 빌더"XML을 전혀 포함하지 않습니다. 설명해 주셔서 감사합니다. 매우 도움이됩니다. – vladiator