(iv) Realization of device hardware interaction and media interaction
4.6 Customizing the camera overlay
1. Draw the contents of the camera to Surfaceview in real time
To customize the capture interface, simply redefine your surface's interface
The following shows all the code
Public classPreviewactivityextendsAppcompatactivityImplementsSurfaceholder.callback {Camera mcamera; Surfaceview Mpreview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_preview); Mpreview=(Surfaceview) Findviewbyid (R.id.preview); assertMpreview! =NULL; Mpreview.getholder (). Addcallback ( This); Mpreview.getholder (). SetType (Surfaceholder.surface_type_push_buffers); Mcamera=Camera.open (); } @Overrideprotected voidOnPause () {Super. OnPause (); Mcamera.stoppreview (); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Mcamera.release (); } @Override Public voidsurfacecreated (Surfaceholder holder) {Try{Mcamera.setpreviewdisplay (Mpreview.getholder ()); } Catch(IOException e) {e.printstacktrace (); }} @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) {camera.parameters params=mcamera.getparameters (); List<Camera.Size> sizes=params.getsupportedpreviewsizes (); Camera.size selected=sizes.get (0); Params.setpreviewsize (Selected.width,selected.height); Mcamera.setparameters (params); Mcamera.setdisplayorientation (90); Mcamera.startpreview (); } @Override Public voidsurfacedestroyed (Surfaceholder holder) {}}
2. Change the direction of shooting, after calling Setdisplayorientation (), the direction of shooting will be displayed vertically
Mcamera.setdisplayorientation (90);
4.12 Creating a Tilt listener
1. Get the Sensormanager service of the system
Msensormanager= (Sensormanager) Getsystemservice (Sensor_service);
2. Get the Accelerometer sensor
Maccelerometer=msensormanager.getdefaultsensor (Sensor.type_accelerometer);
3. Monitoring in the main method, changing colors in real time
Public voidonsensorchanged (Sensorevent event) {float[] values =event.values; floatx = values[0]/10; floaty = values[1]/10; intScalefactor; if(X > 0) {Scalefactor= (int) math.min (x * 255, 255); Mright.setbackgroundcolor (color.transparent); Mleft.setbackgroundcolor (Color.argb (Scalefactor,255, 0, 0)); } Else{scalefactor= (int) Math.min (Math.Abs (x) * 255, 255); Mright.setbackgroundcolor (Color.argb (Scalefactor,255, 0, 0)); Mleft.setbackgroundcolor (color.transparent); } if(Y > 0) {Scalefactor= (int) math.min (Y * 255, 255); Mtop.setbackgroundcolor (color.transparent); Mbottom.setbackgroundcolor (Color.argb (Scalefactor,255, 0, 0)); } Else{scalefactor= (int) Math.min (Math.Abs (y) * 255, 255); Mtop.setbackgroundcolor (Color.argb (Scalefactor,255, 0, 0)); Mbottom.setbackgroundcolor (color.transparent); } valueview.settext (String.Format ("x:%1$1.2f,y:%2$1.2f,z:%3$1.2f", Values[0], values[1], values[2]));
4. Registering and unregistering in resume and pause, respectively
@Override protected void Onresume () { super. Onresume (); Msensormanager.registerlistener (This, Maccelerometer, sensormanager.sensor_delay_ui); }
@Override protected void OnPause () { super. OnPause (); Msensormanager.unregisterlistener (this); }
5. Sensor this piece of water is very deep, really want to learn, I found a book called "Android Sensor Advanced Programming", which day to go to see if you can not open.
(v) Persistence of data
5.1 Making the Preferences interface
1. Previously this user information or system configuration interface is their own components slowly realized, there is a thing called preferenceactivity
2. First, the style is still XML-defined, creating a new directory XML under Res
The following is the complete code for Res/xml/settings.xml
<?XML version= "1.0" encoding= "Utf-8"?><Preferencescreenxmlns:android= "Http://schemas.android.com/apk/res/android"> <edittextpreferenceAndroid:defaultvalue= "Joshua"Android:key= "Namepref"android:summary= "Tell us your name"Android:title= "Name" /> <checkboxpreferenceAndroid:defaultvalue= "false"Android:key= "Morepref"Android:title= "Enable more Settings" /> <Preferencescreenandroid:dependency= "Morepref"Android:key= "Morescreen"Android:title= "More Settings"> <listpreferenceAndroid:defaultvalue= "GRN"android:entries= "@array/color_names"android:entryvalues= "@array/color_values"Android:key= "Colorpref"android:summary= "Choose your favorite Color"Android:title= "Favorite Color" /> <preferencecategoryAndroid:title= "Location SETTINGS"> <checkboxpreferenceAndroid:defaultvalue= "true"Android:key= "Gpspref"android:summary= "Use GPS to find"Android:title= " Use of GPS location" /> <checkboxpreferenceAndroid:defaultvalue= "true"Android:key= "Networkpref"android:summary= "Use NetWork to find you"Android:title= "Use NetWork location" /> </preferencecategory> </Preferencescreen></Preferencescreen>
3. Next, let the current activity inherit preferenceactivity
Public class extends Appcompatactivity
4. Finally, bind the XML resource to the current activity
Addpreferencesfromresource (r.xml.color_setting);
5. In addition, the default preference content can be loaded, which is perfectly combined with sharedpreferences
Preferencemanager.setdefaultvalues (settingsactivity. thisfalse);
@Override protected void Onresume () { super.onresume (); Sharedpreferences settings =preferencemanager.getdefaultsharedpreferences ( This =settings.getstring ("Namepref", " boolean ismoreenabled=settings.getboolean ("Morepref", false ); }
6. However,preferenceactivity has completed its historical mission, and now Google recommends the use of preferencefragment
Very simple, directly on the source
@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_settings); Preferencemanager.setdefaultvalues (settingsactivity. This, R.xml.settings,false); Getfragmentmanager (). BeginTransaction (). replace (R.id.preference,Newprefsfragment ()). commit (); } Public Static classPrefsfragmentextendspreferencefragment {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Addpreferencesfromresource (r.xml.settings); } }
Android5.0 Development Paradigm Daquan Reading notes (Fri)