{"id":14973,"date":"2014-10-03T12:43:28","date_gmt":"2014-10-03T09:43:28","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=14973"},"modified":"2019-04-24T16:00:13","modified_gmt":"2019-04-24T13:00:13","slug":"android-surfaceview-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/","title":{"rendered":"Android SurfaceView Example"},"content":{"rendered":"<p>In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly or if the rendering takes too much time and affects user experience then we should use <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/SurfaceView.html\"><code>SurfaceView<\/code><\/a>.<\/p>\n<p>The Android SurfaceView provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface, however, the SurfaceView takes care of placing the surface at the correct location on the screen.<\/p>\n<p>So, in this example, we are going to see the use of Android SurfaceView through a camera appliction that will use the SurfaceView to preview the camera angle to the screen of the mobile device.<\/p>\n<p>For our example will use the following tools in a Windows 64-bit or an  OS X platform:<\/p>\n<ul>\n<li>JDK 1.7<\/li>\n<li>Eclipse 4.2 Juno<\/li>\n<li>Android SDK 4.4.2<\/li>\n<\/ul>\n<p>Let\u2019s take a closer look:\n<\/p>\n<h2>1. Create a New Android Application Project<\/h2>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<p>Open Eclipse IDE and go to File \u2192 New \u2192 Project \u2192 Android Application Project.<\/p>\n<p><figure id=\"attachment_13637\" aria-describedby=\"caption-attachment-13637\" style=\"width: 596px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1.png\" alt=\"Figure 2. Create a new Android project\" width=\"596\" height=\"559\" class=\"size-full wp-image-13637\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1.png 596w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample1-300x281.png 300w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/a><figcaption id=\"caption-attachment-13637\" class=\"wp-caption-text\">Figure 1. Create a new Android project<\/figcaption><\/figure><\/p>\n<p>Specify the name of the application, the project and the package and then click Next.<\/p>\n<p><figure id=\"attachment_13786\" aria-describedby=\"caption-attachment-13786\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-1.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-1.jpg\" alt=\"Figure 2. Create a new Android project name\" width=\"636\" height=\"543\" class=\"size-full wp-image-13786\"><\/a><figcaption id=\"caption-attachment-13786\" class=\"wp-caption-text\">Figure 2. Create a new Android project name<\/figcaption><\/figure><\/p>\n<p>In the next window, the \u201cCreate Activity\u201d option should be checked. The new created activity will be the main activity of your project. Then press Next button.<\/p>\n<p><figure id=\"attachment_11777\" aria-describedby=\"caption-attachment-11777\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-2.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-2.jpg\" alt=\"Create Activity\" width=\"636\" height=\"543\" class=\"alignnone size-full wp-image-13642\"><\/a><figcaption id=\"caption-attachment-11777\" class=\"wp-caption-text\">Figure 3. Configure the project<\/figcaption><\/figure><\/p>\n<p>In \u201cConfigure Launcher Icon\u201d window you should choose the icon you want to have in your app. We will use the default icon of android, so click Next.<\/p>\n<p><figure id=\"attachment_11778\" aria-describedby=\"caption-attachment-11778\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4.png\" alt=\"Configure Launcher Icon\" width=\"660\" height=\"590\" class=\"alignnone size-full wp-image-13638\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample4-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11778\" class=\"wp-caption-text\">Figure 4. Configure the launcher icon<\/figcaption><\/figure><\/p>\n<p>Select the \u201cBlank Activity\u201d option and press Next.<\/p>\n<p><figure id=\"attachment_11779\" aria-describedby=\"caption-attachment-11779\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5.png\" alt=\"Blank Activity\" width=\"660\" height=\"590\" class=\"alignnone size-full wp-image-13639\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/AndroidExample5-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11779\" class=\"wp-caption-text\">Figure 5. Create the activity and select its type<\/figcaption><\/figure><\/p>\n<p>You have to specify a name for the new Activity and a name for the layout description of your app. The .xml file for the layout will automatically be created in the res\/layout folder. It will also be created a fragment layout xml, that we are not going to use in this project and you can remove it if you want. Then press Finish.<\/p>\n<p><figure id=\"attachment_13788\" aria-describedby=\"caption-attachment-13788\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-3.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-3.jpg\" alt=\"Figure 6. Create a new blank activity\" width=\"660\" height=\"590\" class=\"size-full wp-image-13788\"><\/a><figcaption id=\"caption-attachment-13788\" class=\"wp-caption-text\">Figure 6. Create a new blank activity<\/figcaption><\/figure><\/p>\n<p>Here you can see, how will the structure of the project become when finished:<\/p>\n<p><figure id=\"attachment_13789\" aria-describedby=\"caption-attachment-13789\" style=\"width: 351px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-4.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-4.jpg\" alt=\"Figure 7. The tree of the project\" width=\"351\" height=\"445\" class=\"size-full wp-image-13789\"><\/a><figcaption id=\"caption-attachment-13789\" class=\"wp-caption-text\">Figure 7. The tree of the project<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span id=\"code\"> <\/span><\/p>\n<h2>2. Creating the layout of the main AndroidSurfaceViewExample<\/h2>\n<p>We are going to make a very simple layout xml for the <code>AndroidSurfaceViewExample.class<\/code>, that only consists of a <code>LinearLayout<\/code> that contains the <code>SurfaceView<\/code> and a clickable <code>LinearLayout<\/code>.<\/p>\n<p>Open <code>res\/layout\/activity_main.xml<\/code>, go to the respective xml tab and paste the following:<\/p>\n<p><em><span style=\"text-decoration: underline\">activity_main.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:id=\"@+id\/container\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"\n    tools:context=\"com.javacodegeeks.androidsurfaceviewexample.AndroidSurfaceviewExample\" &gt;\n\n    &lt;SurfaceView\n        android:id=\"@+id\/surfaceView\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"0dp\"\n        android:layout_weight=\"1\" \/&gt;\n\n    &lt;LinearLayout\n        android:id=\"@+id\/capture\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:clickable=\"true\"\n        android:gravity=\"center\"\n        android:onClick=\"captureImage\"\n        android:orientation=\"horizontal\" &gt;\n\n        &lt;TextView\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:gravity=\"center\"\n            android:padding=\"10dp\"\n            android:text=\"Capture\"\n            android:textAppearance=\"?android:attr\/textAppearanceLarge\" \/&gt;\n    &lt;\/LinearLayout&gt;\n\n&lt;\/LinearLayout&gt;\n<\/pre>\n<h2>3. Creating the source code of the main AndroidSurfaceViewExample Activity<\/h2>\n<p>Open <code>src\/com.javacodegeeks.androidcursorexample\/AndroidSurfaceViewExample.java<\/code> file and paste the code below.<\/p>\n<p><em><span style=\"text-decoration: underline\">AndroidSurfaceViewExample.java<\/span><\/em><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.androidsurfaceviewexample;\n\nimport java.io.FileNotFoundException;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\n\nimport android.app.Activity;\nimport android.hardware.Camera;\nimport android.hardware.Camera.PictureCallback;\nimport android.hardware.Camera.ShutterCallback;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.view.SurfaceHolder;\nimport android.view.SurfaceView;\nimport android.view.View;\nimport android.widget.TextView;\nimport android.widget.Toast;\n\npublic class AndroidSurfaceviewExample extends Activity implements SurfaceHolder.Callback {\n\tTextView testView;\n\n\tCamera camera;\n\tSurfaceView surfaceView;\n\tSurfaceHolder surfaceHolder;\n\n\tPictureCallback rawCallback;\n\tShutterCallback shutterCallback;\n\tPictureCallback jpegCallback;\n\n\t\/** Called when the activity is first created. *\/\n\t@Override\n\tpublic void onCreate(Bundle savedInstanceState) {\n\t\tsuper.onCreate(savedInstanceState);\n\n\t\tsetContentView(R.layout.activity_main);\n\n\t\tsurfaceView = (SurfaceView) findViewById(R.id.surfaceView);\n\t\tsurfaceHolder = surfaceView.getHolder();\n\n\t\t\/\/ Install a SurfaceHolder.Callback so we get notified when the\n\t\t\/\/ underlying surface is created and destroyed.\n\t\tsurfaceHolder.addCallback(this);\n\n\t\t\/\/ deprecated setting, but required on Android versions prior to 3.0\n\t\tsurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);\n\n\t\tjpegCallback = new PictureCallback() {\n\t\t\tpublic void onPictureTaken(byte[] data, Camera camera) {\n\t\t\t\tFileOutputStream outStream = null;\n\t\t\t\ttry {\n\t\t\t\t\toutStream = new FileOutputStream(String.format(\"\/sdcard\/%d.jpg\", System.currentTimeMillis()));\n\t\t\t\t\toutStream.write(data);\n\t\t\t\t\toutStream.close();\n\t\t\t\t\tLog.d(\"Log\", \"onPictureTaken - wrote bytes: \" + data.length);\n\t\t\t\t} catch (FileNotFoundException e) {\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t} catch (IOException e) {\n\t\t\t\t\te.printStackTrace();\n\t\t\t\t} finally {\n\t\t\t\t}\n\t\t\t\tToast.makeText(getApplicationContext(), \"Picture Saved\", 2000).show();\n\t\t\t\trefreshCamera();\n\t\t\t}\n\t\t};\n\t}\n\n\tpublic void captureImage(View v) throws IOException {\n\t\t\/\/take the picture\n\t\tcamera.takePicture(null, null, jpegCallback);\n\t}\n\n\tpublic void refreshCamera() {\n\t\tif (surfaceHolder.getSurface() == null) {\n\t\t\t\/\/ preview surface does not exist\n\t\t\treturn;\n\t\t}\n\n\t\t\/\/ stop preview before making changes\n\t\ttry {\n\t\t\tcamera.stopPreview();\n\t\t} catch (Exception e) {\n\t\t\t\/\/ ignore: tried to stop a non-existent preview\n\t\t}\n\n\t\t\/\/ set preview size and make any resize, rotate or\n\t\t\/\/ reformatting changes here\n\t\t\/\/ start preview with new settings\n\t\ttry {\n\t\t\tcamera.setPreviewDisplay(surfaceHolder);\n\t\t\tcamera.startPreview();\n\t\t} catch (Exception e) {\n\n\t\t}\n\t}\n\n\tpublic void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {\n\t\t\/\/ Now that the size is known, set up the camera parameters and begin\n\t\t\/\/ the preview.\n\t\trefreshCamera();\n\t}\n\n\tpublic void surfaceCreated(SurfaceHolder holder) {\n\t\ttry {\n\t\t\t\/\/ open the camera\n\t\t\tcamera = Camera.open();\n\t\t} catch (RuntimeException e) {\n\t\t\t\/\/ check for exceptions\n\t\t\tSystem.err.println(e);\n\t\t\treturn;\n\t\t}\n\t\tCamera.Parameters param;\n\t\tparam = camera.getParameters();\n\n\t\t\/\/ modify parameter\n\t\tparam.setPreviewSize(352, 288);\n\t\tcamera.setParameters(param);\n\t\ttry {\n\t\t\t\/\/ The Surface has been created, now tell the camera where to draw\n\t\t\t\/\/ the preview.\n\t\t\tcamera.setPreviewDisplay(surfaceHolder);\n\t\t\tcamera.startPreview();\n\t\t} catch (Exception e) {\n\t\t\t\/\/ check for exceptions\n\t\t\tSystem.err.println(e);\n\t\t\treturn;\n\t\t}\n\t}\n\n\tpublic void surfaceDestroyed(SurfaceHolder holder) {\n\t\t\/\/ stop preview and release camera\n\t\tcamera.stopPreview();\n\t\tcamera.release();\n\t\tcamera = null;\n\t}\n\n}\n<\/pre>\n<p>Let&#8217;s see in detail the code above.[ulp id=&#8217;Ja8Orb5oPKdShcXt&#8217;]<\/p>\n<p>We set the <code>activity_main.xml<\/code> layout and we initialize our main layout by:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">setContentView(R.layout.activity_main);<\/pre>\n<p>In these lines, we find our SurfaceView from our main layout and we get a holder, an abstract interface to someone holding a display surface. This, allows us to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. We also install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed. We have also written the <code>surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);<\/code> deprecated setting, but required on Android versions prior to 3.0.<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\tsurfaceView = (SurfaceView) findViewById(R.id.surfaceView);\n\tsurfaceHolder = surfaceView.getHolder();\n\tsurfaceHolder.addCallback(this);\n\tsurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);\n<\/pre>\n<p>We have implemented the basic methods of the SurfaceView, <code>public void surfaceCreated(SurfaceHolder holder)<\/code>, <code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)<\/code> and <code>public void surfaceDestroyed(SurfaceHolder holder))<\/code>.<\/p>\n<p>In <code>public void surfaceCreated(SurfaceHolder holder)<\/code>, we are trying to open the camera service, and also to set the camera parameters and when the Surface has been created, to draw the preview.<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\tpublic void surfaceCreated(SurfaceHolder holder) {\n\t\ttry {\t\t\t\n\t\t\tcamera = Camera.open();\n\t\t} catch (RuntimeException e) {\n\t\t\tSystem.err.println(e);\n\t\t\treturn;\n\t\t}\n\t\tCamera.Parameters param;\n\t\tparam = camera.getParameters();\n\n\t\tparam.setPreviewSize(352, 288);\n\t\tcamera.setParameters(param);\n\t\ttry {\n\t\t\tcamera.setPreviewDisplay(surfaceHolder);\n\t\t\tcamera.startPreview();\n\t\t} catch (Exception e) {\n\t\t\tSystem.err.println(e);\n\t\t\treturn;\n\t\t}\n\t}\n<\/pre>\n<p>In <code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)<\/code>, we are trying to reset the preview size and make any resize, rotate or reformatting changes, in order to start the preview with new settings.<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\tpublic void refreshCamera() {\n\t\tif (surfaceHolder.getSurface() == null) {\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tcamera.stopPreview();\n\t\t} catch (Exception e) {\n\t\t\t\n\t\t}\n\t\ttry {\n\t\t\tcamera.setPreviewDisplay(surfaceHolder);\n\t\t\tcamera.startPreview();\n\t\t} catch (Exception e) {\n\n\t\t}\n\t}\n<\/pre>\n<p>In <code>public void surfaceDestroyed(SurfaceHolder holder)<\/code>, we stop the preview and we release the camera service, in order to be used from other applications.<\/p>\n<pre class=\"brush:java; wrap-lines:false\">\tpublic void surfaceDestroyed(SurfaceHolder holder) {\n\t\t\/\/ stop preview and release camera\n\t\tcamera.stopPreview();\n\t\tcamera.release();\n\t\tcamera = null;\n\t}\n<\/pre>\n<h2>4. Android Manifest<\/h2>\n<p>Because this application uses the hardware camera and tries to save images to the device&#8217;s external storage (SD Card), we must also specify this in the manifest. The AndroidManifest.xml of our project is simple and contains the permissions:<\/p>\n<p><em><span style=\"text-decoration: underline\">AndroidManifest.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false,highlight:[7,8,10,11]\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    package=\"com.javacodegeeks.androidsurfaceviewexample\"\n    android:versionCode=\"1\"\n    android:versionName=\"1.0\" &gt;\n\n    &lt;uses-permission android:name=\"android.permission.CAMERA\" \/&gt;\n    &lt;uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" \/&gt;\n\n    &lt;uses-feature android:name=\"android.hardware.camera\" \/&gt;\n    &lt;uses-feature android:name=\"android.hardware.camera.autofocus\" \/&gt;\n\n    &lt;uses-sdk\n        android:minSdkVersion=\"8\"\n        android:targetSdkVersion=\"19\" \/&gt;\n\n    &lt;application\n        android:allowBackup=\"true\"\n        android:icon=\"@drawable\/ic_launcher\"\n        android:label=\"@string\/app_name\" &gt;\n        &lt;activity\n            android:name=\"com.javacodegeeks.androidsurfaceviewexample.AndroidSurfaceviewExample\"\n            android:label=\"@string\/app_name\" &gt;\n            &lt;intent-filter&gt;\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\n            &lt;\/intent-filter&gt;\n        &lt;\/activity&gt;\n    &lt;\/application&gt;\n&lt;\/manifest&gt;\n<\/pre>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nNote, that if an essential function of your application is taking pictures, then restrict its visibility on Google Play to devices that have a camera. To advertise that your application depends on having a camera, put a  tag in your manifest file.<\/div>\n<h2>5. Build, compile and run<\/h2>\n<p>When we build, compile and run our project, the main AndroidSurfaceViewExample should look like this:<\/p>\n<p><figure id=\"attachment_14193\" aria-describedby=\"caption-attachment-14193\" style=\"width: 711px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-5.jpg\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceViewExample-5.jpg\" alt=\"Figure 8. Figure This is how the main Activity looks like\" width=\"711\" height=\"400\" class=\"size-full wp-image-14193\"><\/a><figcaption id=\"caption-attachment-14193\" class=\"wp-caption-text\">Figure 8. Figure This is how the main Activity looks like.<\/figcaption><\/figure><\/p>\n<h2>Download the Eclipse Project<\/h2>\n<p>This was an example of Android AndroidSurfaceViewExample.<\/p>\n<div class=\"download\"><strong>Download<br \/>\n<\/strong>You can download the full source code of this example here:  <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/AndroidSurfaceviewExample.zip\"><strong>AndroidSurfaceViewExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly or if the rendering takes too much time and affects user experience then we should use SurfaceView. The Android SurfaceView provides a dedicated drawing surface &hellip;<\/p>\n","protected":false},"author":14,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[715],"tags":[],"class_list":["post-14973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-surfaceview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android SurfaceView Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android SurfaceView Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-10-03T09:43:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-24T13:00:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Chryssa Aliferi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Chryssa Aliferi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/\"},\"author\":{\"name\":\"Chryssa Aliferi\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\"},\"headline\":\"Android SurfaceView Example\",\"datePublished\":\"2014-10-03T09:43:28+00:00\",\"dateModified\":\"2019-04-24T13:00:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/\"},\"wordCount\":826,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"articleSection\":[\"SurfaceView\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/\",\"name\":\"Android SurfaceView Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"datePublished\":\"2014-10-03T09:43:28+00:00\",\"dateModified\":\"2019-04-24T13:00:13+00:00\",\"description\":\"In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"core\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"ui\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/ui\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"SurfaceView\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/ui\/surfaceview\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Android SurfaceView Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\",\"name\":\"Chryssa Aliferi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"caption\":\"Chryssa Aliferi\"},\"description\":\"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android SurfaceView Example - Java Code Geeks","description":"In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/","og_locale":"en_US","og_type":"article","og_title":"Android SurfaceView Example - Java Code Geeks","og_description":"In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly","og_url":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-10-03T09:43:28+00:00","article_modified_time":"2019-04-24T13:00:13+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","type":"image\/jpeg"}],"author":"Chryssa Aliferi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Chryssa Aliferi","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/"},"author":{"name":"Chryssa Aliferi","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332"},"headline":"Android SurfaceView Example","datePublished":"2014-10-03T09:43:28+00:00","dateModified":"2019-04-24T13:00:13+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/"},"wordCount":826,"commentCount":2,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","articleSection":["SurfaceView"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/","url":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/","name":"Android SurfaceView Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","datePublished":"2014-10-03T09:43:28+00:00","dateModified":"2019-04-24T13:00:13+00:00","description":"In Android, all simple layout views are all drawn on the same GUI thread which is also used for all user interaction. So if we need to update GUI rapidly","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/ui\/surfaceview\/android-surfaceview-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/"},{"@type":"ListItem","position":3,"name":"core","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/"},{"@type":"ListItem","position":4,"name":"ui","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/ui\/"},{"@type":"ListItem","position":5,"name":"SurfaceView","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/ui\/surfaceview\/"},{"@type":"ListItem","position":6,"name":"Android SurfaceView Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332","name":"Chryssa Aliferi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","caption":"Chryssa Aliferi"},"description":"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/14973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=14973"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/14973\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=14973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=14973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=14973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}