常用经典应用布局(游戏开发,照相机,增强现实)
基本的应用布局大家都会知道怎么用,但是对于一些特殊的布局,估计开始对初学者感觉有点困惑。下面主要是讲讲绘画应用布局,照相机应用布局以及增强现实(Augmented Reality)应用布局,主要是一些这方面的简单布局!
1绘画布局(游戏布局)。
绘画布局在游戏开发中经常用到,经常与view,surfaceview结合。下面给出一个常用的例子。Main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.gv.MyView
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
这个是把MyView这个绘画view放在布局中,MyView是一个java类继承view也可以继承SurfaceView,需要注意的是如果是这样布局,一定要在MyView类增加构造函数中的AttributeSet attrs。而不是单个context,否则会报错:比如下面的报错信息:
E/AndroidRuntime(471): Caused by:android.view.InflateException: Binary XML file line #6: Error inflating classcom.gv.MyView
一定需要这个构造类。
public MyView(Context context, AttributeSet attrs) {
super(context,attrs);
//TODO Auto-generated constructor stub
}
java文件类:
GameViewActivity是:
public class GameViewActivity extends Activity {
/** Calledwhen the activity is first created. */
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}。
运行结果
第二种照相机布局,
下面列举我的一个开发的例子
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="@+id/msurfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Buttonandroid:id="@+id/mbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Takea Photo!"
/>
</RelativeLayout>
我们可以发现,其实只是在相对布局加入了 <SurfaceView
android:id="@+id/msurfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
这个surfaceview是应用控制照相机画面的。通过下面两个java语句,连接其照相机布局:
setContentView(R.layout.activity_main);
//实例化拍照界面组件SurfaceView
mSurfaceView =(SurfaceView) findViewById(R.id.msurfaceview);
运行结果:
大家可以发现其实Button可以添加到照相机画面中,一开始我还不知道在绘画View中添加Button等,这个布局刚好解决了这个问题,我很喜欢用相对布局(RelativeLayout),因为我可以随便调整Button的位置。这个例子的源代码,我会在后面的照相机项目中分享给大家,请大家耐心等待。我主要是从基础不好的我的角度来跟大家步步分享我的学习过程。
3增强现实布局
增钱现实布局其实照相机基础上,增加绘画布局。在后面的项目例子源代码中我会分享的,请大家耐心等待。
代码下载:
http://download.youkuaiyun.com/detail/tianke0711/4638896