Java Android MJPEG演示修改
我试图理解我找到的Android演示代码。此代码将IP摄像头视频流传输到android应用程序。在最初的应用程序中,Mjpeg视图占据了整个屏幕,在布局目录中没有使用activity.xml(这是我习惯看到的)。这是作为主活动加载的MjpegSample.java的部分代码。我想我知道setContentView(mv)和WindowManager.LayoutParams.FLAG_全屏显示是所有内容占据屏幕的原因。有没有一种方法可以在处理这种类型的活动时仍然添加其他对象,如按钮或背景Java Android MJPEG演示修改,java,android,video,mjpeg,Java,Android,Video,Mjpeg,我试图理解我找到的Android演示代码。此代码将IP摄像头视频流传输到android应用程序。在最初的应用程序中,Mjpeg视图占据了整个屏幕,在布局目录中没有使用activity.xml(这是我习惯看到的)。这是作为主活动加载的MjpegSample.java的部分代码。我想我知道setContentView(mv)和WindowManager.LayoutParams.FLAG_全屏显示是所有内容占据屏幕的原因。有没有一种方法可以在处理这种类型的活动时仍然添加其他对象,如按钮或背景 pub
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对象作为主视图内容,然后将视图对象添加到其中(我还包括嵌套在主视图中的LinearLayout示例,以展示如何将视图容器嵌入其他视图容器中):
这很有效。因此,看起来您可以通过编程方式添加对象并对视图进行分层。此方法根本不涉及“接口生成器”xml。谢谢你的解释。非常有用。
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));
}
}